Lesson 7 of 9 · 25m
Replication and Failover Without the Fairy Tales
Replication copies changes to other nodes for durability and availability. Failover is easy to automate and hard to make safe — especially after the old primary comes back.
Replication starts simple: ship the write-ahead log from a primary to replicas, replay it there, and use replicas for reads or failover. Then the primary dies during a deploy, a replica is twelve seconds behind, the automation promotes it anyway, and the old primary reboots believing it is still king. Distributed systems are mostly failure handling.
Physical replication is the common default
primary -- WAL stream --> replica A (synchronous)
\- WAL stream --> replica B (asynchronous)The replica applies the same physical changes. PostgreSQL streaming replication and MySQL binlog replication differ in details, but the core flow is similar: ordered change records, replayed elsewhere. Because replication is ordered, lag is meaningful: it tells you how much committed work may be absent from a replica.
Synchronous means waiting, not impossible to lose
A synchronous replica acknowledges that committed WAL is durable on the replica before the primary returns success. That reduces data loss, but every commit now pays network latency and replica availability risk. Asynchronous replication is faster and more tolerant, but failover can lose recent commits.
There is no free setting here. You choose between latency, availability, and durability. Anyone claiming otherwise is selling slides.
Read scaling has consistency traps
Sending reads to replicas is useful, but replication lag means users can write to the primary and then fail to read their own write from a replica. Fixes include pinning a session to primary for a short window, tracking WAL positions, or routing read-your-write traffic explicitly.
-- PostgreSQL replica lag is often inspected in bytes or seconds
SELECT client_addr, state, replay_lag
FROM pg_stat_replication;Lag is not a single integer in practice. Network delay, replay speed, long-running queries on the replica, and disk contention can all contribute.
Failover is a split-brain machine unless fenced
The dangerous sequence:
1. monitoring loses contact with primary
2. automation promotes replica
3. old primary still accepts writes from some clients
4. two divergent write histories now existYou need fencing: revoke writes at the network layer, terminate connections, update service discovery atomically, and force the old primary down before it can rejoin. Then reintroduce it as a replica and rebuild it if its timeline diverged. Do not improvise this at 3 a.m.; rehearse it.
Field notes
- A replica that cannot keep up is not a backup; it is an additional outage waiting to happen.
- Monitor replication delay from the application SLO point of view, not just bytes behind.
- Automated failover without tested fencing is a split-brain generator.
- Backups and replication solve different problems. Replication copies corruption and accidental deletes faithfully.
Was this lesson helpful?