High Availability with Patroni
The leader key in etcd, how a replica promotes itself, how the old primary rejoins, and how clients follow
Postgres can stream WAL to replicas and promote one, but it never decides
on its own that the primary is dead. Something outside has to watch,
decide, promote exactly one replica, and make sure the old primary can't
keep accepting writes. In compose_prod.yaml that's Patroni, with
etcd as the referee.
The leader key
Every node runs a Patroni process next to Postgres. Every loop_wait
(10 s) each one wakes up and looks at one key in etcd,
/service/pg-cluster/leader:
- The primary renews the key, which has a TTL of 30 s. As long as it renews, everyone else stays a replica and follows it.
- If the primary stops renewing (crashed, frozen, cut off), the key expires after the TTL.
- The replicas race to create the key. etcd's compare-and-swap means exactly one succeeds; that node promotes its Postgres. The others start following it.
- A replica too far behind (
maximum_lag_on_failover, 1 MB here) doesn't join the race, so the cluster doesn't promote a node that would throw away a lot of data.
etcd itself is three members and needs a majority (2) to accept a write. That's what makes the key safe: two halves of a split network can't both create it, because only one half has a majority.
The other half: a primary that loses the key steps down
Electing a new primary is only safe if the old one stops. A primary whose Patroni can't renew the key (it can't reach etcd) demotes its own Postgres to read-only when the key's TTL runs out. That's what stops split brain. The failure drills measure it: with the primary cut off the network, it stopped answering as primary before the new one was elected.
The same rule has a sharp edge. If etcd is lost (two of three
members), every Patroni loses the key, and the healthy primary demotes
itself too: the drill shows 35 seconds with no primary although no
Postgres had failed. failsafe_mode: true changes this: while the primary
can still reach every other member over Patroni's REST API, it keeps
running. The drills measure both.
How the old primary comes back
When the dead primary returns, its data may contain WAL the new primary
never received (the async loss window). It can't simply follow the new
primary. Patroni runs pg_rewind: it finds where the two timelines
diverged and copies back only the blocks that changed since, turning the
old primary into a replica in seconds instead of re-cloning hundreds of
gigabytes. That's why use_pg_rewind: true and wal_log_hints are on
(data checksums would also do). In the failover drill the killed primary
rejoined by rewind in 4 seconds.
How clients find the primary
Each node's Patroni answers HTTP health checks:
curl -s -o /dev/null -w '%{http_code}' http://localhost:8008/primary # 200 on the leader, 503 elsewhere
curl -s -o /dev/null -w '%{http_code}' http://localhost:8008/replica # 200 on a healthy replicaHAProxy checks /primary on all three every second and routes port 5000
to the one that answers 200. When the primary changes, on-marked-down shutdown-sessions cuts the connections to the old one, so clients
reconnect instead of waiting on a node that can't write. PgBouncer sits in
front of HAProxy and reconnects for them.
Day-to-day commands
P="docker exec pg-1 patronictl -c /etc/patroni/patroni.yml"
$P list # roles, lag, timeline
$P switchover --leader pg-1 --candidate pg-2 --force # planned: ~3 s of errors
$P edit-config # cluster-wide settings in etcd
$P restart pg-cluster pg-2 # one node
$P reinit pg-cluster pg-3 # re-clone a broken replicaThe timeline is how Postgres keeps histories apart. Every promotion
starts a new timeline (TL in patronictl list), and WAL files carry it
in their names (0000000C... is timeline 12). A replica can only follow
a primary whose timeline history includes its own position, which is why
an old primary needs a rewind before it can follow.