CONSISTENT HASHING

Add a node and only about K/(N+1) keys move

L216 MINALGORITHMS
PREDICT

2000 keys over 4 servers with hash(key) mod N. You add a fifth server. About how many keys now map to a different server?

Consistent hashing puts nodes and keys on the same ring of hash values. A key belongs to the first node token clockwise from its hash. When a node joins, it takes only the keys between its tokens and their predecessors, about K/(N+1) of them. Every other key stays where it is.

With one token per node the arcs between tokens are uneven, so one node can own half the ring. Real systems give each node many virtual nodes (tokens). Each node’s share becomes a sum of many small arcs, and the load evens out.

▶ SIMULATION
NODES (ring, 1 token per node)
none placed
empty
LOAD
average: — keys per node
max / average: —
NODE E JOINS
keys moved — of 2000
moved to E — · between old nodes —
ideal: K/N = 2000/5 = 400
✓ max node load ≤ 1.5 × average✓ adding a node moves ≤ 2 × K/N keys
SCHEDULE
01[CLUSTER]configure: owner = hash(key) mod N
02[CLUSTER]place nodes A, B, C, D
03[CLIENT]assign 2000 keys obj-0 … obj-1999
04[CLUSTER]node E joins; reassign every key
05[REPORT]report load and keys moved
Start of hash mod n scenario. 5 steps.
CONSISTENT HASHING  (ring of 2^32 positions)

ADD_NODE(n):      for i in 0..V-1: insert token (hash(n + "#" + i), n)
                  keep tokens sorted by position
LOOKUP(key):      h = hash(key)
                  i = first token with position ≥ h     (binary search)
                  if none: i = 0                         (wrap around)
                  return tokens[i].node                  (the successor)

Adding a node: each new token takes the keys between its predecessor
token and itself, all from that token's old successor. Expected
moves: K / (N+1). Every moved key goes to the new node.

hash(key) mod N instead: going from N to N+1 nodes keeps a key only
when h mod N = h mod (N+1), so about N/(N+1) of all keys move.

One token per node: arc lengths are uneven, the largest of N arcs
is about (ln N)/N of the ring, so one node carries far more than K/N.
V tokens per node: each node's share is a sum of V arcs, and the
spread shrinks roughly like 1/√V.

Watch the NODE E JOINS box and the invariant strip. HASH MOD N moves 1614 of 2000 keys, 1209 of them between servers that were already there. RING, 1 TOKEN PER NODE moves few keys, but node C starts with 55% of the ring and, even after E joins, carries 2.26× the average. RING + VNODES keeps both invariants: 1.41× at worst, and all 355 moved keys go to E.

TUNE IT NOT YET

At 20 tokens per node the busiest node still carries about 1.4× the average. Raise VNODES PER NODE until the busiest node is within 1.25× the average both before and after E joins. Watch how uneven the improvement is from one setting to the next.

GOAL: max load ≤ 1.25 × average, before and after E joins

FIX IT NOT YET

Implement lookup(ring, key). ring.points is sorted by h ascending, each { h, node }; ring.hash(key) gives the key's position. Return the node of the first point with h ≥ hash(key), wrapping to the first point. The grader uses 20,000 points and allows 64 array reads per lookup.

Mental model:

the owner of a key should depend on its neighbourhood on the ring, not on how many nodes exist. Then a membership change touches only the arcs next to the node that changed. One position per node makes those arcs lumpy; many virtual nodes per node smooth them out, and a sorted token array plus binary search keeps lookup at O(log T).

IN PRODUCTION

Cassandra and DynamoDB-style partitioning (token ranges and virtual nodes), memcached client libraries such as ketama, load balancers that keep connection affinity across backend changes, and CDN request routing. The symptom of mod-N sharding is a cache hit-rate collapse every time a server is added or removed.

IN INTERVIEWS

“Design a distributed cache that can add servers without a cold start.” Show why mod N remaps about N/(N+1) of keys, draw the ring and the successor rule, say a join moves about K/(N+1) keys and only to the new node, add virtual nodes for balance, and describe lookup as binary search over sorted tokens.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: BLOOM FILTER →