Volumes
The phone book got too thick to bind, so it's split into volumes: AโH, IโP, QโZ. To find or add someone, a guide tells you which volume, and you only open that one. Different people can use different volumes at the same time.
Partitioning by key
Replicas copy all the data, which scales reads but not data size or write volume. Sharding (horizontal partitioning) splits the rows across independent databases. A shard key decides where each row lives:
- Range: key ranges per shard. Range scans are efficient; uneven ranges can hotspot.
- Hash:
hash(key)decides the shard. Even spread; range scans hit every shard. Consistent hashing limits data movement when shards are added. - Directory: a lookup table maps keys to shards, flexible but one more thing to keep available.
A router (in the app, a proxy like Vitess, or the database itself, as in MongoDB or Citus) sends each query to the right shard.
The busy volume
If you split by month instead, everyone writes in this month's volume while the others gather dust. Scattering entries by a scrambled customer number shares the work evenly.
Choosing the shard key
A good shard key has high cardinality, spreads writes evenly, and matches your most common queries. Bad keys create hotspots: monotonically increasing ids or timestamps send every new write to one shard, and a celebrity user can overload theirs. Changing the key later means moving all the data, so choose carefully.
Questions about everyone
"Everyone who lives in Leeds?" isn't sorted by surname, so you open every volume and combine the answers. And changing two people in different volumes, all or nothing, needs careful coordination.
The costs of splitting
- Queries without the shard key scatter-gather to every shard: latency is set by the slowest shard, and every shard does work for one query.
- Joins and transactions across shards need two-phase commit or sagas; foreign keys can't span shards.
- Rebalancing when adding shards moves data while the system is live (see the failure list below).
- Operations (backups, schema changes) multiply by the shard count.
So shard late: indexes, caching, read replicas and a bigger machine go a long way first. In multi-tenant SaaS, sharding by
tenant_id keeps each customer's data, and queries, on one shard.
When a volume goes missing
A few things can go wrong. One volume is borrowed by a thousand people at once, say the one holding a famous name, and the queue for it grows while the other volumes sit idle. Worst of all, if the book was split the wrong way, re-splitting it means copying every page.
Or one volume goes missing: only people who need names in it are stuck, and everyone else carries on. The fix is a spare copy kept in another building.
And when the library moves pages between volumes, the guide's notes can be out of date, so readers get sent to the wrong volume for a while.
What can go wrong
- A hot shard (celebrity key). One shard saturates while others idle: its CPU, queue depth or p99 latency sits far above its siblings. Fix: a better shard key, splitting the hot range, or caching and read replicas for hot reads (see Choosing the shard key above).
- One shard down (partial outage). Requests for that shard's keys fail while the rest succeed, so error rates look like a fixed percentage of users, and anything scatter-gathering across all shards fails or returns partial data. Fix: run each shard as a primary with replication and automatic failover; the router must then learn the new primary. Asynchronous replication can lose the newest writes on failover.
- Cross-shard queries and transactions. A transaction touching two shards has no single-database atomicity: a crash between the two commits can leave one applied. Symptoms are slow tail latency and half-applied updates. Fix: keep related rows together, use two-phase commit or sagas with idempotent steps, and avoid cross-shard joins on hot paths (see The costs of splitting above).
- Resharding while writes continue. Rows are copied to a new shard while the old one still takes writes, so changes made mid-copy must be replayed; for a while a key may live in both places, and a router with stale routing sends reads or writes to the old one (lost or stale data, duplicate rows). Fix: copy, replay changes, verify, then switch routing atomically and drop the old copy later; use consistent hashing or many small virtual shards so only a fraction of keys move.
- A bad shard key. Changing it means a long online migration that rewrites and moves all the data (MongoDB 5.0+ can
change a collection's shard key online with
reshardCollection, but it still copies all the data). You'd see permanent hotspots or most queries scatter-gathering. Fix: model your top queries before choosing (see Choosing the shard key above).
| Choice | You gain | You pay |
|---|---|---|
| Hash key vs range key | Hash: even spread, no hot tail | Range scans hit every shard; range keeps scans on few shards but risks hotspots |
| More shards vs fewer shards | More: smaller units, more headroom, smaller failures | More machines to run, more scatter-gather fan-out, more rebalancing and operations |
| Routing in the application vs a proxy | App: one fewer hop, no extra service | Every client must know the shard map and be updated; a proxy centralises routing and failover but adds a hop and a component to keep available |
| Synchronous vs asynchronous shard replicas | Sync: no acknowledged writes lost on failover (if the promoted replica was in sync) | Slower writes; async is faster but can lose recent writes |