Database Replication and Failover
mediumReplication keeps multiple copies of data on different nodes for high availability, read scaling, and disaster recovery. The choice of topology and sync mode shapes durability, latency, and operational complexity.
Key Concepts
quorum): M of N must ack — practical compromise.Approach
- Pick the topology. Single-leader is default. Multi-leader for multi-region writes. Leaderless for Dynamo-style scale-out.
- Pick sync mode. Async default; semi-sync for stronger durability; sync only on hot critical writes.
- Set up replicas. 1-2 same-AZ for HA, 1+ different-AZ for AZ failure, 1+ different-region for DR.
- Wire up a proxy that knows the topology: PgBouncer/HAProxy + Patroni for Postgres; ProxySQL + Orchestrator for MySQL.
- Use a coordinator (etcd, ZooKeeper) for leader election and consensus on failover.
- Implement fencing: ensure the old primary cannot accept writes after demotion.
- Monitor lag continuously; alert if it exceeds budget; auto-page on failover.
- Practice failover in a staging environment monthly. The first time you do it should not be at 3 AM.
Replication modes
Async: primary commits + acks client + streams to followers later. Lowest write latency. Loses last N writes on primary crash.
Semi-sync: primary waits for >= 1 follower ack before client ack. Bounded data loss; small latency increase.
Sync (chain): every follower must ack. Strongest durability. Slow tail; one slow follower kills writes.
Group commit / quorum-sync: M of N followers must ack. Practical compromise (Postgres quorum mode).
Topologies
Single-leader: writes → primary → followers. Default for OLTP.
Multi-leader: writes accepted at multiple nodes, conflicts resolved later. Lower write latency in multi-region; conflict resolution required (LWW, vector clocks, CRDTs).
Leaderless (Dynamo/Cassandra): write to W of N replicas, read from R of N. R+W>N gives linearizability. No special leader role.
Chained replication: A → B → C. Writes durable when last node acks. Used by some specialized systems (Chain Replication paper).
Components
- WAL / binlog / oplog — primary's change stream.
- Replication slot / GTID / oplog position to track follower progress.
- Coordinator (etcd, ZooKeeper, Consul) for leader election.
- Failover automation (Patroni, Orchestrator, RDS, Aurora).
- Client proxy (PgBouncer, ProxySQL) to route reads/writes after promotion.
- Fencing mechanism — STONITH, network ACL change, demoted flag in coordinator.
- Lag monitor + replay alerts.
- Backup + PITR independent of replication.
Failover playbook
1. Detect: heartbeat timeout; quorum of observers agrees primary is unreachable.
2. Choose new primary: most up-to-date follower (highest WAL position).
3. Fence old primary: revoke writes (network ACL, demote flag, STONITH).
4. Promote chosen follower: stop replication, accept writes.
5. Reconfigure remaining followers to replicate from new primary.
6. Rewire client traffic: proxy updates routing.
7. Recover: when old primary returns, reset and bring back as a follower.
Test this in staging. Automation tools (Patroni, RDS) handle most of this; understanding the steps lets you debug when they don't.
Trade-offs
Async: low latency, can lose committed writes on failover.
Sync: durable, slow tail, sensitive to slow followers.
Single-leader: simple, but write capacity bounded.
Multi-leader: lower latency in multi-region, conflict resolution complexity.
Leaderless: tunable consistency, operationally complex.
Replica reads can be stale; route critical reads to the primary or use read-your-writes patterns.