CONSISTENT HASHING
Add a node and only about K/(N+1) keys move
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.
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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.