Data building blocks
Replication
Keeping copies of the same data on several machines, the three topologies that databases use, and the anomalies that replication lag causes. Start here for the replication map.
Replication keeps a copy of the same data on several machines. It sounds like a backup problem, but it is really a coordination problem. Every write has to reach every copy. The copies are connected by an unreliable network, and machines fail in the middle of it. Most serious database incidents, such as lost writes after a failover, a user who cannot see their own comment, or two servers that both think they are in charge, come from how replication handles those facts.
Why replicate
Four reasons, and they pull in different directions:
- Availability. If one machine dies, another holds the data and keeps serving. A single server at 99.9% availability is down about 8.8 hours a year.
- Read scaling. If reads outnumber writes 20 to 1, five read replicas multiply read capacity roughly fivefold.
- Latency. A replica in Frankfurt serves European users in about 10 ms, instead of a 90 ms trip to Virginia. See multi-region.
- Durability. A disk can die for good. Data on three machines in three availability zones survives what one machine cannot.
These goals pull against each other. Read scaling wants many replicas that answer without coordination. Durability wants writes confirmed on several machines before acknowledging. Availability wants writes to keep working when some copies are unreachable. Each topology below makes a different compromise.
Concepts in this topic
Read them in order. Each one builds on the last.
| Step | Concept | What it answers |
|---|---|---|
| 1 | Single-leader replication | How do writes reach followers, and when does a commit count as done? |
| 2 | How changes are shipped | What exactly travels from leader to follower? |
| 3 | Failover and split brain | What happens when the leader dies, and what can be lost? |
| 4 | Replication lag anomalies | Why does a user see old data after writing? |
| 5 | Multi-leader replication | How do several regions accept writes, and how are conflicts settled? |
| 6 | Leaderless quorums | How can a store stay writable with no leader at all? |
Choosing a topology
| Single-leader | Multi-leader | Leaderless | |
|---|---|---|---|
| Write conflicts | None | Must be resolved | Must be resolved |
| Write availability during a partition | Only on the leader's side | Every leader keeps writing | Any W reachable nodes |
| Read freshness | Fresh from the leader, lagging on followers | Lagging across leaders | Tunable with R and W |
| Operational complexity | Lowest; failover is the hard part | High | Moderate; repair is ongoing work |
| Typical systems | Postgres, MySQL, MongoDB | Multi-region MySQL, CouchDB, offline apps | Cassandra, ScyllaDB, Riak |
Default to single-leader. Reach for multi-leader when writes must be accepted in several regions or offline, and you have a conflict plan. Choose a leaderless store when you need very high write availability, and your data can tolerate concurrent updates.
Common mistakes
- Treating a replica as a backup. A bad
DELETEreplicates too. - Alerting only on whether replicas are up. A replica 20 minutes behind is a failover candidate that will lose 20 minutes of data. Alert on lag.
- Reading from a replica right after a write without a plan. Users will see old data.
- Choosing multi-leader without a conflict plan. Ask what happens when the same row changes in two regions within the replication delay.
Key takeaways
- Replication buys availability, read scale, latency and durability, and every copy is a coordination problem.
- Single-leader is the default, because one leader gives a single write order and no conflicts.
- Failover is where incidents happen. Detection timeouts, the choice of follower and fencing the old leader all matter.
- Lag causes user-visible anomalies, which have standard fixes.
- Leaderless quorums give tunable freshness, but they are not linearizable.
Further learning
Martin Kleppmann's Cambridge lecture is a concise walkthrough of why replication is hard, from the author of the standard textbook:
Start with single-leader replication to see how one leader orders every write.
Related
Single-leader replication
One node accepts every write and streams its log to followers. The choice between synchronous and asynchronous commits decides whether a crash can lose an acknowledged write.
Failover and split brain
The four steps of promoting a follower after the leader fails, the data that asynchronous replication can lose, and how fencing prevents two leaders from writing at once.
Leaderless quorums
How Dynamo-style stores accept writes with no leader, using W and R quorums, sloppy quorums with hinted handoff, and repair with read repair and Merkle trees.
CAP, PACELC and consistency models
What "consistent" means once data lives on several replicas, from linearizable to eventual, what each guarantee costs, and how to pick one per feature.
Consensus and Raft
How a cluster agrees on one value or one log despite crashes and partitions, how Raft works, and where etcd, ZooKeeper and consensus-backed databases fit.
Multi-region architecture
Active-passive, active-active and cell-based designs for latency, disaster tolerance and data residency, with the recovery, consistency and cost trade-offs each one carries.