Module 1: Core Concepts
Consistent Hashing
Consistent hashing for system design interviews: hash rings, virtual nodes, and why a node join moves only nearby keys.
When you shard data or cache across N nodes, naive hash(key) % N reshuffles almost everything when N changes. Consistent hashing remaps only a small fraction — that is why it shows up in caches, partitioned stores, and request routing.
What you will learn
- Contrast modulo hashing with consistent hashing
- Explain the hash ring and how a key finds its owner
- Describe what happens when nodes join or leave
- Explain virtual nodes for load balance
- Add replication on the ring and call out hot-key limits
Why modulo hashing hurts
- A common first idea is node = hash(key) % N. It is simple — and painful when membership changes.
- When N becomes N+1, almost every key maps to a different node
- Caches miss en masse; databases reshuffle nearly the whole dataset
- Scaling events turn into thundering herds against origin or disk
- You need a mapping that moves only the keys that must move
The hash ring idea
- Consistent hashing places both nodes and keys on a circular hash space (a ring). Ownership is “walk the ring,” not “mod N.”
- Hash each node to one or more positions on the ring
- Hash each key to a position on the same ring
- A key is owned by the first node clockwise (or a fixed direction) from its hash
- The circle wraps: past the last point, ownership continues from the start
Lookup walkthrough
- Use this 20-second story when asked how a request finds its shard.
- Compute h = hash(key) into the ring space
- Find the smallest node position ≥ h (binary search / tree on sorted node hashes)
- If none exists, wrap to the first node on the ring
- Send the request to that physical node (after resolving virtual → physical)
What happens on add / remove
- Membership changes only disturb keys that live in the affected arc — not the whole keyspace.
- Add a node — it takes keys from its successor’s arc only (~1/N of keys on average)
- Remove a node — its keys move to the next node clockwise
- Unaffected arcs keep serving without migration
- Still plan a controlled handoff: copy data, warm caches, then cut traffic
Virtual nodes (vnodes)
- If each physical machine has only one ring point, random clumping creates hot and cold arcs. Virtual nodes fix that.
- Each physical node owns many virtual positions on the ring
- Load spreads more evenly across machines
- Heterogeneous capacity: stronger hosts can claim more vnodes
- Used widely in distributed caches and several partitioned data stores
Replication on the ring
- For durability or read capacity, store a key on more than one node along the ring.
- Common pattern: primary = first clockwise node; replicas = next R−1 distinct physical nodes
- Skip duplicate physical hosts when walking vnodes so replicas land on different machines
- Reads can prefer nearest replica; writes may require a quorum of replicas
- Helps survive node loss without waiting for a full reshuffle
Hot keys and skew
- Consistent hashing balances key ownership, not request popularity. One viral key can still melt a node.
- A single celebrity user_id or product_id can dominate one shard
- Mitigations: local cache, request coalescing, key salting/splitting, read replicas
- Bounded loads / “power of two choices” variants appear in advanced designs
- Call this out: hashing ≠ automatic hotspot immunity
Where it is used
- Anywhere you map key → node and membership changes over time.
- Distributed caches (partitioned Redis/Memcached-style clusters)
- Database sharding and some ring-based stores
- Request routing / sticky-ish distribution without sticky sessions
- CDN or edge assignment problems in some architectures
Design choices to mention
- Interviewers like one layer of engineering judgment beyond the ring cartoon.
- Hash function — cryptographic hashes are fine; focus on uniform spread
- Vnode count — more vnodes → smoother balance, more metadata to store
- Replication factor R — durability vs write cost and capacity
- Migration strategy — background copy + dual-write/read fence during moves
- Failure detection — remove dead nodes from the ring quickly to avoid timeouts
Common mistakes
- These answers sound close but miss the point.
- Claiming consistent hashing remaps zero keys on scale-out
- Forgetting virtual nodes when asked about imbalance
- Putting all replicas on vnodes of the same physical host
- Treating it as a CAP solution — it is a placement algorithm, not a consistency model
- Ignoring hot keys after drawing a perfect ring
Interview framing
- Use this order when sharding or cache partitioning comes up.
- State the problem: hash % N reshuffles too much on membership change
- Draw the ring; place nodes and one key; walk to the owner
- Explain add/remove moves only an arc (~1/N)
- Add vnodes for balance; add R successors for replication
- Call out hot-key mitigations and migration care
Quick revision checklist
- Contrast explicitly with hash(key) % N.
- Draw the ring and walk clockwise once.
- Say “~1/N keys move” on node add.
- Mention virtual nodes for even load.
- Mention replication via next R physical nodes on the ring.
- Call out hot keys as an orthogonal problem.
FAQs
Is consistent hashing only for caching?
No. It appears in partitioned databases, load distribution, and any key→node mapping that must survive membership changes.
How many keys move when I add a node?
On average about 1/N of the keys when going to N nodes — only the arc that the new node owns. That is the core advantage over modulo hashing.
Why do we need virtual nodes?
A single ring point per host can leave large uneven arcs. Many vnodes per physical machine smooth the load and let stronger hosts take more share.
Does consistent hashing solve hot keys?
No. It balances key ownership, not request popularity. Viral keys still need caching, splitting, or other hotspot tactics.
How do replicas work on the ring?
After choosing the primary (first clockwise node), continue walking to the next distinct physical nodes until you have R copies. Avoid placing two replicas on the same machine via different vnodes.
Is this the same as rendezvous (HRW) hashing?
Both aim for minimal remapping on membership change. Consistent hashing uses a ring; HRW scores key×node pairs and picks the highest. Mention HRW only if asked for alternatives.
Learner’s Task
- Practice out loud for 8–10 minutes with a quick ring sketch on paper or a whiteboard.
- State why hash(key) % N fails when N changes, then introduce the ring in two sentences.
- Draw 4 nodes and 3 keys; show which node owns each key by walking clockwise.
- Add a 5th node and mark which keys move; estimate the fraction that should move.
- Add virtual nodes (say 3 vnodes per host) and explain how that improves balance.
- Write a 4-sentence closing: problem, ring rule, vnode benefit, and one hot-key caveat.