Replication & sharding
Leader/follower, replication lag and stale reads, partition-key choice, resharding pain, hot keys. Every scale question eventually lands here.
The mental model
Two completely different problems get confused because both involve "more than one database server".
Replication is photocopying. Every server holds the same data. You do it for durability (a machine dies, the data lives), for availability (failover), and for read scaling (many copies answering reads). It does nothing for write throughput — every write still has to be applied by every copy — and nothing for dataset size, because every copy holds everything.
Sharding is filing cabinets. Each server holds a different slice of the data — customers A-M in one, N-Z in another. It buys write throughput and dataset size, and it costs you every query that wants to cross cabinets: joins, aggregates, transactions, uniqueness constraints, and "show me everything sorted by date".
The library analogy that sticks: replication is keeping three identical copies of every book so the library survives a fire and three people can read simultaneously. Sharding is splitting the collection across three buildings — now you can hold three times the books, but "find every book by this author" means visiting all three, and a librarian who put all the popular books in one building has created a queue at that door.
Without replication, one disk failure is data loss and one reboot is downtime. Without sharding, you are capped by the largest single machine you can rent — a real ceiling, but a high one: a modern instance with fast NVMe handles tens of thousands of write transactions per second and multi-terabyte datasets. The most common architecture mistake in this topic is sharding at 200 GB because a blog post said to, and inheriting cross-shard pain for a decade.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision