System design · Rebalancing with minimal movement
Consistent hashing explained
The obvious rule, hash the key modulo the number of servers, breaks the moment the cluster changes size. Place servers and keys on a hash ring, give each key to the next server clockwise, and give each server many virtual nodes. Going from 10 to 11 nodes then moves about 9% of keys, against about 91% with hash mod N.
Updated · 5 min read
The short version
- Hash mod N moves about N/(N+1) of all keys when one server is added. A hash ring moves about 1/(N+1).
- Servers and keys hash onto one circle. A key belongs to the first server clockwise; lookup is a binary search over a sorted array.
- Virtual nodes give each server 100 to 200 points to even out load and spread a failed server's keys.
- It balances keys, not traffic. A hot key still lands on one server.
Clarifying questions to ask first
- Cache keys or stored data? (For a cache, a moved key is a miss. For a database, it means copying data.)
- How often do nodes join or leave? (Assume regularly. If the cluster never changes size, hash mod N is fine.)
- Are all machines the same size? (Assume not, so bigger machines should take a bigger share of keys.)
- Does each key need replicas? (Assume yes, on distinct physical servers.)
The problem with hash mod N
server = hash(key) % N spreads keys evenly with no lookup table. But a key stays put only if hash % N and hash % (N+1) agree, and for most keys they don't.
| Change | Keys moved with hash mod N | Keys moved with a hash ring |
|---|---|---|
| 4 → 5 nodes | 4/5 = 80% | 1/5 = 20% |
| 10 → 11 nodes | 10/11 ≈ 91% | 1/11 ≈ 9% |
| 100 → 101 nodes | 100/101 ≈ 99% | 1/101 ≈ 1% |
| 5 → 4 nodes (one fails) | 4/5 = 80% | 1/5 = 20%, only the failed node's keys |
For a cache, 80% of keys moving means an 80% miss rate after a deploy or crash, all landing on the database at once.
The hash ring
Bend the hash output range, say 0 to 2^32 - 1, into a circle. Hash each server's ID to a point on it. To find a key's owner, hash the key and walk clockwise to the first server.
A new server takes over only the keys between it and the previous server counter-clockwise. A removed server's keys pass to the next server clockwise. Nothing else moves.
ring = sorted list of (position, node)
function lookup(key):
h = hash(key)
lo, hi = 0, len(ring)
while lo < hi:
mid = (lo + hi) / 2
if ring[mid].position < h:
lo = mid + 1
else:
hi = mid
if lo == len(ring):
lo = 0
return ring[lo].nodeLookup is O(log M) for M points on the ring. Clients hold the ring locally, so routing needs no network call, only a fresh copy when membership changes.
Virtual nodes
With one point per server, arcs have random lengths, so one server can own several times the keys of another. A failed server also dumps its whole arc on one neighbour, which may then fall over too.
Give each physical server many points instead, by hashing "node-a#0", "node-a#1" and so on. With 100 to 200 points per server, each owns many small arcs and its share stays close to 1/N. A failed server's arcs are scattered, so its keys spread across many survivors. A server with twice the memory gets twice the points and about twice the keys.
The cost is a bigger ring and more small ranges to track when moving data. Cassandra lowered its default token count per node in version 4.0 for this reason.
Replication on the ring
For R replicas, walk clockwise from the key and take the first R distinct physical servers, skipping points owned by a server you already picked. Dynamo calls this the preference list. Any of them can serve the key, and when one fails the next server along the ring takes its place.
Where it's used
- Amazon's Dynamo paper made the ring, virtual nodes and preference lists standard for key-value stores.
- Cassandra nodes own token ranges, and replicas are placed by walking the ring with rack and datacenter awareness.
- Memcached clients hash keys to servers with a ring (the ketama scheme), so adding a cache server doesn't wipe the cache.
- Nginx, HAProxy and Envoy support consistent-hash routing to send the same user or session to the same backend.
Alternatives
- Rendezvous hashing scores every server with hash(key, server) and picks the highest. It moves about 1/N of keys, needs no virtual nodes, and the top R scores make a replica list. Lookup is O(N): fine for tens of servers, while a ring scales better.
- Jump consistent hash, from Google, maps a key to one of N numbered buckets with no ring or memory and moves 1/(N+1) of keys per added bucket. Buckets change only at the end, so it suits numbered shards, not servers that fail at random.
Common traps
- Treating it as a hot-key fix. One celebrity or viral product still maps to one server. Cache it in front, replicate it to more servers, or split it into sub-keys like key#1 to key#10.
- Forgetting to wrap when a key hashes past the ring's largest point. Continue to the first point.
- Deriving token positions from the order servers appear in a discovery response. Ownership then changes with no membership change; derive positions from stable server and virtual-node IDs.
- Routing to a new storage node before its ranges have arrived. A new owner on the ring does not copy the data; plan the transfer and the routing switch as separate steps.
Frequently asked questions
What is consistent hashing in simple terms?
+
A way to assign keys to servers so that adding or removing one moves only a small share of keys. Each key belongs to the first server clockwise from it on a shared circle.
How many keys move when you add a node?
+
About 1/(N+1) of keys when going from N to N+1 nodes. With hash mod N, about N/(N+1) move, which is 80% from 4 to 5 nodes.
Why do we need virtual nodes?
+
One point per server gives uneven load and dumps a failed server's keys on one neighbour. Many points per server fix both.
Does consistent hashing solve hot keys?
+
No. A single very popular key still maps to one server and needs its own fix.