The problem with modulo hashing
3 cache nodes, key "user:42"
slot = hash("user:42") % 3 → node 1
Add node 4:
slot = hash("user:42") % 4 → node 2 ← MOVED! cache miss storm
When StreamHub expanded Redis from 4 to 6 nodes using naive modulo, cache hit ratio dropped from 92% to 41% until keys repopulated — a classic cache stampede on the database.
The hash ring
New node E joins → only ~1/N keys move, not all keys.
Ring mechanics
- Hash space is a fixed ring (0 to 2³²−1 or 0 to 1.0).
- Each node is placed at one or more positions on the ring (via hash of node ID).
- Each key is hashed to a position on the ring.
- Walk clockwise from the key's position — the first node encountered owns the key.
- Adding a node steals only keys between its predecessor and itself.
Only keys in the affected arc move — typically 1/N of total keys when adding one of N nodes.
Virtual nodes (vnodes)
Physical machines get multiple positions on the ring for even distribution:
Physical node A → vnode A0, A1, A2, A3 on ring
Physical node B → vnode B0, B1, B2, B3 on ring
Without vnodes, uneven key density clusters on a few nodes. Redis Cluster and Dynamo use vnode-style partitioning.
# Redis Cluster — key must include hash tag for multi-key ops
SET user:{42}:session abc123
GET user:{42}:profile
# {42} ensures both keys land on same slot
The {...} hash tag forces related keys to the same slot — required for atomic multi-key operations.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 5 sections
- Consistent hashing use cases
- Adding and removing nodes
- Comparison with alternatives
- Hot keys and mitigation
- Rendezvous hashing (alternative)
Test yourself
Answer these before moving on — recall is what makes it stick.