Consistent Hashing
A placement rule should not move nearly every key whenever membership changes.
What, why, when and how
What is it?
Consistent hashing maps keys to positions on a ring or similar placement structure so membership changes move a limited portion of keys.
Why is it needed?
A simple modulo rule can remap many keys when the node count changes, causing large migrations or cache-miss bursts.
When should I use it?
Use it for distributed cache placement or partition ownership when nodes join and leave and limited movement matters.
When should I choose something else?
It does not solve replication, strong consistency or a single hot key. Fixed partition maps can be simpler when controlled migrations and range queries dominate.
How do I use it?
Hash the key, locate its owning token, use virtual nodes for distribution, and separately define replica placement, membership versions and data transfer.
Can you show a concrete example?
With tokens A20, B60 and C90, key45 belongs to B. Adding D50 moves key45 to D while key55 stays on B. Only the newly owned interval moves; replica and failure rules remain separate decisions.
Read the diagram
Tokens A20, B60, C90: key45 belongs to B; Add token D50: interval (20,50] moves; key45 moves to D; key55 stays on B
Follow the detailed diagrams below to trace the additional state changes and failure cases behind these answers.
A placement rule should not move nearly every key whenever membership changes. With hash(key) modulo node_count, changing node_count changes many assignments. Consistent hashing places both keys and node tokens on a shared circular space and gives each key an owner according to an ordered rule. Its benefit is limited reassignment, not automatic fairness or replication correctness.
Learning goals
Membership changes; hash ring ownership; virtual nodes; key movement; replication placement; hot keys; ring metadata; failure detection.
Calculate placement and movement
Take a ring numbered 0 through 99 with tokens A=20, B=60 and C=90. Ownership is the first clockwise token, including wraparound. Keys 15,45,55,85 map to A,B,B,C. Key 95 wraps to A. Adding D=50 moves keys in (20,50] from B to D: 45 changes, 55 stays B. The exact fraction moved depends on actual token gaps and data distribution; “one quarter” is an average intuition for balanced random placement, not this ring's guarantee.
Compare a deliberately separate modulo exercise: integer hashes 1,2,3,4 modulo 3 give owners 1,2,0,1. Modulo 4 gives 1,2,3,0, changing 3 and 4. A membership change can alter many assignments without identifying one stable interval to move. The lesson is reduced remapping, not zero movement.
key 15 45 55 85 95
before A B B C A
after A D B C ARead the diagram
First clockwise token owns the key. A20 B60 C90; add D50. Key45 moves B to D; keys15,55,85,95 stay A,B,C,A. Placement is separate from replica and content versions.
Virtual nodes give one physical host multiple tokens and can approximate capacity weights. When selecting replicas, skip tokens on the same physical host and spread failure domains. Ten tokens on one machine are not ten independent copies. One very popular key still maps to a small owner set; replicated reads or a separate application strategy addresses that pressure.
Moving cache data can tolerate misses only if the origin path is bounded. Moving authoritative data needs copy/catch-up, ownership fencing and reconciliation. Publish a versioned ring, decide how clients refresh stale views and define routing during transfer. Dual routing must not let two owners independently commit conflicting business writes.
Show answer and explanation
Answer: In the pictured convention D takes the interval before its token, not the interval after it. Key 95 still wraps to A. The drawing must use the same direction and inclusivity as the owner table.
Step-by-step reasoning
1. Walk the ring
Hash a key to a position and walk clockwise to the first node token. That token owns the key under this convention. The last interval wraps around to the start. When a new token is inserted, it takes the interval immediately before it from the previous owner of that interval; unrelated intervals keep their owners.
2. Use virtual nodes for placement balance
One physical machine can own many tokens. This spreads a machine’s responsibility across the ring and helps smooth uneven token spacing. Weight token counts or placement for heterogeneous capacity if needed. Virtual nodes distribute many keys; they cannot split the traffic for one intensely popular key by themselves.
3. Place replicas across failure domains
Keeping additional clockwise copies is a possible replication rule, but skip tokens belonging to the same physical machine and consider rack or zone diversity. Ownership and replication are separate decisions. Define which copy accepts writes, what acknowledgments mean, and how an unavailable node is replaced or repaired.
4. Coordinate membership and movement
Clients need a coherent ring view or a router that owns it. Membership changes should carry a version, and data movement should be monitored. A returning node may hold stale data. For a cache, misses during movement may be acceptable if origin load is bounded; for authoritative storage, transfer and reconciliation need stronger guarantees.
Contracts and state
The following sketch makes the decision boundary concrete. Field names and capacity assumptions are illustrative; adapt them to the stated product contract.
Ring positions: A=20, B=60, C=90
key 45 -> B (first clockwise token)
Add D=50
key 45 -> D; key 55 -> B
Moved interval: (20, 50]
The diagram uses a linearized ring; right edge wraps to left.Worked example
Suppose a cache has three nodes and a fourth is added. With a ring, only the new node’s acquired intervals need to warm on that node. Other owners retain their cached keys. However, the most popular homepage might lie in one acquired interval. Warm-up can still cause a large origin spike even though the percentage of moved keys is small. Measure moved request volume as well as moved key count.
Read the diagram
The ring has tokens A20, B60, and C90. Key hash 45 belongs to B60. Adding D50 moves only interval (20,50] to D, including key 45; other key ranges keep their owner. Virtual nodes improve placement balance but do not split one hot key.
Failure walkthrough
Two clients disagree about membership and send the same key to different cache nodes. This can temporarily lower hit rate or return different cached versions. A versioned membership source, controlled rollout, and bounded TTL help. Do not call the ring a consensus protocol: it computes placement from membership but does not establish agreement about membership or the value of a key.
Read the diagram
Publish new ring version → Move only acquired ranges → Warm popular moved keys → Bound origin miss traffic → Retire old routing version
Decisions and trade-offs
| Concern | Mechanism | Does not solve |
|---|---|---|
| Membership churn | Consistent placement | Agreement on membership |
| Uneven key counts | Virtual nodes | Single hot-key traffic |
| Node loss | Replica placement | Freshness without a replication protocol |
Check your understanding
With tokens A=20, B=60, C=90, where do keys 10, 45, and 95 go? What changes when D=50 joins?
Show answer and explanation
Answer: Initially 10 goes to A, 45 to B, and 95 wraps to A. After D joins, 45 goes to D. The other two remain with A. Only the interval (20,50] changes owner under the stated clockwise rule.
Transfer to a new scenario
Add a cache node and identify only the ranges whose owner changes.
Do virtual nodes solve a single extremely popular key?
Continue the connection
Study Sharding and explain which guarantee from this lesson carries into that topic.
Separate placement from replication
Place server tokens around a hash ring and assign a key to the first token clockwise. Adding one token changes ownership of a neighboring interval rather than remapping every key as hash(key) modulo nodeCount can. With many virtual tokens, physical nodes receive several intervals, reducing placement imbalance across many keys.
Replication is a separate rule: for example, choose the next distinct physical owners while respecting failure domains. Two adjacent virtual tokens can belong to the same physical machine, so counting tokens as independent replicas is unsafe. Membership changes need versioned routing and actual data movement or cache refill.
For N equally weighted nodes and uniform keys, adding a node moves roughly 1/(N+1) of keys in expectation. This is not a guarantee about bytes or traffic. A single hot key still maps to one primary placement and may need read replication or a redesigned value.
Read the diagram
Concern / Mechanism / Does not solve; Membership churn / Consistent placement / Agreement on membership; Uneven key counts / Virtual nodes / Single hot-key traffic; Node loss / Replica placement / Freshness without a replication protocol
Operational sketch
ring tokens: A20, B60, C90
key 45 -> B60
add D50: key 45 -> D50
interval (20,50] moves; other intervals retain ownersA tempting mistake
Consistent hashing does not choose a leader, detect failures reliably or make a stale replica safe to promote. It is a placement technique inside a larger protocol.
Transfer exercise
If one key receives half the traffic, will adding many virtual nodes solve the overload?
Show answer and explanation
Answer: No. Virtual nodes balance many keys. Replicate reads, split the logical object when semantics permit, or serialize and limit the hot-key operation.
With tokens A=20, B=60, C=90, where do keys 10, 45, and 95 go? What changes when D=50 joins?
Your design draft
Clarify assumptions, explain your approach, and test the difficult cases. Save your draft, then compare it with the study notes.
Self-review checklist
Self-guided practice. Automated AI feedback and code execution are not connected.