Skip to content

Patroni quarterly failover drill — procedure, timings, sign-off

A repeatable quarterly drill for Patroni-managed Postgres clusters. What to test, the expected timings, and the sign-off criteria that converts HA from hope into evidence.

Six months ago you built a three-node Patroni cluster. The dashboards are green, replicas are zero-lag, and nobody has touched a switchover button since the day you deployed it. Then a hypervisor panics during a Tuesday afternoon, Patroni elects a new leader, and the application takes nine minutes to reconnect because nobody noticed that pgBouncer’s server_check_query was timing out against the demoted primary. The cluster did its job. The plumbing around it didn’t. A quarterly drill — one calendar hour, a fixed checklist, a recorded outcome — is the only practice that catches this before the real one.

What’s happening

A Patroni failover drill is not “click switchover and watch the dashboard.” It tests four things that all have to work for an unplanned event to be a non-incident:

  • Leader election. Does etcd reach quorum and Patroni promote a healthy replica within the configured ttl window (typically 30s).
  • Router convergence. HAProxy must mark the demoted primary down and the new leader up within the health-check interval (3s on a typical setup × 3 fall = 9s worst-case).
  • Pooler reconnection. pgBouncer holds server connections that pointed at the old leader. They must close cleanly via on-marked-down shutdown-sessions and the pooler must re-establish pools to the new write IP.
  • Application recovery. The application’s connection pool must drop dead sockets and pick up new ones from pgBouncer. Some drivers cache DNS or pool entries for minutes by default — this is where most “we failed over but the site was still down” stories actually live.

A drill verifies all four in production-shaped traffic, not in an empty test cluster. The sign-off criterion is total user-visible write-path interruption of under 30 seconds. Anything more and something in the chain needs fixing before the next quarter.

How to verify

Pre-drill snapshot. Capture the baseline so you can compare:

# Cluster state
patronictl -c /etc/patroni/patroni.yml list

# Replication lag per replica
patronictl -c /etc/patroni/patroni.yml history

# HAProxy backends
echo "show stat" | socat stdio /run/haproxy/admin.sock \
  | awk -F, '$1 ~ /^postgres_/'

# pgBouncer pool state
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer -c "SHOW POOLS;"

# Application connection count by source
psql -h <writer> -U postgres -c \
  "SELECT client_addr, state, count(*) FROM pg_stat_activity GROUP BY 1,2 ORDER BY 3 DESC;"

A failover drill with zero application traffic tells you nothing useful. Run a synthetic write load during the drill — even a simple loop is enough to measure the gap:

# Synthetic write load that prints a line per second with success/fail
while true; do
  T=$(date -Iseconds)
  if psql -h pgbouncer.internal -p 6432 -U app -d app -c \
       "INSERT INTO drill_heartbeat(ts) VALUES (now())" >/dev/null 2>&1; then
    echo "$T ok"
  else
    echo "$T FAIL"
  fi
  sleep 1
done | tee /tmp/drill-$(date +%F-%H%M).log

The count of FAIL lines is your write-path outage in seconds. That’s the number the drill is measuring.

The fix

Run this checklist every quarter. Schedule it. Block the calendar. Don’t skip it because “things are quiet.”

1. Pre-flight (T-15 min)

# Confirm all nodes are healthy and on the same timeline
patronictl -c /etc/patroni/patroni.yml list

# Confirm zero lag on replicas
psql -h <leader> -U postgres -c "
  SELECT application_name,
         pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS lag_bytes,
         sync_state
  FROM pg_stat_replication;"

# Confirm WAL archiving is current
psql -h <leader> -U postgres -c "SELECT * FROM pg_stat_archiver;"

# Confirm a backup taken in the last 24h is restorable (don't restore — verify the manifest exists)
ls -la /var/backups/postgres/$(date +%F)/

If any check fails, stop. Fix it and reschedule. A drill on an already-degraded cluster gives you garbage data.

2. Start synthetic load and timer (T-0)

