In a Patroni cluster, what happens when the primary loses its connection to etcd, and how do failsafe_mode and the watchdog change the split-brain risk?
answer
- cannot tell partition from outage
- demote and stay read-only
- ask every member over REST
- reset the host before the key lapses
basics
~20 sWhen Patroni's primary cannot reach etcd it assumes a partition and demotes PostgreSQL, so an etcd outage costs write availability. failsafe_mode lets it stay primary only if every known member confirms via POST /failsafe; the watchdog resets a node whose Patroni stalls.
solid answer
~50 sFrom one node, an etcd outage and a partition that isolates the primary look identical, and in the partition case the other side may let the key expire and promote. So by default the primary demotes PostgreSQL and stays read-only until the DCS returns. If only the primary was cut off, a replica takes the key once it expires; if etcd itself is down, nobody can take the key and the whole cluster is read-only. With `failsafe_mode: true` the leader maintains a `/failsafe` key listing all members; during a DCS outage it calls `POST /failsafe` on each and keeps running only if all answer, and replicas treat those calls as proof the leader is alive. The watchdog covers what Patroni cannot do itself — a hung or killed agent, a paused VM — by resetting the host `safety_margin` (5) seconds before the key expires.
code
yaml · 5 lines# patroni.yml on each payment database host (local configuration)
watchdog:
mode: required
device: /dev/watchdog
safety_margin: 5go deeper
Remember that Patroni needs its DCS: if the primary cannot reach etcd, it stops being primary rather than risk two writers.
Distinguish a full etcd outage from an isolated primary, and say what the replicas can and cannot do in each case.
Explain failsafe_mode's all-members rule, the watchdog's modes and safety_margin, and when each is worth enabling for a critical cluster.
Weigh write availability against split-brain risk across shared DCS infrastructure, and decide who owns DCS reliability for every Patroni cluster.
## Two failures that look the same Patroni may run PostgreSQL as primary only while it can update the leader key in the DCS. When an update fails, the cause is one of two things: the network is **partitioned** and this node is on the wrong side, or the **DCS is down**. A single node cannot distinguish them, so Patroni assumes the worse one. In a partition, the other side may already see the key expire, acquire it and promote a replica; the old primary must therefore stop accepting writes before its key lapses. ## Default behaviour, case by case | Situation | Old primary | Replicas | Outcome | |---|---|---|---| | Whole etcd cluster down | demotes ('DCS is not accessible and I was a leader'), stays read-only | cannot acquire the key | no primary until etcd returns | | Primary cut off from etcd and replicas | demotes | key expires; healthiest eligible replica promotes | one primary, on the majority side | | Primary cut off from etcd only | demotes | key expires; a replica promotes | one primary, an unnecessary failover | | etcd quorum lost on the primary's side | its etcd writes fail, so it demotes | promote if their etcd side has quorum | one primary at most | The default is safe but costly: a shared or flaky DCS turns into lost write availability for every Patroni cluster that depends on it. The documentation notes that an etcd used only by Patroni rarely causes this, while the Kubernetes API as a DCS causes it more often. ## failsafe_mode `failsafe_mode` (default `false`) is a dynamic setting, enabled with `patronictl edit-config -s failsafe_mode=true` once every member runs a version that supports it. It works like this: 1. The current leader maintains a permanent `/failsafe` key listing every known member and its REST API URL. 2. Only members present in `/failsafe` may take part in the leader race. 3. When the DCS becomes unreachable, the primary calls `POST /failsafe` on every member in that list. 4. If **all** of them respond, it keeps running as primary; if any one does not, it demotes. 5. Replicas treat the incoming `POST /failsafe` as proof that the primary is alive, cache that for `ttl` seconds, and do not start a leader race even if the key has expired. It does not apply when the update failed because someone else changed the key (a version or value mismatch): another member owns leadership, and the primary demotes. Why all members, not a majority? The DCS majority and the Patroni majority can sit in different places. If most Patroni nodes, including the primary, end up on the losing side of a partition while the DCS majority is elsewhere, the primary must demote, and only checking every member detects that. ## The watchdog Patroni's normal protection depends on Patroni running. It fails if the agent crashes or is killed, if PostgreSQL shuts down too slowly, or if the VM is paused by the hypervisor. The **watchdog** is the backstop: - Patroni arms a Linux watchdog device (default `/dev/watchdog`; `softdog` works) before promoting, and keeps it alive on every successful key update. - If keepalives stop, the device **resets the whole host**, so a stuck primary cannot keep committing after its key expires. - `watchdog.mode` is `off`, `automatic` (the default: use it if available) or `required` (refuse to become leader without it). - The watchdog fires `safety_margin` seconds before `ttl` expires (default 5, so 25 seconds with `ttl: 30`); `safety_margin: -1` sets it to half of `ttl` for a stricter guarantee. - With defaults, the HA loop has at least 15 seconds (`ttl - safety_margin - loop_wait`) before a reset. ## Putting it together for the payment cluster - Run etcd with three or five members spread over three failure domains, used only by Patroni, so DCS outages stay rare. - Set `watchdog.mode: required` on the database hosts: for payments, a reboot is a better outcome than two primaries. - Turn on `failsafe_mode` if the DCS is shared or known to wobble, accepting that one unreachable member is enough to demote during an outage. - Fencing in general (STONITH and its alternatives) is a separate, engine-neutral topic; Patroni's own contribution is demotion before the key lapses, failsafe checks and the watchdog.
- Why does Patroni's failsafe mode require the primary to reach all members rather than a majority?The DCS's view of a majority and Patroni's can differ: etcd members are spread across zones, but nothing forces Patroni nodes to be. If most Patroni nodes, including the primary, sit on the losing side of a partition while the DCS majority is elsewhere, the primary must demote. Only checking every member detects that case.
- With watchdog mode automatic and no watchdog device present, what does Patroni do?It runs without one: `automatic` uses a watchdog when available and ignores its absence, and 4.1 logs the activation failure only at debug level. The split-brain backstop is then silently missing, which is why payment-grade hosts should use `mode: required`, so a node that cannot arm the watchdog refuses to become leader.
- If etcd goes down, Patroni keeps the primary running as long as PostgreSQL is healthy.
- failsafe_mode keeps the primary running if a majority of members respond.
- During a full etcd outage a replica promotes once the leader key's ttl passes.
- The watchdog is what normally demotes the primary when the lock is lost.
- Two etcd members, one per data centre, avoid split brain for Patroni.