Replication keeps the same data on several machines. It serves two goals that are easy to mix up: scaling reads (more machines can answer queries) and availability (if one machine dies, another has the data). It does nothing for write throughput, because every copy has to apply every write.
Leader–follower
This is the most common setup (PostgreSQL streaming replication, MySQL replication, and managed services such as RDS and Cloud SQL). One node, the primary or leader, accepts writes. It records every change in a log, and the replicas (followers) replay that log. The app sends writes to the primary and spreads reads across the replicas.
Synchronous or asynchronous
| Mode | The primary confirms a write after… | Trade-off |
|---|---|---|
| Asynchronous | it's written locally | Fast. A replica can be seconds behind, and failover can lose the last writes. |
| Synchronous | every replica has it | No data loss, but one slow replica slows every write. |
| Semi-synchronous | at least one replica has it | The usual compromise: one up-to-date standby, the rest async. |
Replication lag and its bugs
With async replication, a replica is always a little behind, usually milliseconds and sometimes seconds under load. That causes some classic bugs:
- Not seeing your own write: a user edits their profile and refreshes. The read goes to a lagging replica and shows the old version. Fix: read from the primary for a short window after a user writes, or pin that user's reads to the primary.
- Going back in time: two refreshes hit two replicas with different lag, and a comment appears, then disappears. Fix: keep each user on the same replica.
- Effects without causes: a reader sees a reply before the message it replies to. Fix: keep related writes on one partition, or track causality.
Failover
When the primary dies, a replica has to be promoted:
- Detect the failure, usually after several missed heartbeats. Too eager and you fail over on a network blip; too slow and the outage lasts longer.
- Choose the most up-to-date replica.
- Promote it and point writers at it, through DNS, a proxy or service discovery.
- Fence the old primary so that if it comes back, it can't accept writes too. Two nodes that both think they're primary is called split brain, and it corrupts data.
Two numbers describe how good your failover is. RTO (recovery time objective) is how long writes are unavailable. RPO (recovery point objective) is how much recent data you can lose. Async replication has an RPO above zero.
Beyond one leader
- Multi-leader: several nodes, often one per region, accept writes. Writes are fast locally, but concurrent edits conflict and need resolving.
- Leaderless (Dynamo-style, as in Cassandra): write to W of N replicas and read from R of them. If
W + R > N, every read overlaps at least one replica holding the latest write.
Replicas or a cache?
Both reduce load on the primary. A cache is cheaper per read served and faster, but it only helps with repeated reads and needs invalidation. Replicas handle any query, including ad-hoc and analytical ones, and double as failover standbys, but each one costs as much as a database. Many systems use both.
In the game: each replica adds 1,500 reads/s. In Four Nines, the primary dies at 38 s. With a replica, you lose 2 seconds of writes while it's promoted. Without one, every database request fails for 12 seconds. In Stale Reads, 15% of reads are users checking their own edits: sending every read to any replica shows them old data, sending everything to the primary overloads it, and routing only post-write reads to the primary fixes both.
Interview checklist
- Say whether replication is there to scale reads, for availability, or both.
- Choose sync, async or semi-sync and state the RPO.
- Handle reading your own writes.
- Describe failover detection, promotion and fencing.
- Remember that replicas don't scale writes.
Level 5: Stale Reads
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 5Further reading
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.