Redis Clustering

What is Redis?

Shehara Grabau
3 min readMay 20, 2020

Redis stands for Remote Dictionary Service.

Redis is a BSD licensed in-memory key-value store. Redis uses data structures to store data in a No-SQL database server and is a distributed shared cache. Let us first of all make ourselves familiar with these terms.

No-SQL database — These databases do not use rows and columns to store data. i.e. Redis is a database that stores data in formats and not in relational tables

In-memory database — An in-memory database that relies on main memory for computer data storage. In-memory databases are faster and can be used for time-critical applications.

Key-value store — This means that Redis can store data key-value pairs.

For example if Age is 24, Age is the key and 24 is the value. Redis is considered to be one of the most popular implementations of key-value databases.

Redis — The uses

  • As Redis uses in-memory data storage, it is fast and can be used for time-critical applications such as telecommunications networks. Twitter, one of the popular social media apps, uses Redis.
  • Supports multiple languages
  • Redis supports data structures such as strings, hashes, lists, sets, sorted sets etc.
  • Redis offers two ways to create a distributed environment : Clustering and Sentinel

Redis clustering as a distributed system

Redis Clustering has been introduced to provide a minimalist ready-to-use distributed system.

A Redis cluster is a good option to ensure high availability while also ensuring fast access speed. It uses a different form of automatic sharding across multiple Redis nodes other than consistent hashing. This provides a master-slave setup to enhance availability in case of a failure. Redis clustering follows a distributed slot map approach. In this form of sharding every key is part of a hash slot. A Redis cluster has 16384 such hash slots.

According to the above figure,

  • The first node has hash slots from 0–5460
  • The second node has hash slots from 5461–10922
  • The third node has hash slots from 10923–16383

This method makes Redis clustering scalable as it allows to add and remove nodes easily without any requirement for a downtime.

The master-slave setup mentioned earlier works in such a way to increase the availability of data. Each master node has at least one slave node in a Redis cluster. At a time when the master node fails to operate, the cluster has an automatic way of choosing a slave node as the new master. This makes Redis Clustering as a fault-tolerant system as the failure in node does not affect or stop the entire system from working.

One of the negative points of Redis Clustering is its inability to guarantee consistency. In some cases and under certain conditions, there is a possibility for a Redis Cluster to lose writes that were acknowledged by the system to the client.

The first reason why Redis Cluster can lose writes is because it uses asynchronous replication. This means that during writes, the following happens:

  • Your client writes to the master B.
  • The master B replies OK to your client.
  • The master B propagates the write to its slaves B1, B2 and B3.

Here, B does not wait for an acknowledgement from B1, B2, B3 before replying to the client. Therefore, if your client writes something, B acknowledges the write, but crashes before being able to send the write to its slaves, one of the slaves (that did not receive the write) can be promoted to master, losing the write forever.

--

--

Shehara Grabau
Shehara Grabau

Written by Shehara Grabau

Inspired by the little things in life, I aspire to be more.

No responses yet