Design Consistent Hashing
Ch. 6 Building Blocks System designThe problem
Eight cache nodes hold two hundred million objects, and every read starts with the same question: which node owns this key? Answer it wrong after a topology change and clients knock on doors that no longer hold the data — cache misses spike, the origin database takes the stampede, and the site crawls.
The textbook scheme, hash(key) % N, answers the question cheaply but
falls apart the moment a node joins or leaves: the modulus changes, and nearly every
key re-homes at once. Consistent hashing keeps the hash function and drops the
modulo — servers and keys land on one shared circle, and a key belongs to the first
server clockwise from its position.
We will size a cache fleet, watch mod-N reshuffle almost everything, build the ring, grow and shrink it, expose its two weaknesses (fat arcs and bunched servers), repair both with virtual nodes, and learn exactly which arc of keys to copy when the circle changes.
Requirements
Functional — what it must do
- Place every cache key on exactly one node, deterministically: with the node set unchanged, the same key maps to the same node from every client.
- When a node joins or leaves, remap only the keys in the affected arc — every other key keeps its home and stays a hit.
- Compute placement locally: each client derives it from a small ring map, with no coordinator call on the read path.
- Keep key load near-even across nodes, no matter which machines happen to be in the pool at the time.
Non-functional — how well it must do it
- Placement costs well under 1 ms: one hash plus a nearest-clockwise scan over an in-memory map.
- Losing one of 8 nodes re-homes about 12.5% of keys; mod-N would move roughly 7 of every 8.
- With virtual nodes, per-node load stays within a few percent of the mean — about 5% at 200 vnodes per server in a published experiment.
- The ring map is tiny — a few MB per client — and refreshes across the fleet within seconds of a topology change.
- Hit rate barely dips during a topology change: only the keys that physically moved can miss.
Scale — how much it must take
- 300,000 cache reads per second at peak, spread over 8 cache nodes and 200 million stored objects.
Assumptions — the numbers this design is sized for
A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.
Takeaways
- Mod-N placement re-homes nearly every key when the pool changes — the failure mode is a cache-miss storm into the origin database.
- Consistent hashing puts servers and keys on one circle; a key belongs to the first server clockwise from its position.
- Any topology change moves a single arc, not the keyspace: adding a server re-homes about k/n keys, removing one hands its keys to the clockwise neighbor.
- Plain rings have two flaws — arcs of unequal size and servers that bunch up; virtual nodes fix both by giving each server many points.
- More virtual nodes mean tighter balance — about 5% of the mean at 200 per server in a published experiment — for a ring map of a few MB. Tune the count.
- To redistribute after a change, walk anticlockwise from the changed node to the previous server: that arc, and only that arc, needs copying.
Mod-N placement, and why it snaps
The default scheme — hash(key) % N — is simple and balanced, right up to the moment the pool changes size.
- 1Spread with the modulo
Give each key a number from a fixed hash function, then take it modulo the pool size. With 4 servers, hash % 4 lands two of the eight keys on every server — a perfectly even spread.
- 2Two keys per server
Read the bottom row as placements: key0 goes to server 1, key1 to server 0, and so on. Every server ends up responsible for exactly two of the eight keys.
- 3Server 1 goes offline
One node dies and the pool is 3 servers now. The hash values have not moved — but dividing by 3 instead of 4 reshuffles the bottom row: seven of the eight keys suddenly point somewhere new.
- 4A storm of misses
Clients still remember where key0 used to live, so they ask servers that no longer hold it. Each re-homed key is a miss until its new home is refilled — and the origin database absorbs the whole stampede at once.
Bend the hash space into a ring
Consistent hashing keeps one hash function and drops the modulo. The trick is the shape of the space: a line whose ends meet.
- 1A very long line
Use a cryptographic hash such as SHA-1 and every key becomes a number between 0 and 2^160 − 1. Picture that whole range as one straight line: small hashes at the left end, enormous ones at the right.
- 2Join the ends
Bend the line until the two ends touch and it becomes a circle. A hash just past the maximum rolls around to near zero — the space no longer has edges, and every value has neighbors on both sides.
- 3Servers take positions
Hash the servers themselves — by IP address or hostname — with the same function. Each one lands at its own spot on the circle. Notice what is missing: no modulo, and no mention of the pool size anywhere.
- 4Keys take positions
Hash each key the same way. Keys and servers now share one circle, and physical distance means nothing — the only thing that matters is which direction you walk from a key.
- 5Walk clockwise to the first server
To place a key, start at its position and walk clockwise; the first server you meet owns it. key0 meets s0 first, key1 meets s1 — every key now has exactly one home, decided without a division in sight.
Grow the pool: one arc changes
Add a server to the ring and the change is local: only the keys in the newcomer's arc move. Everyone else keeps their home.
- 1The baseline ring
Four servers, four keys, one ownership arrow each: k0 to s0, k1 to s1, k2 to s2, k3 to s3. Memorize this frame — we are about to grow it.
- 2s4 joins between k0 and s0
A fifth server is hashed onto the circle and lands between k0 and s0. Walking clockwise, k0 now meets s4 first, so k0 moves. k1, k2 and k3 keep walking to the same servers as before — their view of the ring barely changed.
- 3A fraction, not a flood
One key of four moved — 25%. The general rule: adding a server re-homes about k over n keys, where k is the key count and n the new pool size. Mod-N, by contrast, re-homes nearly all of them every single time.
Shrink the pool: the neighbor inherits
Take a server away and its keys slide to the next server clockwise. The rest of the ring never hears about it.
- 1Back to four servers
The ring as we found it: each key's arrow ends at the first server clockwise. Now imagine s1 starts reporting hardware failure.
- 2s1 disappears
Drop s1 from the circle. Only the keys that pointed at it are affected: k1 walks clockwise again and this time meets s2. Its data gets pulled from backing storage into s2 — the other three keys never move.
- 3Blast radius: one key
One key of four moved, and it landed on the dead node's clockwise neighbor — exactly where the ring always sends overflow. The blast radius of any removal is a single arc, never the whole keyspace.
Two flaws in the plain ring
The basic scheme already beats mod-N, but two defects remain: arcs of unequal size, and servers that bunch up on the circle.
- 1Arcs come in all sizes
Each server owns the arc from its position to its clockwise neighbor. Nothing keeps those arcs equal: here s2's stretch is about 105 degrees while s3's is 80 — the fat arc's owner stores and serves a visibly larger share of the keys.
- 2Bunched servers, idle servers
Worse: the hash function does not know to spread servers out. Park three of them close together on the circle and every clockwise walk from the far side ends at whichever one sits first — here s0 collects four of the eight keys while s1 and s2 idle at zero.
- 3The cure has a name
Both flaws share one cure: stop letting each server be a single point on the circle. Give every server many positions instead, and the arcs average out no matter where the hash happens to drop them.
Virtual nodes flatten the ring
Represent each real server with dozens or hundreds of ring positions. The lookup rule is unchanged — the first point clockwise just has a longer name.
- 1One server, three points
Server 0 now appears on the circle as s0_0, s0_1 and s0_2; server 1 as s1_0, s1_1 and s1_2. Six positions, two real machines. The count three is arbitrary — production fleets run far more.
- 2Same rule, longer names
Drop four keys on the circle and walk clockwise as always. k0 meets s1_0, which is just server 1 wearing another badge — the vnode-to-server mapping is one table lookup.
- 3More points, tighter spread
Every extra position smooths the arcs a little more. In a published experiment, 100 virtual nodes per server kept per-node load within about 10% of the mean, and 200 tightened that to about 5%.
- 4Pay the small bill
The cost is bookkeeping: 8 servers times 200 vnodes is 1,600 ring entries instead of 8 — still only a few megabytes on every client, and lookups stay under a millisecond. Treat the vnode count as a tunable, not a constant.
Find the affected arc
When the ring changes, something must copy the re-homed keys. Consistent hashing tells you exactly which slice to touch: start at the changed node and walk anticlockwise.
- 1Adding: walk anticlockwise
s4 has just joined at the bottom of the circle. To find what it must take over, start at s4 and walk anticlockwise — against the lookup direction — until you hit the previous server, s3. Every key inside that arc moves to s4.
- 2Two keys re-home
s4 lands between s3 and s0, so k4 and k5 — the only keys in the arc — now walk into s4 instead of s0. The transfer job copies exactly those two; k0 through k3 never move.
- 3Removal is the same walk
Remove s1 instead, and the affected arc runs anticlockwise from s1 back to s0: keys in that slice — just k1 here — are copied to s2, s1's clockwise neighbor. One arc, one copy job, and the other keys never notice.
End to end
Everything above in one run: a key finds its home on the ring, a node dies mid-morning, and only one arc of the circle moves.
- 1The whole system
One picture of the fleet. A client wants user:9173; its cache client holds the ring map — 8 cache nodes, 200 million keys, 300,000 reads a second, about 25 million keys apiece. The origin database idles behind the pool, waiting for the day a key goes cold.
- 2A key finds its home
The client hashes user:9173 onto the circle — it lands near the 115-degree mark — and walks clockwise: the first server is s5. The request goes straight there and comes back a hit; the whole ring walk cost well under a millisecond.
- 3s5 dies mid-morning
s5 goes dark. Within seconds every ring map drops it, and each key that used to walk into s5 continues clockwise to s6 instead. That is one arc of the circle — 25 million of 200 million keys, about 12.5%. Mod-N would have re-homed roughly seven of every eight.
- 4The arc refills from origin
The re-homed keys are cold on their new home, so the fill job pulls one arc's worth of data from the origin database. The other 175 million keys never moved, and they keep serving hits the entire time.
- 5Steady state, again
Minutes later the fleet is healthy: seven nodes each serving close to their share — within about 5% of the mean thanks to the 200 virtual nodes — and the origin is back to its idle trickle. The next topology change will cost the same: one arc.
In the wild
The same circle partitions some of the biggest data systems on the internet. Here is who runs which piece.
- 1You have seen this circle before
Consistent hashing is not an interview curiosity. The standard reading lists for this technique name Amazon's Dynamo, Apache Cassandra, Discord, Akamai's content delivery network, and Google's Maglev load balancer — all of them lean on the ring for placement.
- 2Dynamo: the ring is the partitioner
Amazon's Dynamo paper describes a key-value store partitioned across its cluster with consistent hashing: the circle decides which node stores each key, so adding or retiring storage nodes moves a sliver of the keyspace instead of all of it.
- 3Cassandra: partitioning across the cluster
Apache Cassandra applies the same idea to partition data across a decentralized cluster — no master hands out placements, and the ring is recomputed by the nodes themselves, which is exactly the property the paper's title advertises.
- 4Discord: chat at five million concurrent
Discord's write-up on scaling to five million concurrent users is a fixture of consistent-hashing reading lists: at that scale, spreading load across servers without mass reshuffling whenever capacity changes is precisely the ring's job.
- 5Akamai and Maglev: the edge and the balancer
Akamai's content delivery network and Google's Maglev software load balancer round out the list — one spreads cached web content across edge locations, the other maps every flowing connection to a backend, and both need the mapping to stay stable as machines come and go.