Module 4 · Storing Data That Keeps Growing · Lesson 19 of 35
How does consistent hashing work? Adding a machine without moving everything
13 minFree System design course · Beginner to intermediateLesson 4.6By Saket Bhatnagar
Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.
In short
- How does consistent hashing work? It places keys and machines on one circle, and each key belongs to the first machine clockwise from it.
- Going from 4 machines to 5, the new one takes about 1 key in 5. Hash mod N moves about 4 in 5.
- With one spot each, the shares come out uneven, so real systems give each machine many spots.
Video lesson · coming soon
How does consistent hashing work? One new machine, few moves
Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.
- 01 One new node, a cold cache
- 02 Plates round a family table
- 03 Machine E joins the ring
- 04 Step through the ring
- 05 Sixteen of twenty, or one in five
- 06 Recap, then explain it out loud
At 11:50, ten minutes before the final’s noon sale, Arjun Nair adds a fifth Redis node to be safe. Within seconds the cache hit rate falls off a cliff.
As SeatJaldi has spread to 6 states, the cache has grown from one Redis node to four. At ₹3,500 each, a fifth takes the monthly bill from ₹14,000 to ₹17,500. The calm database starts to struggle under a flood of cache misses. Nothing crashed. Nothing was deleted.
The app finds each user’s cached data with hash mod N, the rule from lesson 18. Arjun changed one thing: N went from 4 to 5, just as a fifth shard will next year.
Zoya Mirza frowns. “You did not remove anything. You added a machine. Why did four fifths of the cache go cold?”
How does consistent hashing work?
Consistent hashing places both keys and machines on a circle, and each key belongs to the first machine clockwise from it. The hash function from lesson 10 turns each name or key into a number, its spot.
The one idea to keep: keys and machines sit on one circle, and a new machine only takes from its neighbour. People call the circle the ring.
- The ring here is numbered 0 to 359, like degrees. Real systems use far bigger numbers.
- A key walks clockwise to the first machine it meets, wrapping past 359 to 0.
The ring before machine E
Drag to exploreEach key belongs to the first machine clockwise, so D owns the long stretch from 131 to 250.
Diagram connections:
- Machine A at 20 to Machine B at 100 (Clockwise)
- Machine B at 100 to Machine C at 130 (Clockwise)
- Machine C at 130 to Machine D at 250 (Clockwise)
- Machine D at 250 to Machine A at 20 (Clockwise)
- Machine A at 20: Owns keys from 251 round to 20: u109, u110, u101.
- Machine B at 100: Owns keys from 21 to 100: u102, u103.
- Machine C at 130: Owns keys from 101 to 130: u104.
- Machine D at 250: Owns keys from 131 to 250: u105 to u108.
Now a fifth machine, E, hashes to spot 202, between C and D.
The ring after machine E joins
Drag to exploreMachine E lands at 202 and takes only keys 131 to 202 from D: 2 of the 10 keys. A, B and C are untouched.
Diagram connections:
- Machine A at 20 to Machine B at 100 (Clockwise)
- Machine B at 100 to Machine C at 130 (Clockwise)
- Machine C at 130 to Machine E at 202 (Clockwise)
- Machine E at 202 to Machine D at 250 (Clockwise)
- Machine D at 250 to Machine A at 20 (Clockwise)
- Machine A at 20: Unchanged: u109, u110, u101.
- Machine B at 100: Unchanged: u102, u103.
- Machine C at 130: Unchanged: u104.
- Machine E at 202: Takes keys 131 to 202 from D: u105, u106.
- Machine D at 250: Gives up two keys, keeps u107 and u108.
- A site is slow for a few minutes after its team adds servers, then fast again. The new server’s share of the cache started empty.
- A shop survives a server crash mid-sale. Only that server’s keys moved, to its clockwise neighbour.
- A chat app keeps you on one server for weeks, then moves you. A machine joined or left right before your spot.
Why does adding a machine move so few keys on a ring?
A new machine takes only the keys between it and the machine before it, because every other key still has the same next machine clockwise.
Now your guess. Hash mod N moved 16 of 20 keys in lesson 18, and only keys 1, 2, 3 and 20 stayed. The ring moved 2 of 10.
| 4 to 5 machines | Hash mod N | The ring |
|---|---|---|
| Keys that move | 16 of 20, so 80% | 2 of 10, so 20% |
| Who gives keys up | Every machine | Only D |
Hash mod N is like renumbering every place and dealing all the plates again. The ring is Tara pulling up one chair.
What are virtual nodes, and why do they help?
Virtual nodes put each machine on the ring at many spots, so each machine owns many small slices and the shares come out more even.
Step 6 gave each machine three virtual nodes. At Rukhsana’s table, each person now sits on three chairs spread round the rim, so nobody gets one huge stretch.
| Machine | One spot | Three virtual nodes |
|---|---|---|
| A | 130 (36%) | 100 (28%) |
| B | 80 (22%) | 86 (24%) |
| C | 30 (8%) | 78 (22%) |
| D | 120 (33%) | 96 (27%) |
What just happened? Each spot owns the stretch back to the spot before it, so A’s spots at 28, 150 and 270 own 38, 32 and 30: 100 in all. The biggest share went from about 4 times the smallest to about 1.3 times.
How is consistent hashing used in real systems?
Rings sit inside groups of cache nodes, sharded stores, and load balancers that must send one user to the same server.
Envoy, a widely used reverse proxy, has a ring hash mode: each host, a server behind it, is hashed onto the ring by its address. Adding or removing one of N hosts affects only about 1 in N requests.
Hashes can cluster, so Envoy places each host many times, and its docs say to watch the smallest and largest number of hashes per host.
Rings place copies too: Dynamo puts an item’s copies on the next machines clockwise, skipping repeats of the same machine.
What one new node costs the database
Assume the cache hits 95% of lookups, so 5 in 100 reach the database, and every key is about equally popular.
- Hash mod N: only about 20% of keys keep their node. Hits fall to 0.95 x 0.20 = 0.19, so 81% miss instead of 5%. That is about 16 times the database load (81 / 5).
- Ring: about 80% keep their node. Hits fall to 0.95 x 0.80 = 0.76, so 24% miss instead of 5%. That is about 5 times the load (24 / 5) until E fills. The 20% is an average: E at another spot would take more or fewer keys.
Add a node before the rush and let its cache fill. To replace a node, add the new one first and remove the old one later, so one slice is cold at a time.
const ring = [
{ pos: 20, node: "A" },
{ pos: 100, node: "B" },
{ pos: 130, node: "C" },
{ pos: 250, node: "D" },
];
function ownerOf(keyPos) {
const hit = ring.find((p) => p.pos >= keyPos);
return (hit || ring[0]).node;
}
console.log(ownerOf(151)); // D
console.log(ownerOf(288)); // A, wraps past 359
ring.splice(3, 0, { pos: 202, node: "E" });
console.log(ownerOf(151)); // EWhat just happened? The sorted list is the ring, and find returns the first spot at or after the key. With none, the key wraps to the first spot. Real rings hold far more spots and find a key’s spot without scanning the whole list.
Try it yourself
Draw the ring
Draw a circle marked 0 to 359. Put X at 60, Y at 150 and Z at 270. Mark keys at 10, 40, 75, 110, 140, 170, 200, 230, 260 and 330.
Find each owner
Each key belongs to the first machine at or clockwise after it. Past 359, wrap round to X.
Add a machine
Add W at 200. Which keys change owner, and how many of the ten?
Check your answer
Before: X has 10, 40, 330; Y has 75, 110, 140; Z has 170, 200, 230, 260. W takes 170 and 200 from Z: 2 of 10. Right if only keys between Y and W moved and the owners add up to 10.
So that is why four fifths went cold. With hash mod N, changing 4 to 5 gave about 80% of keys a new node. Those lookups missed and fell through to the database, a cold start for most of the cache.
Zoya moves the cache and the booking shards onto a ring with many virtual nodes. A fifth node is now Tara’s new chair: about one plate in five. Same ₹3,500; the database sees a short rise, not a flood.
Data now lives in many places. Two requests for seat A12 arrive 3 ms apart. Who gets it?
Check yourself
Check yourself
A ring has machines A, B, C and D. A new machine E lands between C and D. Which keys move?
Pick an answer to see why it is right or wrong.
Check yourself
Four machines have one spot each, and one machine owns 36% of the ring. What is the fix?
Pick an answer to see why it is right or wrong.
Remember these three
- Keys and machines sit on one circle; a new machine only takes from its neighbour.
- From 4 machines to 5, hash mod N moves about 4 in 5 keys; a ring moves about 1 in 5.
- One spot per machine shares unevenly, so use many virtual nodes and watch the spread.
Questions people ask
- How does consistent hashing work?
- Consistent hashing puts keys and machines on a circle, and each key belongs to the first machine clockwise from it. Adding a machine moves only the keys right before it.
- What are virtual nodes?
- Virtual nodes are many spots on the ring that stand for one real machine. Each machine gets many small slices, so keys are shared more evenly.
- What is a hash ring example?
- Put machines at 20, 100, 130 and 250 on a ring of 360. A key at 151 belongs to the machine at 250. A new machine at 202 takes only keys 131 to 202.
- Why not use hash mod N?
- Going from 4 machines to 5, hash mod N changes the home of about 4 in 5 keys, emptying most of a cache. A ring moves about 1 in 5.