Consistent Hashing
Before this, it helps to know: Replication & Sharding.
Read a little, play a little. No scary maths, and no rush.
The problem with hash(key) % N
Modulo hashing is simple and wrong at scale: with 4 cache servers, hash(key) % 4 sends each key to a server. Add a 5th server for more capacity, and % 5 relocates almost every key — a cache that was 95% full of useful data goes cold all at once, and every backing database takes the full read load until it refills. The problem isn't hashing — it's that one number (N) decides everyone's home.
The ring
Consistent hashing places both servers and keys on the same hash ring (imagine hash values 0 to 2^32-1 wrapped into a circle). A key belongs to the first server found walking clockwise from its position. Add a server: it only takes over the keys between itself and the previous server on the ring — everyone else's assignment is untouched. Remove a server: only its keys move, to its clockwise neighbor. One server joining or leaving reshuffles a small slice of the ring, not the whole thing.
Virtual nodes fix the lumpy-ring problem
A handful of real servers placed randomly on a ring can create uneven gaps — one server ends up owning way more of the ring than another. The fix: give each real server many virtual nodes (hundreds of points on the ring instead of one), spreading its share evenly and making load balance predictable even with few physical servers.
Where this actually shows up
Distributed caches (Memcached clients implement this client-side), DynamoDB-style key-value stores, CDN request routing, and sharded databases all use some form of this idea. Any time you hear "minimal data movement on scale-out," consistent hashing (or a close relative) is almost certainly underneath it.
The difference, in numbers
Grow a cache from 4 servers to 5.
- Modulo: a key stays put only if
hash % 4andhash % 5happen to agree, which is true for about 1 key in 5. Roughly 80% of keys move, and the cache effectively starts empty. - Ring: the new server takes over its fair share, about one fifth of the keys, and nothing else changes. Roughly 20% of keys move.
On a busy system, that is the difference between a quiet capacity upgrade and a database meltdown.
When a server leaves
If a server fails or is removed, its keys pass to the next server clockwise. With virtual nodes, that server's many small slices are scattered around the ring, so its load spreads across all the remaining servers instead of doubling the load on one unlucky neighbor.
Copies on the ring
Storage systems built on this idea, in the style of Amazon's Dynamo, keep each key on the next two or three distinct servers clockwise, not just the first. Losing one server then loses no data, and reads can be served by any of the copies.
A close relative
Rendezvous hashing (also called highest-random-weight hashing) reaches the same goal differently: for each key, score every server with a hash of the key and server together, and pick the highest score. It needs no ring and also moves only a small share of keys when servers change, which makes it popular for picking among a modest number of servers.
Remember this
- Modulo hashing reshuffles almost everything when the server count changes.
- A ring means only the keys between two neighbors move when one node joins or leaves.
- Virtual nodes spread one server's share evenly across the ring instead of leaving lumpy gaps.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue