Adding a server remaps everything — hash-mod-N and the cluster-wide rebalance
Hash-mod-N has a catastrophic property: when N changes from N to N+1, roughly (N-1)/N of all keys change home — a cluster-wide rebalance, not a gentle nudge.
Hash mod N looked perfect — let's stress-test it by doing the most ordinary operational thing: adding one server.
Scene 03
Adding a server remaps everything
- Watch
- Try it
- Predict
- Capture
Same 12 keys you saw last scene, same 4-server cluster. After a beat, watch what happens when one server is added — the migration counter spikes and most dots turn orange. That's a rebalance.
Highlighted lines are the ones running in the diagram right now.
def migrationFraction(oldN, newN):moved = 0for key in keys:if hash(key) mod oldN != hash(key) mod newN:moved += 1return moved / len(keys)# closed form for oldN -> oldN+1: ~ (oldN - 1) / oldN# 4 -> 5 ~ 80% ; 8 -> 9 ~ 88% ; never small.
def rebalance(oldN, newN):for key in all_keys:oldOwner = servers[hash(key) mod oldN]newOwner = servers[hash(key) mod newN]if oldOwner != newOwner:stream(key, oldOwner -> newOwner)# cache miss on read until copy landsflip_routing(newN)
Where this sits in Build a wide-column store (Cassandra / DynamoDB family)
Scene 03 of 13, in the Sharding act — Hash mod N spreads load — until adding a server reshuffles 80% of keys.. When N changes from 4 to 5, almost every key's home changes — a cluster-wide migration.
Up next. Hash mod N reshuffled 80% of keys when N changed; we need a geometry where servers live in the same space as the keys, so adding one only nudges its neighborhood.
All 13 scenes in Build a wide-column store (Cassandra / DynamoDB family) · Every curriculum