Consistent Hashing Explained: Scale a Cluster Without Moving Everything
Consistent hashing explained from the problem up — why plain hash % N breaks when you add a server, how the hash ring maps keys to the next server clockwise so only 1/N of keys move, virtual nodes for even load, and where it is used.
🔵 Fundamentals · Fresher-friendly → 🔴 goes deep
Consistent hashing is one of those ideas that sounds intimidating but is genuinely elegant once you see the picture — and it shows up everywhere: sharded databases, distributed caches, load balancers, Cassandra, DynamoDB. Interviewers love it because it separates people who memorise from people who understand. Let's build it from the exact problem it solves.
The problem: plain hashing breaks when you scale
Say you spread data across 4 cache servers with the obvious formula: server = hash(key) % 4. Simple and even. But the moment you add a 5th server, the formula becomes % 5 — and now almost every key maps to a different server. Nearly your entire cache is suddenly "wrong," everything misses at once, and the database gets flooded. Removing a server is just as bad.
key "user42": hash % 4 = server 2 add one server → hash % 5 = server 4 ← moved! Result: adding 1 server remaps ~80% of keys. Disaster.
So plain modulo hashing makes scaling a cluster incredibly disruptive. Consistent hashing fixes exactly this.
The idea: a ring
🟠 Imagine a circle numbered 0 up to a huge number and back to 0 — a hash ring. Now:
- Hash each server to a point on the ring.
- Hash each key to a point on the ring too.
- A key belongs to the first server you meet going clockwise from the key's position.
That's the whole mechanism. key1 sits just after Server A, so walking clockwise it lands on Server B. Every key deterministically maps to a server this way.
Why it's magic: adding/removing barely moves anything
🟠 Here's the payoff. When you add a new server, it drops onto some point of the ring and only takes over the keys between it and the previous server — just its own slice. Every other key stays exactly where it was. When a server leaves (crash), only its keys move — to the next server clockwise — and the rest are untouched.
So instead of remapping ~everything (plain modulo), you remap only about 1/N of the keys. For a cache, that means a small dip in hit rate instead of a full cold cache and a database meltdown. That single property is why consistent hashing underpins nearly every distributed data system.
The catch, and the fix: virtual nodes
🔴 Naive consistent hashing has a flaw: with only a few servers placed randomly, the ring can be lopsided — one server ends up responsible for a big arc and gets overloaded, while another gets a tiny slice. And when a server dies, all its load dumps onto the single next server.
The fix is virtual nodes: instead of placing each server once, place it at many points around the ring (say 100–200 "virtual" copies each). Now every physical server's responsibility is scattered into lots of small arcs, so load is even, and when one server dies its load spreads across many others rather than crushing one. Virtual nodes are what make consistent hashing practical — and naming them is a strong senior signal.
Where it's actually used
| System | Uses consistent hashing for |
|---|---|
| Cassandra, DynamoDB | Deciding which node stores each row |
| Distributed caches (Memcached clients) | Mapping keys to cache servers |
| Load balancers / CDNs | Sticky routing that survives node changes |
The interview-ready summary
If asked "how do you shard a cache so adding a server doesn't wipe it?", the answer is crisp: "Consistent hashing — put servers and keys on a ring, each key goes to the next server clockwise, so adding or removing a node only moves about 1/N of the keys. And I'd use virtual nodes to keep the load balanced." Say that and you've shown you understand both the mechanism and the practical fix. That's exactly the depth they're after.
What to read next
- Database Sharding — the problem consistent hashing helps solve
- Message Queues & Kafka — decoupling with async processing
- Caching Strategies — where distributed caches live
- ← The complete System Design guide (hub)
Frequently Asked Questions
What problem does consistent hashing solve?
With plain hashing (server = hash(key) % N), adding or removing a server changes N and remaps almost every key, wiping caches and flooding the database. Consistent hashing places servers and keys on a ring so that adding or removing a server only moves about 1/N of the keys instead of nearly all of them.
How does the hash ring work?
Both servers and keys are hashed to points on an imaginary circle. Each key belongs to the first server found going clockwise from the key position. When a server is added it only takes the slice of keys between it and the previous server; when one leaves, only its keys move to the next server clockwise.
What are virtual nodes in consistent hashing?
Virtual nodes place each physical server at many points around the ring instead of just one. This spreads each server responsibility into many small arcs, so load is even and, when a server dies, its load spreads across many others instead of crushing a single next server.