0
0
Redisquery~10 mins

Hash slots distribution in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hash slots distribution
Start with 16384 hash slots
Calculate key's hash value
Modulo operation: hash_value % 16384
Assign key to resulting hash slot
Distribute slots evenly across cluster nodes
Client queries node responsible for slot
Node handles key operations
END
Redis cluster divides all keys into 16384 slots by hashing keys and assigning each to a slot, which are then distributed across nodes.
Execution Sample
Redis
HASH_SLOT = CRC16(key) % 16384
Assign key to HASH_SLOT
Distribute slots to cluster nodes
Client queries node for HASH_SLOT
Node processes key commands
This process shows how Redis calculates a key's hash slot and routes commands to the correct node.
Execution Table
StepActionKeyCRC16 HashHash Slot (CRC16 % 16384)Node Assigned
1Calculate CRC16 hashuser:10001234512345 % 16384 = 12345Node A
2Calculate CRC16 hashorder:5002345623456 % 16384 = 7072Node B
3Calculate CRC16 hashsession:abc3456734567 % 16384 = 1799Node C
4Calculate CRC16 hashcart:7894567845678 % 16384 = 12910Node A
5Calculate CRC16 hashproduct:3215678956789 % 16384 = 10637Node B
6End----
💡 All keys assigned to slots between 0 and 16383, distributed to nodes accordingly.
Variable Tracker
VariableStartAfter user:1000After order:500After session:abcAfter cart:789After product:321Final
Key-user:1000order:500session:abccart:789product:321-
CRC16 Hash-1234523456345674567856789-
Hash Slot-12345707217991291010637-
Node Assigned-Node ANode BNode CNode ANode B-
Key Moments - 3 Insights
Why do we use modulo 16384 in the hash slot calculation?
Because Redis cluster has exactly 16384 hash slots numbered 0 to 16383, modulo ensures the hash value fits into this range (see execution_table rows 1-5).
How does Redis decide which node handles a key?
Each node is assigned a range of hash slots; the key's slot determines the node (see Node Assigned column in execution_table).
What happens if two keys have the same hash slot?
They are stored on the same node responsible for that slot, allowing efficient grouping (implied by slot assignment in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hash slot for the key 'order:500'?
A23456
B12345
C7072
D8415
💡 Hint
Check the 'Hash Slot' column for 'order:500' in execution_table row 2.
At which step does the key get assigned to Node C?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Node Assigned' column in execution_table to find Node C.
If the modulo was changed from 16384 to 10000, what would happen to the hash slot for 'cart:789'?
AIt would remain 12910
BIt would be 45678 % 10000 = 5678
CIt would be 45678 % 16384 = 12910
DIt would be 45678
💡 Hint
Recall the hash slot is calculated as CRC16(key) % number_of_slots (see execution_sample).
Concept Snapshot
Redis Cluster uses 16384 hash slots.
Each key's CRC16 hash is computed.
Hash slot = CRC16(key) % 16384.
Slots are distributed evenly across nodes.
Clients query nodes by slot to access keys.
This enables scalable, distributed key storage.
Full Transcript
Redis Cluster divides all keys into 16384 hash slots. Each key is hashed using CRC16, then the hash value is taken modulo 16384 to find its slot number. This slot number determines which cluster node stores the key. The cluster distributes these slots evenly among nodes. When a client wants to access a key, it calculates the key's slot and sends the command to the node responsible for that slot. This process ensures keys are spread across nodes for load balancing and scalability.