SHARDING & REBALANCING
Adding one node should not move every key
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.
function shardFor(key, nodes) {
return nodes[hash(key) % nodes.length]; // N changes → almost every key moves
}const BUCKETS = 1024; // fixed forever
const bucketOf = (key) => hash(key) % BUCKETS;
// bucketTable: bucket -> node, stored in config (Redis Cluster slots, Couchbase vBuckets)
function shardFor(key) { return bucketTable[bucketOf(key)]; }
// add a node: move whole buckets to it, nothing else changes
// write-hot key (a counter): salt it so writes land in many buckets
const writeKey = `${key}#${rand(16)}`; // reads fan out to all 16 and sum
// read-hot key: salting does not help (every read hits all 16);
// replicate it or cache it in front: write all copies, read one 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Partitioning and shardingDatabases
- Project: shard the shopDatabases