Consistent hashing, made visible.
Add and remove servers and watch exactly which keys move. Compare the naive hash mod N with a consistent hash ring, then use virtual nodes to even out the load.
Each dot is a key at its hash position. Its colour is the node that owns it. After a change, keys that moved get a white ring.
More points per node means smaller, more even arcs.
With so few points per node the ring is lumpy, so some nodes own much bigger arcs than others. Raise the virtual nodes to even it out.
1.81
Largest node ÷ average. 1.00 is perfectly even.
–
Add or remove a node
| Node | Keys | Share |
|---|---|---|
| A | 75 | 25% |
| B | 44 | 15% |
| C | 181 | 60% |
The tick on each bar marks a perfectly even share.
What you are looking at in the consistent hashing lab
A distributed cache or database has to decide which server stores each key. The simplest rule is hash mod N: hash the key and take the remainder when dividing by the number of servers. It spreads keys evenly, but when N changes almost every key changes owner, which means a huge data move or a cache that suddenly misses almost every request.
Consistent hashing places servers and keys on the same ring of hash values. A key belongs to the first server point clockwise from it. Add a server and it takes over only the keys between itself and its neighbour; remove one and only its keys move. On average that is about 1/N of the keys instead of nearly all of them.
With only one point per server the arcs have very different lengths, so some servers carry far more than their share. Giving each server many virtual nodes scatters it around the ring in many small arcs, and the load evens out. The lab shows the imbalance as the largest node divided by the average, where 1.00 is perfect.
Things to try in the consistent hashing lab
- Switch to Hash mod N, then click + Add node. Watch how many keys get a white ring: most of them move.
- Switch back to Hash ring and add a node again. Only the keys next to the new node move, close to 1 divided by the new number of nodes.
- Look at the imbalance with one virtual node per server, then drag the slider to 100. The bars even out.
- Remove a node on the ring. Only that node's keys move, and they go to the next node clockwise. Nothing else changes owner.
- Raise the key count to 600 and add or remove nodes again. The percentages settle closer to the theory as the sample grows.
Why consistent hashing matters in a system design interview
Consistent hashing is a staple of system design rounds on caches, sharded databases and load balancers. A strong answer has three parts: why hash mod N fails when the cluster changes size, how the ring limits what moves to about 1/N, and how virtual nodes fix uneven load (and let a stronger machine take more points).
For the full discussion, including range and directory sharding, shard keys and the cost of resharding, read the replication and sharding lesson. Real systems vary: some use a fixed number of hash slots instead of a ring, and the principle (move as little as possible) is the same.