# Terminal 1 — synthetic load (see "How to verify" above)
./drill-load.sh > /tmp/drill.log 2>&1 &

# Terminal 2 — watch cluster state
watch -n 1 'patronictl -c /etc/patroni/patroni.yml list'

# Terminal 3 — watch HAProxy
watch -n 1 'echo "show stat" | socat stdio /run/haproxy/admin.sock \
  | awk -F, "\$1 ~ /^postgres_/ {print \$1,\$2,\$18,\$36}"'

Note the wall-clock time. This is T-0.

3. Graceful switchover (T+0)

patronictl -c /etc/patroni/patroni.yml switchover \
  --master <current-leader> \
  --candidate <intended-new-leader> \
  --force

Expected timing on a tuned cluster:

  • Leader switched message in patronictl: 2-5 seconds
  • HAProxy postgres_write backend converged on new leader: 5-10 seconds
  • First successful write through pgBouncer to new leader: 10-15 seconds
  • All replicas re-attached to new leader, lag zero: 15-30 seconds

Total user-visible write-path interruption: target under 15 seconds.

4. Unplanned-failure simulation (T+10 min, only after step 3 passes)

Graceful switchover tests the happy path. Unplanned failure tests the angry path. Pick one node — not the one currently leading after step 3 — and simulate a hard failure on the current leader:

# On the current leader, simulate VM-level death by killing patroni AND blocking the port
sudo systemctl kill -s SIGKILL patroni
sudo iptables -A INPUT -p tcp --dport 5432 -j DROP
sudo iptables -A INPUT -p tcp --dport 8008 -j DROP

Expected timing:

  • Patroni ttl expires on etcd, replica detects: 20-30 seconds (matches ttl: 30 in patroni.yml)
  • Replica promoted: 30-35 seconds
  • HAProxy converges: 35-45 seconds
  • Synthetic load returns to ok: 45-60 seconds

Target for unplanned: under 60 seconds of write-path interruption.

Restore the killed node after the drill:

sudo iptables -D INPUT -p tcp --dport 5432 -j DROP
sudo iptables -D INPUT -p tcp --dport 8008 -j DROP
sudo systemctl start patroni
# Watch it rejoin as a replica
patronictl -c /etc/patroni/patroni.yml list

It should auto-rewind via pg_rewind (configured in bootstrap.dcs.postgresql.use_pg_rewind: true). If it doesn’t, that’s a separate finding — record it and triage after the drill.

5. Sign-off criteria

The drill is passed if all of these are true:

  • Graceful switchover write-path gap was under 15 seconds.
  • Unplanned failover write-path gap was under 60 seconds.
  • The failed node rejoined as a replica without operator intervention beyond restarting the daemon.
  • Replication lag on the rejoined replica returned to zero within 5 minutes.
  • pgBouncer SHOW POOLS shows healthy sv_active connections to the new writer within 30 seconds.
  • No alerts fired that were not expected (paging an on-call for the drill itself is fine; paging because monitoring is misconfigured is a finding).

The drill is failed if any criterion is missed. Record the gap, file a ticket against the responsible component, and re-run the drill after the fix. Do not let “almost passed” pass.

6. Recording the run

A drill that isn’t recorded didn’t happen. Append to /var/log/patroni-drills.log on the monitoring host:

cat >> /var/log/patroni-drills.log <<EOF
================================================================
Drill date:        $(date -Iseconds)
Operator:          $(whoami)
Cluster:           pgcluster
Graceful gap:      __ seconds
Unplanned gap:     __ seconds
Rejoin time:       __ minutes
Pass/Fail:         __
Findings:
  -
  -
EOF

Four entries per year. After a year, you have a trend line. Drift in the gap — say, graceful went from 8s to 22s over three quarters — tells you something in the chain is degrading and you should investigate before it becomes the incident.

For clusters we operate, the drill is on our calendar and the log goes into the customer’s runbook. Run this stack under our Cloud Compute tier with the drill baked into our Managed Cloud Ops — or book a scoping call if you’d like us to take over the cadence.