Chapter 12: Database Scaling — Replication, Sharding, Read Replicas

Scaling Strategy Progression

1. Optimize queries + indexes (free!) 2. Vertical scaling (bigger machine) 3. Read replicas (scale reads) 4. Caching layer (reduce DB load) 5. Sharding (scale writes + storage) Do them IN ORDER. Most systems never need step 5.

Read Replicas

Writes ──► Primary DB ──replication──► Replica 1 (reads) ──► Replica 2 (reads) ──► Replica 3 (reads) App routes: writes → primary, reads → any replica Scales read throughput linearly with replica count. Trade-off: replication lag (reads may be slightly stale)

Sharding (Horizontal Partitioning)

Shard by user_id: user_id % 4 = 0 → Shard 0 user_id % 4 = 1 → Shard 1 user_id % 4 = 2 → Shard 2 user_id % 4 = 3 → Shard 3 Each shard is an independent database holding 25% of data. Scales both reads AND writes.

Sharding Challenges

Choosing a Shard Key

KeyProCon
user_idAll user data on one shardCelebrity users create hot shards
geographic regionData localityUneven region sizes
hash(id)Even distributionRange queries hit all shards
time-basedOld data archivableRecent shard gets all writes
⚠️ Sharding is a Last Resort

Sharding adds enormous complexity. A single PostgreSQL instance with proper indexes handles millions of rows and thousands of QPS. Exhaust all other options (indexes, caching, read replicas, vertical scaling) before sharding.

Key Takeaways