7. Sharding and partitioning
One-liner. When one machine can't hold the data, split it by a key you'll always query by.
Memorize
- Range partitioning: good scans, risk of hot spots (time-ordered writes).
- Hash partitioning: even spread, kills range queries.
- Consistent hashing: adding nodes moves ~1/N of keys instead of everything.
- Hot key (celebrity user, viral item) needs salting, caching, or request coalescing.
- Resharding is the hard part: plan for double-writes + backfill + cutover.
- Shard key = the key present in every hot query; otherwise you fan out to all shards.
Trap. Sharding by created_at for a feed — every write lands on one shard. Do this. Check the shard key against your top three queries.
Check
A time-series table is sharded by timestamp and one shard takes all writes. Best fix?
Why
The write path is the bottleneck; replicas only help reads. Composite/hashed keys spread writes.