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
Memory card and where the analogy breaks
3 min read+10 XP
The memory card (tear this page out)
| # | Question | Memory code |
|---|---|---|
| 14.1 | Why replicate? | Copies for safety, for crowds and for distance, but never instead of a backup |
| 14.2 | Leader-follower? | One office writes, every branch files the copies in order |
| 14.3 | Sync vs async? | Wait for a copy and be safe, or don't wait and be fast |
| 14.4 | Replication lag? | The courier can fall behind; always know by how much |
| 14.5 | Fixes for lag? | Show people their own changes, and never let time run backwards |
| 14.6 | Read replicas? | Reads to the branches, changes to head office |
| 14.7 | Failover? | Notice, choose the freshest, promote, redirect, and silence the old boss |
| 14.8 | Split brain? | Only a majority can crown a leader, and the old crown must stop working |
| 14.9 | Multi-leader? | Many offices, many pens, and someone must settle the arguments |
| 14.10 | Leaderless quorums? | Write to most, read from most, and the newest version wins |
The whole chapter in one line: One office writes, branches copy in order, know how far behind they are, and only a majority may crown a new head office.
Where the analogy breaks
- A courier takes hours. Replication usually takes milliseconds, which is why lag problems show up as rare, confusing bugs rather than obvious delays.
- A burnt office is obviously gone. A failed server often isn't: it may be slow, half-working or cut off, which is exactly what makes failover and split brain hard.
- Branches can phone each other to compare notes. Replicas can't trust each other's clocks, so "which write was last?" is much harder than it sounds.
Quick check
Five questions on this chapter. Pick an answer, then check it.
quiz