Consistent Hashing
Build the hash ring as code: a sorted list of points, a binary search, virtual nodes for balance, and replication for failure. Know what moves when a node joins and why.
Overview
Consistent hashing answers one question: which node owns this key, in a way that barely changes when nodes come and go. The name comes from a 1997 paper by Karger, Lehman, Leighton, Levine, Lewin and Panigrahy, written to spread web cache load. Today it decides where a key lives in Dynamo-style stores, Cassandra, and most distributed caches.
The low-level interview is not about the idea; it is about the code. You are expected to build the ring from a sorted list and a binary search, explain why a node needs many points on the ring, show what happens when a node dies, and know the two main alternatives. This topic walks through each of those with a tiny ring you can trace by hand.
Why modulo fails
The obvious way to pick a node is hash(key) % n. It spreads keys evenly, but the moment n changes almost every key changes owner, because the remainder of nearly every hash changes. The table uses five keys with small hash values so you can check it.
- k1 — 7 — 1 — 3 — yes
- k2 — 30 — 0 — 2 — yes
- k3 — 50 — 2 — 2 — no
- k4 — 83 — 2 — 3 — yes
- k5 — 99 — 0 — 3 — yes
Four of five keys move when a fourth node joins. In a cache that is a stampede of misses; in a database it is a mass migration.
Interview questions
- Implement consistent hashing. How do virtual nodes help?
- What happens when a node fails in a consistent hashing ring?
- Compare the ring to rendezvous hashing and jump consistent hash.
- How would you make the ring safe to use from many threads, and how do clients agree on it?
- How do you support nodes of different capacity?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio