Consistent Hashing
A hashing technique that keeps most data in place when servers are added or removed.
What is it?
A simple way to pick which server holds a piece of data is hash(key) % numberOfServers. The problem: when the number of servers changes (one is added or fails), the result changes for nearly every key, so almost all data must move at once.
Consistent hashing avoids this. It places both servers and data on a circle (a hash ring), so adding or removing a server only affects the small slice of data next to it.
Explain like I'm 10
Picture seats in a circle. Each guest takes the nearest seat clockwise from where their name lands. Remove one seat and only its guest moves to the next seat; everyone else stays put. Compare a rule like 'seat number = name length % total seats', where adding or removing one seat reshuffles almost everyone.
Examples
The core idea of a hash ring
// Simplified: servers and keys both hashed onto the same circular range
const servers = [{ id: "server-A", position: 10 }, { id: "server-B", position: 200 }];
function getServerForKey(key) {
const keyPosition = hash(key) % 360; // hash onto the same circle
// find the first server at or after keyPosition, wrapping around
return servers
.sort((a, b) => a.position - b.position)
.find((s) => s.position >= keyPosition) ?? servers[0];
}How it works
Servers and keys are hashed onto the same circular range. Each key belongs to the next server clockwise from it.
When a server is added, it only takes the keys between itself and the previous server; everything else stays. When a server is removed, only its keys move, to the next server clockwise.
Why does it exist?
Without it, adding or removing one server from a sharded store or distributed cache would force nearly all data to move at once, which is expensive and disruptive. Consistent hashing makes scaling up or down far cheaper by moving as little data as possible.
When to use it
Use it for a distributed cache or sharded data store whose server count will grow or shrink over time, to avoid a full data reshuffle. This is how distributed caches and some NoSQL databases decide where data lives.
When not to use it
For a small, fixed number of servers that will basically never change, plain hash(key) % numberOfServers is easier to reason about and build. Consistent hashing pays off when the server count changes.
Common mistakes
Using simple modulo hashing (
hash(key) % n) in a system that will scale up or down, then getting a massive, disruptive reshuffle when the server count changes.Forgetting a naive hash ring can spread data unevenly if servers land close together. Real implementations give each server several 'virtual nodes' to smooth this out.
Assuming consistent hashing removes all data movement. It minimizes it, but the affected slice still has to move.
Practice exercises
- Easy:
Explain in your own words why
hash(key) % numberOfServersmakes almost all data move when a server is added. - Medium:
Describe how consistent hashing limits how much data moves when a new server joins.
- Hard:
Explain what 'virtual nodes' are in consistent hashing and why they spread data more evenly.
Interview questions
What problem does consistent hashing solve?
It minimizes how much data moves when the number of servers changes, unlike simple modulo hashing, which reshuffles almost everything.
How does a hash ring decide which server owns a given key?
The key is hashed onto the same circle as the servers and belongs to the next server clockwise from it.
What are virtual nodes, and why are they used?
Several ring positions for each real server, so data spreads evenly instead of depending on where each server happens to land.