SHARDING & REBALANCING

Adding one node should not move every key

L215 MINDATABASES
PREDICT

600 keys, placed with hash(key) % 4. You add a fifth node and switch to hash(key) % 5. About how many keys move?

Sharding splits keys across N nodes. The obvious rule, hash(key) % N, spreads keys evenly. Its flaw shows when N changes: almost every key’s remainder changes, so almost every key moves.

Virtual buckets fix that. Hash each key to one of a fixed number of buckets, say 128, and keep a table from bucket to node. To add a node, hand it whole buckets from the busiest nodes. Only those buckets’ keys move: about 1/(N+1) of them.

Balance is about requests, not keys. One very popular key lands on one shard however many you have.

▶ SIMULATION
ROUTER
no keys placed yet · 4 nodes
✓ keys moved on resize ≤ 1.5 × K / (N+1)✓ busiest shard ≤ 2 × average load
SCHEDULE
01[ROUTER]place 600 keys: node = hash(key) % N
02[OPS]add one node (N → N+1) and rebalance
03[OPS]report
Start of hash mod n scenario. 3 steps.
function shardFor(key, nodes) {
  return nodes[hash(key) % nodes.length];   // N changes → almost every key moves
}

Watch the RESIZE box and the invariant strip. HASH MOD N moves 482 keys to add one node. VIRTUAL BUCKETS moves 118. HOT KEY is balanced by key count and still breaks: one shard carries 2.6× the average. SALT THE HOT KEY runs the same viral traffic and resize and keeps both invariants. That works because the viral traffic here is writes to a like counter. If it were reads, each one would hit all 16 sub-keys and no shard would get quieter.

TUNE IT NOT YET

user:42 carries 40% of requests. Your plan is to add nodes until the load evens out. Drag SHARDS and watch the busiest shard after the resize. Find a shard count where it carries more than 3× the average.

GOAL: busiest shard > 3× average after adding a node

FIX IT NOT YET

Rewrite assign so adding a node moves only about 1/(N+1) of the keys, and no node gets more than 2× its share. You cannot keep a table between calls. Hash each key to one of a fixed number of virtual buckets, then give each bucket to the node with the highest hash(bucket + node) (rendezvous hashing). Adding a node only takes the buckets it now wins.

Mental model:

never let the number of nodes appear in the formula that places a key. Put a fixed layer of buckets in between and move buckets, not keys. Then remember that balance is measured in requests: a hot key has to be split (if it is write-hot) or replicated and cached (if it is read-hot), because no placement scheme can split one key across shards.

IN PRODUCTION

Redis Cluster's 16,384 hash slots, Couchbase vBuckets, Cassandra and DynamoDB virtual nodes and partitions, Vitess and Citus shard maps. The symptoms: a resize that empties the cache or triggers a huge data copy, and one shard pinned at 100% CPU while the rest idle.

IN INTERVIEWS

“Design the sharding for a key-value store that must grow without downtime.” Explain why hash mod N fails on resize, introduce fixed buckets with a bucket-to-node map (or consistent hashing with virtual nodes), say how rebalancing moves buckets, and handle the hot-key follow-up with salting or a cache in front.

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: DEADLOCKS IN THE DATABASE →