15. Sharding
15.8 How do sharding and replication work together?
Putting it together
At the exam centre: Each hall holds its own students (that's the split). And each hall's answer sheets are photocopied and kept in a second building, in case one hall floods (that's the copies).
In the system: Real systems shard and replicate: each shard is itself a small leader-follower group (Chapter 14).
Figure: 4 shards × 3 copies. Writes scale with shards; reads and safety with replicas.
- Shards spread data and writes.
- Replicas within each shard give failover, durability and read capacity.
- Each shard fails over on its own: a dead machine affects only that shard's users, and only until its replica takes over.
- Spread each shard's copies across availability zones, and make sure two shards' leaders aren't all on one physical machine.
Sizing (assumed): 2 billion orders at about 1 KB each is about 2 TB. Keeping each shard near 250 GB gives 8 shards; with a leader and two replicas each, 24 database servers. At 20,000 writes a second at peak, each shard leader takes about 2,500. With 1,024 logical shards, that's 128 per server, ready to spread to more servers later.
Many managed and distributed databases do all of this internally (DynamoDB, Spanner, CockroachDB, Cassandra, MongoDB's sharded clusters), but the same questions remain yours: which shard key, which queries cross shards, and where the hot spots are.
Memory code: "Split for size, copy for safety: every hall has its own photocopies."