Redis Cluster

  • Used for Data storage in more than one instance
  • Redis Cluster allows you to host multiple master shards
  • Data is automatically sharded across multiple Redis Nodes
  • Automatically splits data among multiple nodes
  • Continues to operation when a subset of the nodes are experiencing failures
  • Some degree of availability during partitions

Goals :

  • High performance and linear scalability up to 1000 nodes
  • An acceptable degree of writing safety
  • Availability

How it works

  • DataSharding is based on Key-Hash tags
  • Each master node in a cluster handles a subset of the 16384 hash slots
  • How is hash calculated? which decides the node in which the key will land ?
    • It takes the CRC16 of the key and it decides the bucket/node in which it is gonna be stored
    • Always the CRC16 of the key will be less than 16384 values
    • So suppose if there are 4 nodes then each node can handle 16384/4 nodes
  • Every node in a redis cluster is responsible for a subset of the hash slots
  • Redis Cluster only supports Database 0

Example :

We will be creating a cluster of 6 nodes, out of which 3 will be master nodes and 3 will be replica/slave nodes.

We will start by creating .conf file for our nodes

n1_redis.conf

port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

Similarly, we will create 5 more redis.conf file. n2_redis.conf file, n3_redis.conf file,n4_redis.conf file,n5_redis.conf file and n6_redis.conf file

We will move each conf file into a directory so that we can keep logs and other file for a single node into a directory

Now we will naviagte to each directory and start the redis-server with that .conf file

redis-server n1_redis.conf &

Similarly start all the 6 clusters

Check if all the 6 clusters are running using ps command

ps -ef | grep -i 'redis'

Now enable replica for all the 6 nodes to form a failover cluster

redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1

You have to enter Yes to accept the configuration

Method one to view the summary of the cluster

Method two to view the summary of the cluster

Checking the slots of the Cluster

Connect to the cluster using below command

redis-cli -c -p 7001

Example of how keys are sharded between master nodes

In above example 7002, 7003 and 7004 are master

Leave a Comment