Replication
Replication means keeping a copy of the same data on several machines (replicas), so the system survives a dead server, serves more reads, and sits closer to users. The hard part is not copying the data once: it is keeping the copies in step while writes keep arriving and machines keep failing.
Every replicated database answers two questions:
- Who accepts writes? One leader, several leaders, or any replica.
- When is a write “done”? As soon as one machine has it (asynchronous), or only after others confirm it (synchronous).
Watch a write travel
The most common setup is single-leader (also called primary/replica): clients write to the leader, the leader ships its log to the followers, and reads can go to any of them.
| NO. | STEP | FROM | TO | TYPE | INFO |
|---|---|---|---|---|---|
| 1 | 01 | client | leader | WRITE | user 42 city = Toronto → log pos 42, ack |
| 2 | 02 | leader | follower1 | REPLICATE | pos 42 applied |
| 3 | 02 | leader | follower2 | REPLICATE | pos 42 delayed (busy disk, slow link) |
| 4 | 03 | client2 | follower2 | READ | city = New York · stale by 2 s |
| 5 | 04 | leader | follower2 | REPLICATE | pos 42 applied · replicas agree |
- One leader and two followers all store user 42: city = New York. Only the leader accepts writes.
- The user moves to Toronto. The leader appends the change to its log and, in async mode, confirms to the client immediately.
- The leader streams the log to its followers. Follower 1 applies it; follower 2 is busy and lags behind.
- Another client reads from follower 2 and gets New York: a stale read caused by replication lag.
- Follower 2 catches up and the replicas agree again. Async replicas are eventually consistent.
If the leader had crashed between step 1 and step 2, the client would have been told “saved” while no other machine had the write. Promoting a follower at that moment loses the write. That is the core trade-off of asynchronous replication.
Synchronous vs asynchronous
| Mode | Write is acknowledged when | Upside | Downside |
|---|---|---|---|
| Synchronous | every (or a chosen) follower has it | No data loss on failover | One slow follower slows every write; one dead follower can block writes |
| Semi-synchronous | at least one follower has it | Survives a leader crash, bounded slowdown | Still pays one network round trip per write |
| Asynchronous | the leader has it | Fastest writes, followers can be far away | Recent writes are lost if the leader dies; followers serve stale data |
PostgreSQL controls this with synchronous_commit and synchronous_standby_names, MySQL with semi-sync plugins, and Kafka with acks=all plus min.insync.replicas. The latency cost of waiting is the “else” half of PACELC.
Three replication topologies
| Topology | Who accepts writes | Examples | Watch out for |
|---|---|---|---|
| Single-leader | One leader per dataset (or per partition) | PostgreSQL, MySQL, MongoDB replica sets, Kafka partitions | Failover: detecting a dead leader, avoiding two leaders (split brain) |
| Multi-leader | One leader per region or device | Multi-region MySQL/Postgres setups, offline-first apps, CouchDB | Write conflicts: two leaders change the same row at once |
| Leaderless | Any replica; client writes to several | Cassandra, ScyllaDB, Riak, the original Amazon Dynamo design | Needs quorums, read repair and conflict resolution; see Consistency vs Eventual Consistency |
Leader election and failover in modern systems usually rely on a consensus protocol (Raft in etcd, CockroachDB, Kafka KRaft; Paxos in Spanner) so that the cluster agrees on exactly one leader.
Living with replication lag
Async followers are always a little behind, normally milliseconds, sometimes minutes under load. Three guarantees fix the most visible problems:
- Read-your-writes: after a user saves their profile, read it back from the leader (or from a replica that has caught up to that write’s log position).
- Monotonic reads: pin a user to one replica so a refresh never shows older data than the previous page.
- Consistent prefix: readers see writes in the order they happened, so an answer never appears before its question.
How replicas are fed
- Statement-based: ship the SQL text. Breaks on
NOW(),RANDOM()and triggers. Mostly historical. - Physical log (WAL) shipping: ship disk-level changes. Exact copies, but leader and follower must run the same major version.
- Logical (row-based) log: ship “row X changed from A to B”. Works across versions and is what change data capture tools like Debezium read.
Replication vs sharding
Replication copies the same data to more machines (more availability, more read capacity). Sharding splits different data across machines (more storage and write capacity). Production systems combine them: each shard is replicated.
Common mistakes
- Treating a replica as a backup. A
DROP TABLEor a bad migration replicates in milliseconds. Keep point-in-time backups too. - Reading from a replica right after writing. Classic bug: “I saved it and it disappeared.” Route those reads to the leader.
- Running analytics on the primary. Point heavy BI or ELT extraction at a read replica or at CDC instead.
- Ignoring lag metrics. Alert on replica lag (seconds and bytes) the same way you alert on CPU.
- Automatic failover without fencing. If the old leader is only slow, not dead, you can end up with two leaders accepting writes.
Learning Resources
Books
- Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini: the replication chapter
- Database Internals, Alex Petrov: part II on distributed systems
Docs and articles
- PostgreSQL: High Availability, Load Balancing, and Replication
- In Search of an Understandable Consensus Algorithm (Raft): how leaders are elected safely
- Jepsen analyses: what real databases lose during failover