skip to content

In Patroni 4, how do maximum_lag_on_failover, synchronous_mode (on or quorum) and synchronous_mode_strict change what an automatic failover can lose and when writes stall?

level: seniorimportance: should knowfreq 42%

answer

  1. bytes behind the last known position
  2. 1 MiB unless you set it
  3. the /sync key decides who may promote
  4. ANY n instead of FIRST n

basics

~20 s

Asynchronously, a replica over maximum_lag_on_failover bytes (default 1 MiB) behind cannot promote, bounding loss without removing it. synchronous_mode on or quorum lets only replicas holding every acknowledged commit promote; synchronous_mode_strict blocks writes rather than run unreplicated.

solid answer

~40 s

By default Patroni replicates asynchronously. A replica leaves the leader race when it is more than `maximum_lag_on_failover` bytes (1048576 by default) behind the last WAL position the leader wrote to the DCS; that position is refreshed once per loop, so the documented worst-case loss is that many bytes plus whatever was written in the last `ttl` seconds. `synchronous_mode: on` makes Patroni manage `synchronous_standby_names` from `synchronous_node_count` (default 1) and record the sync standbys in the `/sync` key; only they may promote automatically. `synchronous_mode: quorum`, new in 4.0, writes an `ANY n (...)` list and promotes a node once enough voters confirm it is not behind, which smooths commit latency. If no sync standby is left, Patroni falls back to unreplicated commits unless `synchronous_mode_strict` is on, in which case writes block.

code

yaml · 7 lines
yaml
# Dynamic configuration for the three-node payment cluster
# (patronictl -c /etc/patroni/patroni.yml edit-config paygw)
synchronous_mode: quorum
synchronous_node_count: 1
synchronous_mode_strict: false
maximum_lag_on_failover: 1048576
check_timeline: true

go deeper

for a junior

Remember that Patroni is asynchronous by default and that synchronous_mode exists to avoid losing acknowledged commits at failover.

for a middle

Explain maximum_lag_on_failover in bytes, the /sync key, synchronous_node_count, and the difference between FIRST and ANY lists.

for a senior

Walk through a sync standby crash, the standalone fallback versus strict mode, quorum's latency benefit, and the cases where zero loss still fails.

for a principal

Set the durability policy per service: which data justifies strict mode's write stalls, and how many nodes and sites that implies.

