14. Replication
1
The picture to keep in your head2
14.1 Why keep copies of the same data?3
14.2 How does leader-follower replication work?4
14.3 Should the leader wait for followers before confirming a write?5
14.4 What is replication lag, and why does it happen?6
14.5 How do you stop lag from confusing users?7
14.6 How do you use read replicas in practice?8
14.7 What happens when the leader fails?9
14.8 What is split brain, and how is a leader safely chosen?10
14.9 When would you have more than one leader?11
14.10 How does leaderless replication work, and what are quorums?12
Memory card and where the analogy breaks13
Practice: five levels14
Interview questions15
Solutions16
Sources17
Code lab14. Replication
14.1 Why keep copies of the same data?
3 min read+10 XP
The basics
At the bank: Copies at every branch mean customers don't all travel to head office (less crowding), customers in far cities read notices nearby (faster), and if head office is lost, the notices still exist (safety).
In the system: Replication keeps the same data on several machines, called replicas. Three reasons:
- Survive failures (availability and durability): if the machine holding the data dies, another copy takes over. Without replicas, one disk failure can mean lost orders.
- Scale reads: spread read traffic across copies. The bookstore's browsing (book pages, search results) is mostly reads (Chapter 2's read/write ratio).
- Be closer to users: a copy in another region serves nearby users faster (Chapter 7, section 7.10).
What replication doesn't do:
- It doesn't help write throughput in the common setup: every copy must apply every write. Splitting writes is sharding (Chapter 15).
- It isn't a backup. Delete a table by mistake, and the delete is faithfully copied everywhere within seconds. Keep separate backups.
Memory code: "Copies for safety, for crowds and for distance, but never instead of a backup."