Reliability

Patroni High Availability for PostgreSQL: Architecture, Config and Failover Testing

How Patroni and etcd deliver automatic PostgreSQL failover: architecture, a minimal configuration, the key timing parameters, split-brain safety and how to test failover.

PostgreSQL has streaming replication, but it does not decide who should be primary when a node dies. Patroni adds that missing piece: a cluster manager that uses a distributed consensus store to elect a single leader and automate failover. Try the failover simulator alongside this guide to see the sequence in action.

1. How the pieces fit

  • PostgreSQL nodes (one leader, several replicas), each running a Patroni agent.
  • A consensus store (etcd, Consul or ZooKeeper; commonly a 3-node etcd cluster) holding the leader key and cluster state.
  • A routing layer (HAProxy, a virtual IP or DNS) that sends clients to the current leader, using Patroni's REST API health checks (for example, the leader endpoint returns success only on the primary).

The leader keeps renewing a lease (a key with a time-to-live) in the store. If it stops, the lease expires and a healthy replica acquires it and is promoted.

2. A minimal configuration

scope: pg-cluster
name: node1

restapi:
  listen: 0.0.0.0:8008
  connect_address: 10.0.1.11:8008

etcd3:
  hosts: 10.0.1.21:2379,10.0.1.22:2379,10.0.1.23:2379

bootstrap:
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
    postgresql:
      use_pg_rewind: true

postgresql:
  listen: 0.0.0.0:5432
  connect_address: 10.0.1.11:5432
  data_dir: /var/lib/postgresql/15/main

Authentication, replication users and extra parameters are omitted for brevity. Follow the Patroni documentation for a complete production file.

3. The timing parameters that matter

  • ttl: how long the leader lease lasts without renewal. Shorter means faster failure detection.
  • loop_wait: how often each agent runs its cycle.
  • retry_timeout: how long to retry the consensus store or PostgreSQL operations.

Patroni requires these to be consistent (roughly, loop_wait + 2 * retry_timeout must not exceed ttl). Aggressive values detect failure sooner but risk false failovers during brief network blips. Check the documentation for allowed minimums and tune with failure testing, not guesswork.

4. Safety: avoiding split-brain

  • The consensus store is the single source of truth. A node that cannot reach it demotes itself rather than continuing as primary.
  • Use a majority-quorum store (3 or 5 nodes) spread across failure domains. Losing quorum stops promotions to protect your data.
  • maximum_lag_on_failover prevents promoting a replica that is too far behind. synchronous_mode trades some write latency for zero data loss on failover.
  • Enable pg_rewind so a former primary can rejoin as a replica without a full re-copy.
  • Consider a hardware or software watchdog so a frozen Patroni process cannot leave an old primary running.

5. Day-to-day operations

patronictl -c /etc/patroni.yml list                # cluster state, roles, lag
patronictl -c /etc/patroni.yml switchover          # planned leader change
patronictl -c /etc/patroni.yml failover            # forced failover
patronictl -c /etc/patroni.yml edit-config         # change DCS settings
patronictl -c /etc/patroni.yml pause               # maintenance mode (no automatic failover)

Always use pause during manual maintenance and resume afterwards. Perform minor-version upgrades as rolling restarts: replicas first, then a switchover, then the old leader.

6. Test failure, do not assume

Run a scripted game day covering:

  1. Kill the PostgreSQL process on the leader.
  2. Stop the Patroni service; reboot the whole node.
  3. Partition the leader from the consensus store.
  4. Take down one etcd member (cluster should keep working) and then two (promotions should stop).
  5. Fill the leader's disk.

For each scenario, record detection time, time until writes succeed, and any data loss. Dashboards for replication lag and WAL growth make the results easy to verify.

7. Client behaviour

Failover only works end to end if applications reconnect. Use connection retry logic, short DNS TTLs or a health-checked load balancer, and keep a connection pooler in mind: a pooler in front of the routing layer must also find the new leader.

Checklist

  1. 3-node consensus store across failure domains.
  2. Health-checked routing to the leader.
  3. Tuned, documented ttl / loop_wait / retry_timeout.
  4. pg_rewind on, lag limit set, synchronous mode considered.
  5. Regular failure drills with recorded timings.

Enjoyed this?

I write about PostgreSQL, AWS and database reliability. Have a question or a similar problem to solve?