Learn Labs
Replication & Scaling

The Failover Drill

Switchover, clean stop, kill -9, and a kill while replicas lag: downtime and lost writes, measured from the application's side

"Automatic failover" is a claim. labs/failover.py checks it the way an application would experience it: four threads insert rows through PgBouncer as fast as they can (about 2,000 a second), each row with an id the client chose, and the client records which inserts it was told succeeded. Then the primary is taken away.

conn.execute("INSERT INTO failover_log (id) VALUES (%s)", (row_id,))acked.add(row_id)COMMIT returned OK: the client believes it's stored...stored = SELECT id FROM failover_logafterwards, on the new primarylost = acked - storedtold "OK", but gonein_doubt = failed & storedtold "error", but committed anyway
docker compose -f compose_prod.yaml run --rm labs python failover.py            # all five
docker compose -f compose_prod.yaml run --rm labs python failover.py kill       # one

Three ways to lose a primary

0102030switchover2.8 sdocker stop3.2 skill -923 s: leader key must expirestop, before fix15 s: server_login_retry
=== switchover
 t=  4.3s  leader: none
 t=  4.5s  leader: pg-3
 errors from t=3.1s to t=5.9s
 acknowledged 38,990, failed 28; lost 0, in doubt 0
=== stop
 t=  1.3s  leader: none
 t=  1.5s  leader: pg-2
 errors from t=0.1s to t=3.3s
 acknowledged 32,704, failed 44; lost 0, in doubt 0
=== kill
 t= 22.4s  leader: pg-3
 errors from t=0.0s to t=23.0s
 acknowledged 35,000, failed 1,345; lost 0, in doubt 0
 pg-2 rejoined as a replica in 4s (pg_rewind)
  • Switchover (planned): 2.8 s of errors. Patroni stops the old primary cleanly, waits for the candidate to receive everything, promotes it. This is how upgrades and maintenance should move the primary.
  • docker stop: 3.2 s. A clean shutdown: Postgres stops, and Patroni deletes the leader key on its way out, so the replicas don't wait for the TTL.
  • kill -9: 23 s. The node vanishes with its Patroni, nobody deletes the key, and the replicas must wait for it to expire (TTL 30 s, minus however long ago it was last renewed). Crashes, kernel panics and lost power look like this. Lower ttl fails over faster and risks failing over on a short network blip; 30 s is Patroni's default.

No acknowledged write was lost in any of them. The killed primary came back as a replica in 4 seconds through pg_rewind.

The 15 seconds that weren't Patroni's

The first run of the stop scenario looked like this:

=== stop
 t=  1.2s  leader: none
 t=  1.4s  leader: pg-2
 errors from t=0.1s to t=15.1s
     691 x OperationalError: ... server login has been failing, cached error: the database system is shutting down

Patroni had a new primary in 1.4 seconds, but clients got errors for 15. PgBouncer's log had the answer: after a server login fails, PgBouncer stops trying for server_login_retry seconds (default 15) and hands every client the cached error. The first reconnection attempt hit the old primary while it was shutting down, and that error was served for 15 seconds. server_login_retry = 1 brought it to 7.3 s.

The rest was HAProxy. With inter 2s rise 2, it needed two successful checks two seconds apart before routing to the new primary: about 4 seconds after promotion. Patroni's /primary is authoritative (it answers 200 only on the leader), so one success is enough: inter 1s rise 1 brought it to 3.2 s. Two defaults, one in each proxy, were 80% of the outage.

Killing a primary that's ahead of its replicas

On one host the replicas are microseconds behind, so even kill -9 lost nothing. To see the async loss window, the lab delays WAL from the primary to the replicas by 50 ms (tc netem, as in the replication lab), writes at full speed, and kills it.

async: acknowledged, then lost (7 runs, about 35,000 writes each)run 156run 20run 34run 42run 516run 649run 741synchronous_mode (17,154 acknowledged)acknowledged, then lost0error, but committed (in doubt)4
=== kill-lagging      (first run)
 acknowledged 36,275, failed 1,427; lost 56, in doubt 0
=== kill-lagging      (six more runs)
 lost 0, 4, 2, 16, 49, 41
=== kill-sync
 primary pg-2; writing 66 rows/s; fault at t=0
 acknowledged 17,154, failed 1,643; lost 0, in doubt 4
  • Async: 0 to 56 writes lost, median 16, across seven runs. Each lost write was acknowledged to the client, so the application believed it was stored, but it was in the WAL that hadn't reached a replica when the primary died. At about 2,400 rows a second, 50 ms of delay keeps roughly 120 rows in flight; how many of them had already been acknowledged depends on exactly where the kill lands. The first run lost 56 and the second lost 0, which is why this lab was then run five more times: a single run would have given either a scary or a reassuring answer, and both would have been wrong. If they were payments, they are gone.
  • synchronous_mode: 0 lost. Every acknowledged commit was already on the sync replica. The price was throughput: with each commit waiting 100 ms for the round trip, the four writers managed 66 rows a second instead of 2,326.
  • 4 writes "in doubt". The client got an error, but the rows exist. They had committed (and replicated) when the primary died, before the OK reached the client. No database setting fixes this: when a commit fails with a connection error, the application can't know the outcome. Idempotent writes (a client-chosen id, as here, with retries that check for it) are the only real answer.

On this page