Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what do we mean by hash slot in redis cluster?

Tags:

redis

I have read redis-cluster documents but couldn't get the gist of it. Can someone help me understand it from the basics?

Redis Cluster does not use consistent hashing, but a different form of sharding where every key is conceptually part of what we call an hash slot.

like image 214
Prashant Mudgal Avatar asked Jan 18 '18 05:01

Prashant Mudgal


2 Answers

Hash slots are defined by Redis so the data can be mapped to different nodes in the Redis cluster. The number of slots (16384 ) can be divided and distributed to different nodes.

For example, in a 3 node cluster one node can hold the slots 0 to 5640, next 5461 to 10922 and the third 10923 to 16383. Input key or a part of it is hashed (run against a hash function) to determine a slot number and hence the node to add the key to.

like image 103
techuser soma Avatar answered Oct 13 '22 12:10

techuser soma


Think of it as logical shards. So redis has 16384 logical shards and these logical shards are mapped to the available physical machines in the cluster.

Mapping may look something like:

     0-1000 : Machine 1
  1000-2000 : Machine 2
  2000-3000 : Machine 3
           ...
           ...

When redis gets a key, it does the following:

  1. Calculate hash(key)%16384 -> this finds the logical shard where the given key belongs, let's say it comes to 1500
  2. Look at logical shard to physical machine mapping to identify physical machine. From the above mapping, logical shard 1500 is served by Machine 3. So route the request to physical machine #2.
like image 20
NPE Avatar answered Oct 13 '22 13:10

NPE