Consistent Hashing
Why modulo hashing breaks on rescale, how the ring fixes it, virtual nodes, and the modern alternatives — rendezvous hashing and bounded loads.
#The problem
Distribute keys across N nodes. The obvious approach:
node = hash(key) mod NPerfect distribution, O(1) lookup, no state. And it fails catastrophically the moment N changes.
N = 4: hash("user:42") = 1001 → 1001 mod 4 = 1 → node 1
N = 5: hash("user:42") = 1001 → 1001 mod 5 = 1 → node 1 (lucky)
N = 5: hash("user:99") = 1002 → 1002 mod 5 = 2 → node 2
(was 1002 mod 4 = 2 → node 2 ... also lucky)In general, adding one node remaps roughly (N−1)/N of all keys — about 80% at N=5, ~99% at N=100.
For a cache, that means a near-total cache miss storm: every remapped key is now looked up on a node that doesn't have it, so the entire load hits your database at once. For a sharded datastore it's worse — nearly all your data must physically move.
#The ring
Consistent hashing puts both the keys and the machines on the same circle of numbers — imagine a clock face running from 0 to about four billion, with the end joined back to the start. Hash a machine's name to get its position; hash a key to get its position. A key belongs to the first machine you meet walking clockwise from where it landed.
The trick is what that buys you: when a machine leaves the circle, only the keys sitting between it and the previous machine need a new home. Everyone else is unaffected.
Now add node E between B and C:
Only the keys between B and E move — from C to E. Every other key stays exactly where it was. Removing a node is symmetric: its keys go to the next node clockwise, and nothing else is disturbed.
The result: adding or removing a node remaps ~1/N of keys instead of ~all of them. At 100 nodes, adding one moves 1% of the data rather than 99%.
#Virtual nodes (the necessary fix)
Plain consistent hashing has a real problem: with few nodes, the ring positions are uneven, so the load is uneven. With 3 randomly placed nodes, one might own 60% of the ring. And when a node dies, all of its load goes to exactly one successor — which may then also fail, cascading around the ring.
Solution: put each real machine on the ring at many positions instead of one, by hashing its name together with a counter. Those positions are called virtual nodes — there's still one machine, it just appears in 150 places around the circle.
Physical node A → A#1, A#2, A#3, ... A#150
Physical node B → B#1, B#2, B#3, ... B#150
Ring: A#7 B#22 B#3 C#91 A#44 D#12 B#67 C#5 A#88 ...
(interleaved; each physical node covers many small arcs)With ~100–200 virtual nodes each, load variance drops to a few percent. Critically, when a node fails, its many small arcs are inherited by many different successors — the load spreads instead of concentrating, which is what prevents the cascade.
Virtual nodes also give you a free way to handle machines of different sizes: give the one with twice the memory twice as many positions, and it naturally receives twice the keys.
#Lookup implementation
Keep the virtual node positions in a sorted structure and binary search for the first position ≥ hash(key):
ring = sorted array of (position, node)
lookup(key):
h = hash(key)
i = binary_search_first_ge(ring, h)
return ring[i % len(ring)].node # wrap aroundO(log V) where V is the virtual node count — a few microseconds. With 10 nodes × 150 vnodes = 1,500 entries, that's ~11 comparisons.
For replication (Dynamo-style), walk clockwise and take the next R distinct physical nodes — skipping virtual nodes belonging to a physical node already chosen, which is a detail people get wrong.
#Where it's used
- Distributed caches — Memcached clients, Redis Cluster. (Redis Cluster actually uses a close relative: every key hashes into one of 16,384 fixed slots, and a table assigns slots to nodes. Adding a node means handing it some slots — the same "many logical shards, few machines" idea as pre-splitting.)
- Dynamo-family databases — Cassandra, Riak, DynamoDB, ScyllaDB for partition assignment and replica placement.
- Load balancers — session affinity (each user keeps going to the same server) that mostly survives servers being added or removed: only ~1/N of users get moved to a new server.
- CDNs — mapping content to edge cache servers.
- Sharded application state — WebSocket connection ownership, rate limiter counters.
#The limits, and what to say about them
1. It doesn't fix hot keys. Consistent hashing distributes keys evenly, not load. One key receiving 40% of traffic saturates its node no matter how good the ring is. Fixes: replicate the hot key across several nodes, or front it with a local cache. This is the celebrity problem again.
2. Load can still be uneven under skew. Which motivates:
Consistent hashing with bounded loads. Set a capacity cap (e.g. no node may exceed 1.25× the mean). If a key's target node is at capacity, walk clockwise to the next one with room. You keep most of the stability of the ring while guaranteeing a bound on imbalance. Used in practice by large load-balancing systems.
3. Rebalancing still moves real data. Moving 1/N of a petabyte is still a lot of bytes. Production systems throttle migration to protect foreground traffic.
#Rendezvous hashing (HRW) — the simpler alternative
Often a better choice than the ring, and naming it is a nice signal of breadth.
For each key, compute a score against every node and pick the highest:
node = argmax over all nodes n of hash(key, n)
("argmax" = the node whose score is highest)Advantages over the ring:
- No virtual nodes needed — every node competes for every key with an independent random-looking score, so each node wins about 1/N of the keys without any tuning.
- Trivially supports weights — scale each node's score by its capacity, and a node with double weight wins about twice as many keys.
- Gives you a natural ordered preference list (2nd highest = first replica, 3rd = second replica), which is exactly what replica placement wants.
- Much simpler to implement and reason about; no ring state to keep in sync.
Disadvantage: O(N) per lookup rather than O(log V). Irrelevant for tens or hundreds of nodes; a problem for thousands.
Guarantee: it has the same minimal-disruption property — removing a node only moves keys that mapped to it.
For most systems under a few hundred nodes, rendezvous hashing is the pragmatically better answer, and saying "I'd actually use rendezvous hashing here — same disruption guarantee, no virtual node tuning, and it gives me the replica preference list for free" is a strong, specific opinion.
#Jump consistent hash
A third option worth knowing: a tiny algorithm that maps a key to one of N buckets with optimal balance and minimal disruption, using no memory at all.
Limitation: buckets are numbered 0…N−1 and you can only add or remove from the end. That makes it excellent for sharding into a numbered set of shards you grow sequentially, and unsuitable when arbitrary nodes come and go.
#Choosing
| Situation | Choice |
|---|---|
| Cache cluster, nodes come and go | Consistent hashing + virtual nodes |
| Replica placement, ≤ few hundred nodes | Rendezvous hashing |
| Fixed shard count you grow sequentially | Jump consistent hash |
| Known skew, need a load guarantee | Consistent hashing with bounded loads |
| Need to relocate individual hot tenants | Directory-based (sharding) — not hashing at all |
That last row is worth emphasizing in interviews: hashing gives up control in exchange for statelessness. A hash function needs no stored map — any client can compute where a key lives — but that also means the placement is decided by the math, not by you. When you need to move one specific noisy tenant to its own node, no hash function will do it — you want an explicit directory. Knowing when not to reach for consistent hashing is as valuable as knowing how it works.
#Check yourself
You go from 10 to 11 servers. With hash(key) % N, roughly what share of keys move — and with a consistent-hashing ring?Show answer
Node E is added to a ring between B and C. Which keys move, and from where?Show answer
Why are virtual nodes "the necessary fix" rather than a nice optimisation?Show answer
A server with twice the memory joins the ring. How do you give it twice the keys?Show answer
For three replicas you "take the next three nodes clockwise". What's the bug?Show answer
One key gets 40% of all traffic. Does consistent hashing help? What does?Show answer
When is rendezvous hashing a better choice than a ring?Show answer
You need to move one noisy tenant onto its own server. Which hashing scheme does that?Show answer
#What to carry forward
hash(key) % Nmoves almost every key when N changes; a ring moves about 1/N of them.- Virtual nodes — 100 to 200 per machine — even out load, spread a failed node's keys across many successors, and give you weights for free.
- Consistent hashing spreads keys, not load: hot keys need replication or a cache, and bounded loads cap the imbalance.
- Rendezvous hashing is often simpler for replica placement, jump hash suits shards you grow in order, and a directory is the answer when you need control over placement.