Lesson 7 / 32

Replication & Sharding

Scale reads with replication and writes with sharding, and pick shard keys that avoid hotspots.

Replication

Keep copies of the same data on multiple nodes. A primary takes writes and streams them to replicas that serve reads and stand by for failover.

Sharding (partitioning)

Split one dataset across nodes by a shard key (e.g. user_id hash or range). Each shard holds a slice, so writes and storage scale out.

Pick the shard key carefully

A bad key creates hotspots (e.g. sharding by date puts all of today's writes on one node). Aim for even distribution and queries that hit one shard.

Quick check: Your database is bottlenecked on write throughput. What helps most?

  • Shard the data
  • Add read replicas
  • Add a bigger cache
  • Switch to a larger CDN
Answer

Shard the data — Replicas and caches scale reads. Sharding spreads writes across nodes.