Replication,
explained.
An overall breakdown of replication, the tradeoffs behind it, and what it means for the people using your app.
Keeping copies of your database on multiple machines sounds straightforward—until the data changes.
Which machine accepts the update? When do the other copies receive it? What happens if two people change the same thing while those machines can’t communicate?
Replication helps systems survive failures, serve users from nearby locations, and distribute reads. The challenge is keeping those copies useful while they temporarily disagree. The chapter explores three approaches: single-leader, multi-leader, and leaderless replication.
1. Single-leader replication: one place accepts writes
One database acts as the leader. It accepts changes, and followers reproduce those changes using a replication log.
Imagine a creator updating their profile. The leader saves the new bio, then sends that update to its followers. Those followers can serve reads, reducing pressure on the leader.
A new follower typically starts from a snapshot, then replays subsequent changes to catch up. If the leader fails, a follower can be promoted—but an asynchronous follower may not have received every acknowledged write.
2. Synchronous versus asynchronous: when is a write finished?
With synchronous replication , a write waits for acknowledgment from the required replica or replicas. This improves durability against certain failures, but adds latency and can block writes when those replicas are unavailable.
With asynchronous replication , the leader can acknowledge a write before followers confirm it. Writes avoid that wait, but replicas can lag behind.
For example, a “Saved!” message might appear before another machine has the update. If the leader is permanently lost during that window, the update may be lost too. The exact guarantees depend on what the acknowledgment confirms: receipt, durable storage, or application of the change.
3. Replication lag becomes a user-experience problem
Three guarantees describe different ways to make stale reads less confusing:
| Guarantee | What it means | Example |
|---|---|---|
| Read-your-writes | You can see your own completed updates. | Your new profile photo doesn’t disappear after refreshing. |
| Monotonic reads | Your reads don’t move backward to older data. | A comment you already saw doesn’t vanish on the next refresh. |
| Consistent prefix reads | Causally related changes appear in the right order. | A reply doesn’t appear before the message it answers. |
“Eventually consistent” alone doesn’t promise any of these experiences.
4. Multi-leader replication: more places accept writes
Multiple leaders accept updates and exchange them afterward. This supports situations such as regional writes and offline editing, but introduces conflicting changes.
Imagine editing a note on your disconnected laptop while changing the same paragraph on your phone. When they reconnect, both edits need reconciliation.
Choosing a winner can discard work. Merging requires rules that make sense for the data.
5. Leaderless replication: coordinate across replicas
In a leaderless design, writes go to multiple replicas without one designated leader ordering all writes.
A common configuration uses:
- N: number of replicas.
- W: acknowledgments needed for a successful write.
- R: replicas contacted for a read.
With
N = 3
,
W = 2
, and
R = 2
, read and write sets overlap. That helps readers discover recent versions, but
overlap alone isn’t a blanket guarantee of the latest value under concurrent writes
and failures.
6. Replicas need repair—and conflicts need meaning
Replicas can catch up through read repair , which fixes stale copies discovered during reads, and anti-entropy , which compares replicas in the background. Hinted handoff temporarily stores updates for an unavailable replica.
Version tracking helps distinguish an update that supersedes another from concurrent updates that need merging.
For a shopping cart, merging two independently added items may be sensible. For two edits to a delivery address, blindly combining fields could produce an address neither person intended. Conflict resolution is partly a product decision.