Quick revision
The short answer
With hash(key) % n, changing the number of nodes from n to n + 1 moves almost every key to a different node, which empties caches all at once or forces a huge data migration. Consistent hashing places both nodes and keys on a ring and assigns each key to the next node clockwise, so adding or removing a node moves only about 1/n of the keys. Virtual nodes give each server many points on the ring, which evens out the load and lets us weight servers by capacity.
What to remember
In this answer, we saw that hash(key) % n moves almost every key when n changes, which causes cache miss storms and large migrations. Consistent hashing puts nodes and keys on a ring and assigns each key to the next node clockwise, so adding or removing a node moves only about 1/n of the keys. Virtual nodes give each server many ring points, evening out the load, spreading a failed node's keys across the cluster, and allowing weights by capacity. Rendezvous hashing achieves the same with simpler code at O(n) per lookup, and Redis Cluster uses 16,384 fixed hash slots with an explicit slot-to-node map.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With hash(key) % n, changing the number of nodes from n to n + 1 moves almost every key to a different node, which empties caches all at once or forces a huge data migration. Consistent hashing places both nodes and keys on a ring and assigns each key to the next node clockwise, so adding or removing a node moves only about 1/n of the keys. Virtual nodes give each server many points on the ring, which evens out the load and lets us weight servers by capacity.
Why this question is asked
Every distributed cache, sharded database and partitioned queue has to answer the same question: which node owns this key? The answer has to be fast, the same on every client, and stable when the cluster changes, because clusters change all the time through scaling, failures and deployments. Interviewers ask about consistent hashing because it is the standard answer to the stability part, and because the details, virtual nodes and the alternatives, show whether we understand why it works. In this answer, we will see what goes wrong with modulo, build the hash ring, add virtual nodes, look at rendezvous hashing, and compare with Redis Cluster's fixed hash slots.
1Why hash(key) % n Breaks When n Changes
The simplest placement rule is node = hash(key) % n. It spreads keys evenly and needs no state. The problem is what happens when n changes. A key stays on the same node only if hash % n equals hash % (n + 1), and for random hashes that is true for only about 1 key in n + 1. Going from 4 to 5 nodes therefore moves about 80 percent of the keys.
For a cache, that means a miss storm: almost every request suddenly misses, the database behind the cache receives the full read load, and it may fall over exactly when we were trying to add capacity. For a sharded database, it means moving most of the data between machines. Both make scaling risky, which is the opposite of what we want.
2Building the Hash Ring
Consistent hashing changes the rule. We imagine the whole hash space, say all 32-bit values, bent into a circle. Each node is hashed, usually by its name or address, to a point on the circle. Each key is hashed to a point too, and it belongs to the first node found by walking clockwise from the key's point.
Now consider adding a node E. It lands at one point on the ring and takes over only the keys between its predecessor and itself, which used to belong to its successor. Every other key keeps its owner. With n nodes evenly spread, E takes about 1/(n + 1) of the keys, so adding a fifth node moves about 20 percent instead of 80 percent. Removing a node hands only its own keys to its successor. In Java, a TreeMap from ring position to node implements the clockwise lookup in O(log n) with ceilingEntry, wrapping to firstEntry at the end of the ring.
| Placement rule | Keys that change node | Effect |
|---|---|---|
| hash % n, 4 -> 5 nodes | about 80 percent | cache miss storm, mass migration |
| Ring, one point per node | about 20 percent on average | uneven: depends on where E lands |
| Ring with 100+ virtual nodes | about 20 percent, spread out | moved keys come evenly from all nodes |
Notice that the hash function must be identical on every client, and that String.hashCode is a poor choice for ring positions because similar names produce clustered values; production code uses a well-mixed hash such as Murmur3 or xxHash.
3How Virtual Nodes Help
With one point per node, the ring is lumpy. Four random points rarely divide a circle into four equal arcs; one node might own 40 percent of the ring and another 10 percent. And when a node fails, all its keys move to a single successor, which can double that node's load at the worst moment.
Virtual nodes fix both problems. Each physical server is placed at many points, for example 100 to 200, by hashing names like nodeA#0, nodeA#1 and so on, which is what the add method above does. Many small arcs average out, so each server owns close to its fair share. When a server leaves, its many small arcs are inherited by many different successors, so the extra load spreads across the whole cluster. Virtual nodes also give a simple way to weight servers by capacity: a machine with twice the memory gets twice as many virtual nodes and therefore about twice the keys. Apache Cassandra used this approach with its num_tokens setting; check your version for the current default.
4An Alternative: Rendezvous Hashing
Rendezvous hashing, also called highest random weight hashing, avoids the ring entirely. For each key, we compute a score for every node, hash(key, node), and pick the node with the highest score. When a node is added, it wins only the keys for which its score is the new highest, about 1/(n + 1) of them, and when a node is removed, only its keys move, each to its own second-best node, so the load spreads naturally without virtual nodes.
The code is very short and the balance is good. The cost is O(n) work per lookup, which is fine for tens of nodes and becomes noticeable with thousands, where a ring with O(log n) lookup is cheaper.
5Fixed Hash Slots in Redis Cluster
Redis Cluster takes a third approach. The key space is divided into 16,384 fixed hash slots, and a key's slot is CRC16(key) mod 16384. Modulo is fine here because the number of slots never changes. What changes is the mapping from slots to nodes, which the cluster stores and gossips. To add a node, we move a chosen set of slots to it, and only keys in those slots migrate. Clients cache the slot map and follow MOVED redirects when it changes.
This has two practical advantages over a ring. Rebalancing is explicit and controllable, because an operator or a tool decides exactly which slots move, and the slot map is small and easy to reason about. Hash tags such as {user42}:cart let related keys share a slot, which multi-key commands need. Kafka partitions and many sharded databases use the same fixed-partitions idea.
6Follow-up questions
Does consistent hashing guarantee a perfectly even load?
No. It guarantees minimal movement, and virtual nodes make the key count close to even. But real load also depends on how hot individual keys are; one celebrity key can overload its node no matter how the ring is drawn. Hot keys need replication or a local cache in front.
How does replication fit onto the ring?
A common scheme stores each key on the owner and the next distinct physical nodes clockwise, for example three replicas. With virtual nodes we must skip points that belong to a server we have already chosen, so the replicas land on different machines.
Why not just rehash everything during a quiet period?
For a small cache it can be acceptable, but large systems rarely have a quiet period, and the cluster changes for unplanned reasons such as a node crash. A scheme that moves only the necessary keys makes every change, planned or not, cheap.
7Conclusion
In this answer, we saw that hash(key) % n moves almost every key when n changes, which causes cache miss storms and large migrations. Consistent hashing puts nodes and keys on a ring and assigns each key to the next node clockwise, so adding or removing a node moves only about 1/n of the keys. Virtual nodes give each server many ring points, evening out the load, spreading a failed node's keys across the cluster, and allowing weights by capacity. Rendezvous hashing achieves the same with simpler code at O(n) per lookup, and Redis Cluster uses 16,384 fixed hash slots with an explicit slot-to-node map.
Open with
The problem with hash of key mod n is that the result depends on n, so when I add or remove a node almost every key changes owner, about 80 percent going from four to five nodes.
Then, if they want more
For a cache that is a miss storm that can take down the database, and for a sharded store it is a massive migration. Consistent hashing fixes that by hashing both nodes and keys onto a ring and giving each key to the first node clockwise.
Adding a node only takes the keys between it and its predecessor, about one over n of them, and removing a node only moves its own keys. In Java I would implement it with a TreeMap of ring positions and ceilingEntry, wrapping to the first entry.
With one point per node the arcs are uneven and a failed node dumps its whole load on one neighbour, so we use virtual nodes: each server gets a hundred or more points, which evens out ownership, spreads a failure's keys across many servers, and lets me give bigger machines more points.
An alternative is rendezvous hashing, where each key goes to the node with the highest hash of key and node, which is simple but O(n) per lookup. And Redis Cluster uses 16,384 fixed slots with a slot map, so rebalancing means moving chosen slots, which is easier to control.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain consistent hashing and why a cache or sharding layer would use it instead of...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/0045c24dbd/