How it works
In a primary/replica arrangement, changes go to the primary, and copies called replicas apply them and serve many reads. If one read replica fails, reads can move to another or to the primary. Replication does not distribute writes away from the primary, so a write bottleneck needs a different remedy.
Replication timing is a choice. With synchronous replication, the primary waits for a replica to confirm a change before telling the client it succeeded. No confirmed write is lost if that replica survives, but every commit pays for the extra round trip, and a stalled replica can stall writes. With asynchronous replication, the primary confirms first and ships the change afterward: faster, but replicas lag behind. Postgres defaults to asynchronous and can be configured for synchronous commits.
Lag makes replica reads stale. If someone renames their profile and the next page reads from a replica two seconds behind, they see the old name. A common fix routes a user’s reads to the primary for a few seconds after their own write. Lag also sets the cost of losing the primary: at 500 writes per second, two seconds of lag means up to 1,000 confirmed writes may be missing from the replica you promote.
Promotion also means redirecting clients and preventing split brain, where an isolated old primary keeps accepting writes alongside the new one and the copies diverge. Tools such as Patroni use a majority-based store like etcd to allow one primary at a time, and the old node must be fenced, meaning blocked from accepting writes.
Monitoring loses contact with the primary and promotes a replica. Ten minutes later the network heals, and some web servers are still writing to the old primary.
This is split brain: two nodes accept writes and their data is diverging. Fence the old primary at once so only the promoted node takes writes, then compare the two write histories and reconcile what you can. Prevent a repeat with fencing and a majority-based failover tool, because a health check alone cannot tell a dead primary from an unreachable one.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Replicas help with reads and availability, but their reads can be stale.
- Synchronous replication trades write speed for fewer lost writes; asynchronous does the reverse.
- Failover is an operational process: plan for missing writes and split brain.