The Runtime Theory
mediumSystemInternals#consistent-hashing#sharding#virtual-nodes

Why Use Consistent Hashing for Sharding?

The interview answer to why consistent hashing reduces rebalancing cost, and the follow-up questions about virtual nodes and hotspots.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read

A strong answer

I would start by contrasting consistent hashing with the naive modulo approach, then explain why the difference matters in production.

Naive modulo: Assign shard hash(key) % N to server pool[N]. Adding one server changes N to N+1, and every key's shard assignment changes. You must rehash the entire dataset — a 10 TB database means copying terabytes of data across the network.

Consistent hashing: Place servers on a hash ring. Assign each key to the next server clockwise from its hash position. When a server is added, only the keys in the arc between the new server and its predecessor move. When a server is removed, only its keys move. The movement is bounded to roughly 1/N of the key space, not 100%.

The interviewer usually asks: "But what if the hash distribution is uneven?" That's where virtual nodes come in — each physical server gets many positions on the ring, smoothing out the distribution and ensuring that when one server fails, its load spreads across many others, not just one neighbor.

The shard distribution article covers the full design, including the shard-key choice and the query-cost implications. The load balancer article explains how the same technique applies to routing requests.

Follow-up directions

  • Compare consistent hashing with rendezvous hashing, which avoids the ring entirely and supports weighted distribution.
  • Discuss what happens when a virtual node fails — the remaining nodes must absorb its load, and if the load was uneven, some nodes may exceed capacity.
  • Explain how replication interacts with the shard map — the replica placement must avoid putting all replicas in the same failure domain.

This answer walks

Practice follow-ups

  1. 01What happens if a hot shard gets all the traffic — how do you split it?
  2. 02How does replication interact with consistent hashing — do you store replicas on adjacent nodes?
  3. 03What is a virtual node, and why is it important for load balance?

More interviews in this topic

One dispatch a week

The trace behind each question, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save