Understand the building blocks / CHAPTER 15
Consistent hashing
Keep key placement more stable when nodes change while treating balance and durability as separate concerns.
Builds on Data partitioning, Load balancing algorithms.
- Place keys and node tokens on a hash ring.
- Explain limited movement during membership changes.
- Recognize virtual-node, hot-key, and failure-domain limitations.
01Stabilize placement when membership changes
A cache stores each object on one machine. To choose a machine, it runs a repeatable calculation on the object key, called a hash. A simple rule divides the result by three and uses the remainder to select one of three machines: hash(key) mod 3.
Adding a fourth machine changes the divisor to four. Many keys now choose a different machine. That causes cold misses in a cache and requires actual record movement in retained storage. Consistent hashing aims to keep most assignments stable when machines join or leave.
Build a small ring calculation:
- Use positions 0 through 99 arranged in a circle.
- Assign machine A position 20, B position 50, and C position 80. These assigned positions are tokens; abbreviate them as
A20,B50, andC80. - Hash a data key into the same position range.
- Move clockwise from the key’s position to the first machine token. That machine owns the key.
- If no token remains before 99, wrap around to the start. A key exactly on a token belongs to that token in this example.
Apply the rule:
- Key
12reachesA20first, so A owns it. - Key
44reachesB50first, so B owns it. - Key
90wraps around and reachesA20first.
The ring describes a placement calculation. It is not physical network wiring and does not make machines send requests clockwise.
02Add a node and identify the exact movement
Now assign a new machine D position 35. Work from the key to its old and new destination:
- Before D joins, key
28reachesB50first. - After D joins, key
28reachesD35beforeB50. - Every key greater than 20 and up to 35 changes in the same way. Write this interval as
(20,35]: exclude 20, include 35. - B previously held those records, so B must transfer them to D. A’s token at 20 marks the lower boundary; it does not mean A stored the transferred interval.
| Key | Before D35 |
After D35 |
Reason |
|---|---|---|---|
| 12 | A20 |
A20 |
A is still the first clockwise token. |
| 28 | B50 |
D35 |
D now appears before B. |
| 44 | B50 |
B50 |
The key remains beyond D and before B. |
| 90 | A20 |
A20 |
Wraparound is unchanged. |
Only the newly assigned interval changes in this simplified update. Its size depends on the actual token positions; there is no fixed movement percentage for every deployment. Removing a token transfers its interval to the next eligible owner.
One token per physical machine can create uneven intervals by chance. Give a machine several positions and it owns several smaller intervals instead. These additional positions are called virtual nodes or virtual tokens.
- More positions provide flexibility for dividing and moving ownership.
- A larger-capacity machine can receive a tested larger share of positions.
- More positions add mapping information and movement work.
- One popular key still chooses one owner under this rule. Extra positions do not automatically spread that key’s requests.
03Separate placement, copies, and commitment
Calculating B-to-D placement does not copy any bytes. A retained store needs a separate transition:
- Copy the affected records from B to D.
- Capture and apply writes that arrive while copying proceeds.
- Pause new writes for the moving interval, finish writes already accepted by B, then verify D has copied those records and applied every accepted change.
- In this illustrative handoff, save D as the new owner and have B forward requests from clients with the old machine list. B must not keep accepting independent writes for the transferred interval; resume writes for the transferred interval at D.
- Retain the saved owner and move progress across a crash. Retire B’s old copy only after D’s records and required replicas are verified. Use the store’s supported movement protocol to enforce this handoff.
Copy placement is another separate decision:
- If several successive tokens belong to the same physical machine, they do not provide independent replicas.
- Choose distinct physical owners and the required zones for retained copies.
- Define when a write succeeds and which reads may use a lagging copy. Knowing its destination does not prove that copies agree on accepted changes.
Rendezvous hashing offers another stable-placement method without a ring:
- Calculate a repeatable score for each pair of data key and eligible machine.
- Choose the machine with the highest score.
- When D joins, a key moves only if its score with D beats its previous winning score.
The score still does not transfer data, establish independent copies, or settle concurrent writes. In an interview, work an addition and a removal with actual numbered keys, then explain movement, uneven demand, and the failure locations of copies.
Adding D changes one key’s successor
Follow the numbered arrows
- Before: D absent → After: D35 addednew token precedes B
- After: D35 added → Actual ownership moveidentify prior owner B
- Clockwise order → After: D35 addedsuccessor rule
- Actual ownership move → Safe transitionmapping is not movement
- Clockwise order → Unchanged examplesother successors remain
Trace the diagram in words
- Start with key 28 and the original A20, B50, C80 tokens: B is first clockwise.
- Insert D35: it now appears before B for key 28.
- B is the former owner of the changed interval, so B sends those records to D.
- Verify copied records and later changes before retiring the old route.
DRAW. CALCULATE. EXPLAIN.
Add and remove a toy-ring owner
20 minutes · guided practice
Use tokens A10, B40, and C70, meaning machines A, B, and C have positions 10, 40, and 70. Keys occupy positions 5, 25, 55, and 95.
- List their initial owners.
- Add
D30and identify which example moves. - From that new ring, remove
C70and identify the affected example. - Explain one requirement for moving retained data safely.
Compare the worked solution
- Initially, 5 reaches
A10, 25 reachesB40, 55 reachesC70, and 95 wraps toA10. - Add
D30. Keys greater than 10 through 30 now reach D before B. Among these examples, only 25 moves, from B to D. - Remove
C70. Its interval greater than 40 through 70 now reachesA10after wraparound. Key55therefore moves from C to A. - Copy retained records and capture writes made during copying. Apply and verify them before retiring the old route, and handle clients still using an older machine list.
- If replicas are needed, choose distinct physical machines and suitable failure locations. Several tokens on one machine are not several independent copies.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01How does the ring choose a key’s owner?
- Hash the data key to a numbered ring position.
- Choose the first machine token clockwise, including an exact match.
- Wrap around at the end.
A token is an assigned position, not another physical machine or an automatic data copy.
CARD 02What does D35 change in A20, B50, C80?
- D at 35 takes keys greater than 20 and at most 35.
- Before D joined, those keys reached B at 50 first.
- B therefore transfers them to D.
A at 20 is the lower boundary, not the former owner of that interval.
CARD 03Which storage guarantees must be designed separately from hashing?
- The placement rule chooses a destination.
- A migration procedure copies records and handles writes during the move.
- Replica placement chooses independent machines and failure locations.
- The storage protocol defines write success and read freshness.
One calculation does not provide all four guarantees.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.