GuidesConsistent hashing
Consistent hashing explained, with numbers
9 min read · Updated 1 December 2026
You run a cache in front of your database, spread over four cache servers. To decide which server holds a key, you do the obvious thing: hash the key and take the remainder when dividing by four. Every key has a home, and load is even.
Then traffic grows and you add a fifth server. Suddenly almost every lookup misses. The cache is still full of data, but nearly every key now points at a different server. Your database, which was answering 1,200 queries a second behind the cache, gets hit with 40,000.
Consistent hashing is the fix. It is a way of assigning keys to servers so that adding or removing one server moves only a small share of the keys. This guide shows the problem with real numbers, then builds the fix one piece at a time.
Key takeaways
- With hash mod N, going from 4 to 5 servers moves about 80% of keys, because almost every remainder changes.
- On a hash ring, each key goes to the next server clockwise, so adding one server moves only about 1/N of the keys.
- Virtual nodes give each server many points on the ring, which evens out load and spreads a failed server's keys across all the others.
- Replicas go on the next few distinct servers clockwise, so a key's copies sit on different machines.
- Rendezvous hashing solves the same problem without a ring and is a good alternative for small server lists.
The problem with hash mod N
Let's make the opening example concrete. A hash function turns any key, like user:42, into a large number. The same key always gives the same number, and different keys spread out evenly. To pick a server, you take that number mod N, the remainder after dividing by the number of servers.
server = hash(key) mod 4hash("user:42") = 1,000,006 -> 1,000,006 mod 4 = 2 -> server 2This works well while N stays fixed. Now add a fifth server:
1,000,006 mod 5 = 1 -> server 1The key moved. And it is not just this one. A key stays on the same server only if its remainder is the same when dividing by 4 and by 5. Run through the numbers 0 to 19 and you find that only 4 of the 20 keep their server: 0, 1, 2 and 3. That is 20% staying and 80% moving.
It gets worse as you grow. Going from N to N + 1 servers moves about N out of every N + 1 keys. From 10 servers to 11, about 91% of keys move.
For a cache, a moved key is a miss. If the cache was absorbing 97% of 40,000 reads a second, the database was seeing 1,200. Right after the change, it sees most of the 40,000. That is often enough to take it down. For a database that stores data on those servers, it is worse: 80% of your data has to be copied across the network.
The hash ring
So how do we fix this? The idea is to stop tying keys to the count of servers, and tie them to the position of servers instead.
Picture a clock face whose numbers run from 0 up to the largest hash value, then wrap back to 0. This circle is the hash ring.
- 1.Place each server on the ring by hashing its name:
hash("cache-a")gives a point on the circle. - 2.Place each key on the ring with the same hash function.
- 3.Walk clockwise from the key's point. The first server you meet owns that key.
Now add a fifth server. It lands at one point on the ring, between two existing servers. Only the keys in the arc just before it, the keys that used to walk on to the next server, now stop at the new one. Every other key still meets the same server it always did.
On average the new server takes about 1/5 of the keys, all from one neighbour. Compare that with 80% under mod N. Removing a server works the same way in reverse: its keys move to the next server clockwise, and nothing else changes.
In an interview, say it in one line: "With consistent hashing, adding or removing a server moves about 1/N of the keys instead of nearly all of them."
Find the owner on the ring
The app asks for user:42. The cache client (a small library in the app, or a proxy) hashes the key, walks clockwise on the ring and finds that cache B owns it.
Virtual nodes: evening out the load
The simple ring has a weakness. With only four points on a big circle, the gaps between them are random. One server might end up owning 40% of the ring and another only 10%. The first does four times the work of the second.
Failure makes it worse. When a server dies, its whole arc goes to one neighbour. That neighbour suddenly takes double load, and might fall over too, passing an even bigger arc to the next server. This chain reaction is a real risk.
The fix is to give each server many points instead of one. Hash cache-a#1, cache-a#2, up to cache-a#200, and place all 200 on the ring. Each point is a virtual node. The physical server owns every arc that ends at any of its virtual nodes.
Here's why this works. Each server now owns hundreds of small, randomly placed arcs. Some are big and some are small, but the totals average out, so each server ends up close to an equal share. And when a server fails, its hundreds of arcs go to hundreds of different neighbours, so the extra load is shared by everyone rather than dumped on one machine.
Virtual nodes also help with uneven hardware. Give a server with twice the memory twice as many virtual nodes, and it takes about twice the keys.
The cost is a bigger ring to store and search: 50 servers with 200 virtual nodes each is 10,000 points. That is still tiny. Keep them in a sorted list and use a binary search to find the next point clockwise, which takes about 14 comparisons.
| Approach | Keys moved when adding 1 of 5 servers | Load balance | When a server fails |
|---|---|---|---|
| hash mod N | About 80% | Even | Almost every key remaps |
| Ring, one point per server | About 20%, from one neighbour | Can be very uneven | All its keys land on one neighbour |
| Ring with virtual nodes | About 20%, from many servers | Close to even | Its keys spread across all servers |
Replication along the ring
A cache can afford to lose a key; the database still has it. A key-value store cannot. It needs each key stored on several machines.
The ring gives a simple rule for that. Keep 3 copies of each key: put the first on the owner (the first server clockwise), then keep walking clockwise and put copies on the next two servers.
With virtual nodes there is one catch. The next two points clockwise might be two virtual nodes of the same physical server, so all three copies would sit on one machine. The rule is therefore: walk clockwise and skip any point whose physical server already holds a copy, until you have 3 distinct machines. Stores that span racks or data centres apply the same idea to spread copies across those too.
When a server fails, the next servers on the ring already hold copies of its keys, so reads keep working. When it is replaced, it only needs to copy the keys for its own arcs from its neighbours. Amazon's 2007 Dynamo paper describes this approach, and Dynamo-style stores such as Apache Cassandra use a ring with virtual nodes in a similar way.
This also connects to the trade-offs in the CAP theorem guide: how many of those 3 copies must answer before a read or write counts decides how consistent and how available the store is.
Where consistent hashing is used
You will meet the same idea in a few kinds of system. Described generically:
- Distributed caches. The cache client hashes each key to pick a cache server. Adding a server during a traffic spike costs a small, short dip in hit rate rather than a near-empty cache.
- Key-value and wide-column stores. The ring decides which machines own each key and its replicas, and lets you add machines with only a small share of data moving. See database sharding for how this fits the bigger picture.
- Load balancers with sticky routing. Hashing a user or session ID onto a ring sends the same user to the same backend, which helps when backends keep local state such as a warm cache. When a backend is added or removed, most users stay where they were.
- Sharded queues and stream partitions sometimes use it to assign partitions to consumers so a rebalance moves as few as possible.
It is not always the right tool. If your number of shards never changes, plain mod N is simpler. And if you can pre-split data into many fixed logical shards, a lookup table from shard to machine often works just as well and is easier to inspect.
An alternative: rendezvous hashing
There is another way to get the same "only move a few keys" property, with no ring at all. It is called rendezvous hashing, or highest random weight hashing.
For each key, score every server by hashing the key and the server name together, then pick the server with the highest score:
owner(key) = server with the largest hash(key + server)Add a server, and a key moves only if the new server beats its current top score, which happens for about 1/N of keys. Remove a server, and only its keys move, each to whichever server scored second for it. Load spreads evenly without any virtual nodes, and replicas are simply the top 3 scores.
The cost is work per lookup: you score every server, so 1,000 servers means 1,000 hashes per key. For tens of servers that is nothing, which is why rendezvous hashing is popular for smaller clusters. For very large clusters, the ring's binary search is cheaper.
Mention it in an interview as a one-line alternative. It shows you know the goal (move few keys, balance load) matters more than one particular technique.
Questions people ask
What is consistent hashing in simple terms?
It is a way of assigning keys to servers so that adding or removing a server moves only a small share of the keys. Servers and keys are placed on a circle by their hash, and each key belongs to the next server clockwise. Adding one server to N moves about 1/N of the keys instead of nearly all of them.
Why not just use hash mod N?
Because changing N changes almost every remainder. Going from 4 to 5 servers moves about 80% of keys, which empties a cache or forces most of a database to be copied. Mod N is fine only if the number of servers never changes.
What are virtual nodes in consistent hashing?
Each physical server is placed on the ring many times, often a hundred or more, under different names. This evens out how much of the ring each server owns. It also means a failed server's keys spread across many servers instead of all landing on one neighbour.
Where is consistent hashing used?
In distributed caches, in key-value and wide-column stores that spread data and replicas across machines, and in load balancers that want the same user to keep reaching the same backend. Amazon's Dynamo paper and Apache Cassandra are well-documented examples.
What is the difference between consistent hashing and rendezvous hashing?
Both move only about 1/N of keys when a server changes. Consistent hashing uses a ring and a quick search to find the owner. Rendezvous hashing scores every server for each key and picks the highest, which is simpler and balances well but costs one hash per server on every lookup.