How does redis cluster work

So this is how redis cluster will look like which has 3 masters and 3
slaves.Each master node has a slave node. If master node goes down, slave
node will be promoted to the master role

So lets discuss a bit how redis cluster works internally. Redis basically
divides whole cluster into 16384 slots. These slots are like buckets which
can hold multiple cache entries. So if you have 3 nodes in a cluster, it
will divide these slots equally. first 5500 will go to node 1 and so on.

When ever we insert any key, redis will first calculate the CRC of the key
and the result and then result mod 16384 is done. This mod operation will
return a number which will lie between 0 and 16384. So in this way redis
finds out which slot a given key should go to.

Now if we add more nodes to the cluster. Say node D, REDIS WILL MOVE FEW
Similarly if a node is removed, its slot will be moved to remaining nodes.

Now lets see how multi key operations will work

Get_multi(‘red_male_shirts’, ‘blue_male_shirts’)

In this example we are
fetching 2 keys red male shirts and blue male shirts. If both keys belong
to different nodes, redis will throw an exception that keys dont belong to
same node.. Redis wants all keys to belong to same node for multi key

This can be problematic. So there is a way to force keys to to a fix node.
This concept in redis is known as hashtagging. So if we prepend they key
with curly braces with some string between, redis will use this string to
calculate the slot the given key should go to. So if you want multiple keys
to go to same node, so that you can later on issue multi key operations on
them, you need to prepend the key with curly braces and some constant
For example I have two keys blue_shirts and red_shirts and I want them to
go to same node as there are chances of issuing multi key operations on
them. We can append constant strings with the key like this.Now redis will
use the string male_clothes to calculate the node.So boht these strings
will go to same node.

{male_clothes}blue_shirt and {male_clothes}red_shirt

This was how redis cluster internally works.

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *