Explain consistent hashing and virtual nodes
Company tags are compiled from public interview reports and are signals, not guarantees.
What interviewers are testing
This question separates candidates who memorized "add virtual nodes" from those who can do the distribution math: why modulo hashing remaps 1 minus 1/n of keys, why the ring remaps only about K/n, and how vnodes turn a few arbitrary arcs into a smooth distribution. The follow-ups about failures and rebalancing test whether the model survives contact with operations.
Mental model
Map servers and keys onto the same circular hash space, then assign each key to the first server clockwise. When membership changes, only the arcs that changed hands move — about K/n keys instead of K times (1 minus 1/n) for modulo. Virtual nodes multiply each server's presence on the ring so small clusters average out.
Step-by-step solution
Step 1: Place keys on the ring
Consistent hashing places both nodes and keys on a circular hash space, usually 0 to 2^32. Each server is hashed to a position on the ring, and each key is assigned to the first node clockwise from the key's own hash. No central directory is needed: any participant can compute ownership from the ring and the same hash function. The catch is variance. Three physical nodes land at arbitrary points, so the arcs between them are wildly uneven — one node can own 52 percent of the keyspace while another owns 17 percent. Small clusters are the worst case because there are too few sample points for the distribution to average out. Watch the animation as server names and keys are projected onto the ring and each key walks clockwise to its owner. That imbalance is exactly what virtual nodes fix later; first let the simple ring expose the problem.
Step 2: Adding a node remaps keys
Now add a fourth node and count what moves. With naive modulo hashing, key k lives at hash(k) mod N. Changing N from 3 to 4 changes the modulus for every key, so a key survives only if hash(k) mod 3 equals hash(k) mod 4 — on average only one in N+1 of them survives. Moving from N to N+1 nodes remaps N/(N+1) of the keys — equivalently 1 − 1/N′, where N′ is the new node count; here that is 75 percent — and almost every cache entry becomes a miss or every partition is rewritten at once. The ring behaves differently. Adding node D only affects keys whose position lies between D's predecessor and D, because only those keys find a new first-node-clockwise. That is D's arc, on average 1/(N+1) of the keyspace — one quarter, here. Watch the animation contrast the two: the modulo panel drains almost all keys, while the ring moves a single arc. The captions show the remapped fraction — 75 percent versus 25 percent — which is the difference between a full migration and a bounded one.
Step 3: Redistribute only the new arc
The ring tells you which keys move but not how to move them safely. Take the arc between C and D: those keys currently resolve to the next node clockwise after D, which is A. A scans its store for keys in that range and streams them to D, so the transfer is bounded by one arc of data instead of the whole dataset. During the move the system has a choice: dual-write to both A and D and read from A until the backfill completes, or accept a short read-only window. When D has caught up and the backfill is verified, the routing table flips and D serves the arc; A deletes its stale copies lazily, and the extra writes stop. Watch the animation as exactly one arc of keys streams to the new node while every other arc stays put. Movement proportional to the membership change, not to the dataset size, is the entire point of consistent hashing.
Step 4: Failure walks keys clockwise
When a node dies, the ring does not stall: the keys it owned simply fall to the next node clockwise, because ownership is recomputed locally on every client. That is the availability win over a central directory. The failure mode is a hotspot: the successor now serves its own arc plus the dead node's arc, and without replicas it takes double the traffic at the worst possible moment. The standard mitigation is replication — store each key on the next R nodes clockwise, typically three — plus load-aware rerouting while the cluster repairs. With replication in place, reads for the dead node fail over to the next replica immediately and the redistribution becomes background repair rather than an emergency. Watch the animation as Node B dies, its arc walks to C, the load doubles briefly, and the replicas on A and C absorb reads; the background repair then rebuilds the missing copies and the doubled load fades.
Step 5: Virtual nodes smooth the ring
Virtual nodes are the fix for both hotspots and churn. Instead of one point per server, each physical node is hashed to 100 to 200 points, its virtual nodes, spread around the ring. A key still walks clockwise to the first point, but now the small arcs average out: the node that would have owned a 52 percent super-arc owns many small arcs that sum to roughly 1/N of the keyspace. With 150 vnodes per node, the relative standard deviation of load shrinks to roughly 1/√150 ≈ 8.2 percent — across a three-node cluster that is an absolute spread of about 2.7 percentage points — so it lands near 33/33/33. Vnodes also soften joins and failures: adding one server inserts 150 small arcs instead of one giant one, so transferred keys spread across many nodes in small pieces, and a dead node's load is absorbed by many successors. Watch the final animation as the bold arcs dissolve into hundreds of tiny arcs converging on 36/33/31.
Edge cases & traps
- Too few virtual nodes leave visible imbalance: use 100-200 vnodes per physical node and review real key counts, not just ring theory.
- Poor vnode hashing collides or clumps points: use a strong hash such as 128-bit MurmurHash or xxHash and deduplicate positions.
- Hot keys defeat any hashing scheme because one key cannot be split: cache them, shard them with a random suffix, or replicate them separately.
- Rebalancing without replication risks data loss when a node dies mid-transfer: keep R copies on the next clockwise nodes and repair asynchronously.
- Clients with stale ring views briefly route to the wrong node: version the ring through gossip or a config service and retry lookups on miss.