## Asynchronous by default: maximum_lag_on_failover Out of the box Patroni sets up **asynchronous** streaming replication: the primary acknowledges a commit before any standby has it. A failover can therefore lose commits, and `maximum_lag_on_failover` is the bound Patroni puts on that loss. - On every HA loop the leader writes its current WAL position into the DCS (`/status`). - When the leader key expires, each replica compares its own WAL position with that value. If the difference exceeds `maximum_lag_on_failover`, it logs 'My wal position exceeds maximum replication lag' and stays out of the race. - The default in the 4.1 source is **1048576 bytes (1 MiB)**. - Because the leader's position is sampled, not live, the documented worst case is `maximum_lag_on_failover` bytes **plus** whatever was written in the last `ttl` seconds (about `loop_wait`/2 seconds on average). - `check_timeline: true` also excludes replicas on an older timeline than the former primary, and the `failover_priority` tag decides between replicas that have received or replayed the same WAL. If no replica qualifies, nobody is promoted: availability is traded for a bound on loss. Choosing the value is a business decision expressed in bytes. A larger limit lets a lagging replica take over after a burst of writes, at the price of more lost commits; a smaller one protects data but can leave the cluster without a primary after a heavy batch. Watch the receive and replay lag that `patronictl list` shows during your busiest hour before you pick it. ## synchronous_mode: on With `synchronous_mode: on` Patroni takes over PostgreSQL's `synchronous_standby_names` and keeps state in the DCS `/sync` key: the leader plus the current synchronous standbys. Its invariants: 1. A node that can accept writes is always recorded as the latest leader. 2. A node published as synchronous in `/sync` is set as synchronous in PostgreSQL. 3. Only the last leader or a listed synchronous standby may promote itself automatically. `synchronous_node_count` (default 1) is how many sync standbys to keep, reduced automatically if fewer are eligible; with 2 the leader gets `FIRST 2 (node1,node2)`. The `nosync` tag, or since 4.1 a `sync_priority` of 0 or less, keeps a member from being chosen; a higher `sync_priority` makes it preferred. `maximum_lag_on_syncnode` (default -1, disabled) lets Patroni swap out a sync standby that falls too far behind. When the only sync standby crashes, commits block until Patroni's next loop switches the primary to **standalone** by clearing the list — at worst `ttl` seconds, on average `loop_wait`/2. A standby that is shut down cleanly asks to be released first, so planned restarts do not stall commits. ## synchronous_mode: quorum Added in Patroni 4.0, **quorum mode** writes an `ANY n (...)` list containing every eligible standby once it has caught up, and stores a `quorum` number in `/sync`. A commit needs acknowledgement from any `n` of them, so one slow standby is compensated by the others. At failover a candidate must show, by comparing LSNs with enough voters, that it is not behind the latest acknowledged commit. `synchronous_node_count`, `maximum_lag_on_syncnode` and `synchronous_mode_strict` work as in `on` mode; `failover_priority` currently does not work in quorum mode. ## synchronous_mode_strict Without it, a primary that has lost all sync standbys keeps accepting writes without replication guarantees, and if it then fails no standby is promoted automatically. With `synchronous_mode_strict: true`, Patroni never drops to standalone: writes **block** until a synchronous standby returns, unless a transaction lowers `synchronous_commit` itself. Use at least three PostgreSQL nodes, so losing one standby does not stop writes. ## Where zero loss still breaks 1. The primary and its only synchronous standby fail together, and a third, asynchronous node is all that is left. 2. A backend waiting for the standby's acknowledgement is cancelled; its transaction becomes visible locally without being replicated. 3. A transaction runs with `synchronous_commit` set to `off` or `local`. 4. An operator runs `patronictl failover` to an asynchronous node and confirms the prompt. ## Summary | Setting | Who may promote automatically | No sync standby left | Commit cost | |---|---|---|---| | async (default) | replicas within `maximum_lag_on_failover` | not applicable | none | | `synchronous_mode: on` | listed sync standbys or the last leader | standalone, unreplicated | slowest listed standby | | `synchronous_mode: quorum` | a node confirmed by the quorum | standalone, unreplicated | fastest `n` standbys | | either, plus `synchronous_mode_strict` | as above | writes block | as above |

  • The payment cluster runs synchronous_mode: on and its only sync standby crashes. What do clients see?
    Commits wait until Patroni's next loop notices. If another eligible replica is connected, Patroni makes it the synchronous standby; otherwise it clears the list and runs the primary standalone, at worst `ttl` seconds later. With `synchronous_mode_strict: true` it never goes standalone, so writes keep blocking until a synchronous standby returns.
  • Why can quorum mode lower commit latency compared with synchronous_mode: on?
    In `on` mode each commit waits for the named synchronous standbys, so a slow one delays every write. Quorum mode lists all eligible standbys as `ANY n`, so a commit returns once the fastest `n` acknowledge. The price is a more careful failover: a candidate must prove, by comparing LSNs with enough voters, that it holds the latest acknowledged commit.

  • maximum_lag_on_failover guarantees zero data loss in asynchronous mode.
  • synchronous_mode: on loses nothing even if the primary and its sync standby die together.
  • strict is a third value of synchronous_mode next to on and quorum.
  • With synchronous_mode on you hand-edit synchronous_standby_names to pick the standby.
  • In quorum mode the member with the highest failover_priority always wins.

Assumes Patroni 4.1 managing PostgreSQL 18; synchronous_mode: quorum arrived in Patroni 4.0 and the sync_priority tag in 4.1.