Redis Cluster / Redis Cluster Master-Slave Model: The Ultimate Architecture of Redis
The Redis cluster is the ultimate architecture of Redis. It allows for horizontal scaling of Redis.
In Redis cluster, we decide to spread the data we are storing across multiple machines, which is known as Sharding. So each such Redis instance in the cluster is considered a shard of the whole data.
The Redis Cluster uses algorithmic sharding. To find the shard for a given key, we hash the key and mod the total result by the number of shards. Then, using a deterministic hash function, meaning that a given key will always map to the same shard, we can reason about where a particular key will be when we read it in the future.
To handle further addition of shards into the system (resharding), the Redis cluster uses Hashslot, to which all of the data is mapped. Thus, when we add new shards, we simply move hashslots from shard to shard and simplify the process of adding new primary instances into the cluster. And to the advantage, this is possible without any downtime, and minimal performance hit. Let’s look at an example below:
Consider the number of hashslots to be 10K.
Instance1 contains hashslots from 0 to 5000,
Instance2 contains hashslots from 5001 to 10000.Now, let’s say we need to add another instance, now the distribution of hashslots comes to,
Instance1 contains hashslots from 0 to 3333.
Instance2 contains hashslots from 3334 to 6667.
Instance3 contains hashslots from 6668 to 10000.
What is gossiping in the Redis cluster?
To determine the entire cluster’s health, the redis cluster uses gossiping. In the example below, we have 3 main instances and 3 secondary nodes of them. All these nodes constantly determine which nodes are currently available to serve requests. Suppose, if enough shards agree that instance1 is not responsive, they can promote instance1’s secondary into a primary to keep the cluster healthy. As a general rule of thumb, it is essential to have an odd number of primary nodes and two replicas each for the most robust and fault-tolerant network.
Redis and its role in System Design
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It is widely used for its fast performance, flexibility, and ease of use.
|