Load/Bearing
Fundamentals · 11 of 22

Sharding and consistent hashing

When one primary can't absorb your writes, split the data so each database owns a slice.

Caches and replicas scale reads. Writes are different: every replica still applies every write, so a single primary is a hard ceiling on write throughput and on how much data fits on one machine. Sharding (horizontal partitioning) breaks that ceiling. Each shard is a full, independent database that owns a subset of the rows.

APPROUTERkey → shardSHARD Ausers 0–3SHARD Busers 4–7SHARD Cusers 8–bSHARD Dusers c–f
A router (in the app, a proxy, or the database itself) maps each key to the shard that owns it.

Try everything else first

Sharding is hard to undo and makes every later feature more complicated. Before you shard, check that you have tried indexes and query tuning, a bigger machine, a cache, read replicas, moving old data to cheaper storage, and splitting unrelated tables into separate databases.

Partitioning strategies

StrategyHowStrengthWeakness
RangeShard A holds ids 1–1M, B holds 1M–2M…Range scans stay on one shardHot spots: new ids all land on the last shard
Hashshard = hash(key) mod NEven spreadRange queries hit every shard; changing N moves most keys
Consistent hashKeys and shards placed on a ringAdding a shard moves only ~1/N of keysMore complex routing
DirectoryA lookup table maps key → shardFull control, easy to move tenantsThe directory is another critical service

Choosing the shard key

The shard key is the most important decision. A good key:

Bad keys create hot spots. A celebrity's account or a timestamp-based key can send most of the traffic to one shard. Mitigations include splitting a hot key into key#0…key#9 (and merging on read), or giving very large tenants their own shards.

Why mod-N hurts, and consistent hashing

With hash(key) mod N, going from 4 to 5 shards changes the shard for about 80% of keys, because a key only stays put if h mod 4 = h mod 5. That's a huge data migration for one new shard.

Consistent hashing places both shards and keys on a ring of hash values. Each key belongs to the next shard clockwise. Adding a shard only takes keys from its immediate neighbour, about 1/N of the data. Giving each physical shard many virtual nodes on the ring smooths out uneven ranges and spreads a failed node's load across all the others. Dynamo, Cassandra and many caches use this approach.

The costs you take on

In the game: each database you add in Hypergrowth is another shard with 600 writes/s of capacity. About 2,500 writes/s at peak means at least five shards, and six keeps write latency comfortable. The CDN and cache keep reads off the shards, so the budget goes on write capacity. Celebrity Shard lets you choose the shard key and the key-to-shard mapping, then adds a shard mid-run: watch one shard turn red with a range or plain user_id key, and the rebalance overload every shard with hash mod N.

Interview checklist

Practise it

Level 13: Celebrity Shard

Put this into practice against live traffic. Your progress in the game is saved in your browser.

Play level 13

Further reading