Question presented to candidate:
"We spread cache keys over servers with hash(key) % N. Every time we add or lose a server, the hit rate collapses. What is consistent hashing, how does it fix this, and what problem does it still have?"
What a strong answer should cover:
Why hash % N breaks: changing N changes the remainder for most keys, about N/(N+1) of them when one server is added
The hash ring: servers and keys are hashed onto the same circular space; a key belongs to the first server clockwise
Adding or removing a server moves only the keys on one arc, on average 1/N of them, and they move only to or from that server
Lookup is a binary search over sorted server positions, O(log n)
The remaining problem: with one position per server, arcs are very uneven; the standard fix is virtual nodes
Where it is used: distributed caches, Dynamo-style stores (Dynamo, Cassandra), and load balancers that need sticky placement
Code / implementation expected: Optional โ a ring with a sorted array and binary search is a common whiteboard ask; the example measures movement against hash % N.
consistent-hashing
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Beginners to system design who know what a hash function and a cache are; no distributed-systems background assumed.
Difficulty: Medium
Verified with: Node 22.22, Python 3.12, Go 1.24 and Java 21 (all four print identical output). History is cited from Karger et al., STOC 1997; system usage from the Dynamo paper (SOSP 2007) and the Apache Cassandra documentation.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. Numbers come from running the example; the Verified section quotes its full output.
1. Why This Even Matters โ A Story First
A school puts students into lockers by a simple rule: take the student number, divide by the number of locker rooms, and the remainder is your room. With 4 rooms, student 13 goes to room 1. It works perf
The solution is hidden so you can try first
11 min read ยท 12 sections
4
Read the code
A consistent hashing ring vs hash % N: keys moved when a node joins or leaves