Failure Drills
Losing etcd, cutting the primary off the network, a dead replica holding WAL, and a full disk, with writers running
The failover drill covered a dead primary. labs/drills.py breaks the
other things a cluster depends on, while writers run through PgBouncer,
and prints a line whenever who-is-primary or the write rate changes
(P primary, r replica, - not answering). Every drill restores what it
broke before it exits.
docker compose -f compose_prod.yaml run --rm labs python drills.py # all
docker compose -f compose_prod.yaml run --rm labs python drills.py etcd # oneetcd loses its majority
Two of the three etcd members are stopped at t=0 and started again at t=60. No Postgres node has anything wrong with it.
=== etcd majority lost (failsafe_mode off)
t= 10.2s pg-1=P pg-2=r pg-3=r writes ok 2294/s errors 0/s
t= 25.3s pg-1=- pg-2=r pg-3=r writes ok 1371/s errors 2/s
t= 26.5s pg-1=r pg-2=r pg-3=r writes ok 0/s errors 0/s
...
starting etcd-2 and etcd-3 again
t= 61.5s pg-1=P pg-2=r pg-3=r writes ok 0/s errors 72/s
t= 62.6s pg-1=P pg-2=r pg-3=r writes ok 1776/s errors 14/s
=== etcd majority lost (failsafe_mode on)
t= 1.3s pg-1=P pg-2=r pg-3=r writes ok 1383/s errors 0/s
... (no change for 90 s)
acknowledged 179,999, failed 0With failsafe_mode off, a perfectly healthy primary demoted itself.
For 25 seconds nothing happened (the leader key's TTL was still running),
then pg-1 could no longer prove it held the key and turned itself into a
replica. For 36 seconds there was no primary at all, until etcd came
back and pg-1 promoted again within about 2 seconds. That is the design
working: a primary that can't reach the referee must assume it might have
been replaced.
With failsafe_mode on: zero errors. When etcd is unreachable,
Patroni asks the other members directly over its REST API; if it can reach
all of them and they all accept it as leader, it keeps running. The
condition matters: if any member is unreachable too, it demotes as before,
because then a real partition is possible.
The trade-off: failsafe mode turns an etcd outage from "writes stop" into
"nothing happens", at the cost of relying on Patroni's own member check
during that window. For a cluster whose etcd is less reliable than its
network, it's worth turning on. The PatroniFailsafeActive alert tells you
it's in effect.
The primary is cut off the network
docker network disconnect isolates pg-1 from everything: clients, the
replicas and etcd.
=== primary cut off the network
t= 0.1s pg-1=P pg-2=r pg-3=r writes ok 2204/s errors 0/s
t= 1.2s pg-1=P pg-2=r pg-3=r writes ok 0/s errors 0/s
t= 15.1s pg-1=- pg-2=r pg-3=r writes ok 0/s errors 72/s
t= 26.9s pg-1=- pg-2=P pg-3=r writes ok 0/s errors 73/s
t= 28.0s pg-1=- pg-2=P pg-3=r writes ok 1586/s errors 17/s
reconnecting pg-1
t= 57.0s pg-1=r pg-2=P pg-3=r writes ok 1354/s errors 0/s
acknowledged 116,523, lost 0
pg-1 replica streaming timeline 12
pg-2 leader running timeline 12- Writes stopped at once (nobody could reach pg-1), and for the first
few seconds there weren't even errors: in-flight connections simply hung
until timeouts fired. Hanging is worse than failing, which is why
clients need
connect_timeoutand statement timeouts. - At 15 s pg-1 stopped acting as primary (it couldn't renew the key), and at 27 s pg-2 was elected, after the key had expired. The order is what prevents split brain: the old primary gave up before the new one took over. Writes resumed about 28 s after the cut.
- When pg-1 came back it had a stale timeline. Patroni rewound it and it rejoined as a replica on timeline 12. 0 acknowledged writes lost.
A dead replica fills the primary's disk
Each replica has a replication slot on the primary: a promise to keep every WAL segment the replica hasn't received yet. That's what lets a replica catch up after a restart instead of being rebuilt. It's also a way to fill a disk.
round 1: pg_wal 576 MB; slot pg_1 active=False status=reserved retains 245 MB, safe_wal_size 1817 MB
round 4: pg_wal 1312 MB; slot pg_1 active=False status=reserved retains 980 MB, safe_wal_size 1082 MB
round 5: pg_wal 1312 MB; slot pg_1 active=False status=extended retains 1224 MB, safe_wal_size 837 MB
round 6: pg_wal 1488 MB; slot pg_1 active=False status=extended retains 1469 MB, safe_wal_size 593 MB
pg-1 started again and caught up in 2s
pg_wal after checkpoint 1: 1136 MBWith the replica stopped, the slot held on to every segment: 1.5 GB after
six rounds. wal_status went from reserved to extended once it passed
max_wal_size (1 GB), and safe_wal_size counted down towards
max_slot_wal_keep_size (2 GB). At zero the primary would invalidate the
slot (lost) to protect its disk, and the replica would need a full
re-clone. Without that setting there is no limit: a replica that's
gone for a weekend can fill the primary's disk and stop the database. The
PostgresReplicationSlotRetainingWAL alert fires at 1 GiB.
When pg-1 came back it caught up in 2 seconds. But pg_wal didn't shrink
to its old size: after two checkpoints it stayed at 1,136 MB. Postgres
recycles old segments (renames them for reuse) instead of deleting
them, keeping about max_wal_size worth. The disk space a slot takes
doesn't all come back when the slot catches up.
The disk fills up
The last drill starts a throwaway Postgres with a 300 MB disk and inserts until it's full.
=== disk full (a throwaway Postgres with a 300 MB disk)
the insert failed with 139 MB of data and 144 MB of WAL on the 300 MB disk:
DiskFull: could not extend file "base/5/16384": No space left on device
a read: ok (135000,)
a small insert: ok
DROP TABLE to free space: ok
server status: running- Half the disk was WAL. 139 MB of data, 144 MB of WAL: a database needs room for its log, not just its tables. Size disks (and alerts) for both.
- A full data disk is not a crash. The insert that couldn't extend a
file failed, and only that transaction. Reads kept working, a small
insert fit in space already allocated, and
DROP TABLEfreed space. - A full WAL disk is different. If Postgres can't write WAL it can't
commit anything and stops with a
PANIC. This drill didn't reach that case (data and WAL shared one disk and the data write failed first), so it's documented behaviour here, not a measurement. Production systems putpg_walon its own volume or keep generous headroom, and alert on disk usage well before 100%.
Run these drills on a schedule, not once. The numbers here (25 s until
demotion, 27 s until a new primary after a partition) come straight from
ttl: 30 and loop_wait: 10; change those and the drill tells you what
you actually bought.