Patroni is good at electing a new leader. What it cannot do on its own is make a stale replica current. If the replica that wins the election is 200 MB of WAL behind, every transaction in those 200 MB is gone the moment it is promoted. The application gets a working primary and a silent hole in its data.
The setting that stands between you and that hole is maximum_lag_on_failover. This post covers what it does, how to choose a value, how to check lag yourself before a planned switchover, and what to do when the only replicas left are the lagging ones.
What maximum_lag_on_failover actually does
maximum_lag_on_failover is a cluster-wide Patroni setting, in bytes. When the leader disappears, each replica compares its own WAL position with the last leader position Patroni recorded in the DCS. A replica that is further behind than the limit will not try to take the leader lock. The Patroni default is 1048576 bytes, which is 1 MiB.
Two details matter in practice:
- The comparison uses the last position the leader published, which is refreshed on each Patroni loop. Lag that built up in the final seconds before a crash can be larger than what the check sees.
- The limit applies to automatic failover. A manual switchover or failover started with
patronictlis a separate path, so the operator still has to look at lag before choosing a target.
Check the current value with:
patronictl -c /etc/patroni/patroni.yml show-config | grep maximum_lag_on_failoverChange it cluster-wide (it lives in the DCS, not in each node's YAML):
patronictl -c /etc/patroni/patroni.yml edit-config \
-s maximum_lag_on_failover=1048576Choosing a value
There is no universally right number. It is a trade between two failure modes.
- Small limit (for example 64 KiB to 1 MiB): Patroni prefers no leader over a leader that lost writes. During a bad moment, such as a replica that fell behind during a bulk load, the cluster can stay without a primary until a human decides.
- Large limit (tens or hundreds of MB): the cluster almost always comes back on its own, but the amount of data you can lose on failover grows with it.
A practical way to pick: look at your normal peak replication lag over a week, then set the limit a bit above it. If you regularly exceed 1 MiB during batch jobs, either raise the limit knowingly or fix the batch job. If you cannot lose any committed write at all, the answer is not a smaller limit but synchronous replication (synchronous_mode: true in Patroni), with its latency cost.
Check lag before a planned switchover
patronictl list shows a lag column per member, which is the quickest view:
patronictl -c /etc/patroni/patroni.yml listFor exact numbers, ask the leader:
SELECT application_name,
state,
pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes,
replay_lag
FROM pg_stat_replication
ORDER BY replay_lag_bytes DESC;And on a replica, compare what it received with what it replayed:
SELECT pg_wal_lsn_diff(pg_last_wal_receive_lsn(), pg_last_wal_replay_lsn()) AS not_yet_replayed_bytes,
now() - pg_last_xact_replay_timestamp() AS replay_delay;A replica with a large receive-but-not-replayed gap has the WAL on disk but is busy applying it, often because of a long query holding a conflict or slow storage. A replica with a large gap on the leader side is not receiving fast enough, which usually points at the network or at the leader's WAL sender.
Then run the switchover to a named candidate:
patronictl -c /etc/patroni/patroni.yml switchover --leader pg1 --candidate pg2Also check member tags. A node tagged nofailover: true will never be promoted, which is what you want for a reporting replica or a node in another region, but it is easy to forget when you are counting how many safe targets you have.
When only lagging replicas are left
Sometimes the leader is gone and every surviving replica is over the limit. Patroni will, correctly, refuse to promote. You now have three options, in this order:
- Recover the old leader. If the host or disk can come back, that is the only path with no data loss.
- Raise the limit temporarily or promote a named replica by hand, accepting the loss.
- Write down what was lost. Record the WAL position difference between the old leader's last known position and the promoted replica, plus the time gap, so the application team can reconcile.
Step 3 is the one teams skip, and it is the one that matters a week later when someone finds a missing order.
How this looks in Jarwis ClusterOps
On a PostgreSQL Patroni cluster, the cluster Overview tab has a Patroni summary and a Switchover dialog. Each cluster has a maximum failover lag, 1 MiB by default, with an allowed range of 64 KiB to 1 TiB. You can change it from the Switchover dialog or through the cluster settings API (pg_max_failover_lag_bytes), and Jarwis writes it live to Patroni's maximum_lag_on_failover.
The Switchover dialog lists every member with its replay lag and a verdict: eligible, or the reason it is not (lag exceeds threshold, lag unknown, not running, unreachable or the nofailover tag), read live from each member's Patroni. If you leave the target on auto, Jarwis picks the eligible replica with the least replay lag. A candidate that lags beyond the limit, or one that is stopped, unreachable or tagged nofailover, is refused before anything starts, with the lag and the limit in the message.
For the case where the leader is gone and only lagging replicas remain, an administrator can force a promotion of a named replica. It needs an explicit acknowledgement that data will be lost. Jarwis writes the estimated loss (WAL position difference in bytes and the time gap) to the task log, raises a critical forced promote alert that stays open until you resolve it, and records the action in the audit log. Forced promote only overrides a refusal caused by lag.
All of this runs from Jarwis Cloud through a small outbound agent on each database server, so there are no inbound ports to open. Lite is free up to three nodes, enough for a three-node Patroni cluster. The details are in the WAL-lag failover protection docs and high availability and failover, and there is a read-only demo at demo.jarwis.com.