For a payment gateway's PostgreSQL on Patroni across two data centres, would you stretch one Patroni cluster with a third-site etcd member or run a standby cluster, and why?
answer
- the DCS majority decides
- two sites cannot break a tie
- a witness in a third place
- manual promotion of a standby cluster
basics
~20 sWith a third site for one etcd member, stretch one Patroni cluster and replicate synchronously across sites for automatic, loss-free failover. With only two sites, run a standby cluster in the second and promote it manually, accepting slower recovery.
solid answer
~40 sPatroni fails over automatically only where its DCS keeps a majority; with etcd split two and one across two sites, losing the site with two members stops writes everywhere. If I can place one etcd member in a third location — a small VM elsewhere, no PostgreSQL needed — I stretch one cluster: etcd 1+1+1, two PostgreSQL nodes in DC1 and one in DC2, `synchronous_mode: on` or `quorum` so an acknowledged payment is on two nodes, `synchronous_mode_strict` only if refusing writes beats any unreplicated commit, and a required watchdog. If a third site is impossible, DC2 runs a Patroni standby cluster with its own etcd, promoted with `patronictl promote-cluster` after DC1 is fenced; Patroni's documentation says automatic promotion is impossible there. The trade is recovery time against cross-site commit latency and operational complexity.
code
yaml · 7 lines# Dynamic configuration of the DC2 cluster (paygw-dc2): replicate from DC1
standby_cluster:
host: paygw-dc1-write.internal
port: 5432
primary_slot_name: paygw_dc2
create_replica_methods:
- basebackupgo deeper
Remember that Patroni needs a majority of its etcd members to fail over, so an odd number of members across locations matters.
Explain why two sites cannot host a majority-safe DCS, and what a Patroni standby cluster is and how it is promoted.
Lay out both designs with concrete settings: sync placement, watchdog, failsafe, and the promote-cluster and demote-cluster runbook.
Own the trade between RTO, RPO and commit latency with the business, justify a third site's cost, and set the triggers for revisiting the topology.
## The constraint: the DCS majority Patroni can promote a node only by acquiring the leader key, and the DCS can grant it only while a majority of its own members agree. Patroni's documentation asks for an odd number of etcd, ZooKeeper or Consul nodes — 3 or 5. With two data centres every placement has a site that holds the majority, and losing that site leaves the other one unable to elect anyone. That is a property of the DCS, not something PostgreSQL settings can fix, so the first decision is whether a third location exists. ## Option A: one stretched cluster with a third-site witness - **DCS:** three etcd members, one per location; the third can be a small VM in another region or provider, since it stores only Patroni's keys. - **PostgreSQL:** `pay-dc1-a` and `pay-dc1-b` in DC1, `pay-dc2-a` in DC2, all in one Patroni cluster `paygw`. - **Durability:** `synchronous_mode: on` or `quorum`, so a payment acknowledged to the gateway exists on at least two nodes and Patroni only promotes a node holding it. - **Availability:** losing any one site keeps the etcd majority, so failover stays automatic. - **Safety:** `watchdog.mode: required` on every database host, and possibly `failsafe_mode` if the WAN between sites is unreliable. - **Cost:** each commit waits for a synchronous standby; if that standby is in the other site, every payment pays a cross-site round trip. ## Option B: a standby cluster in the second site When there are only two sites, Patroni's multi-datacentre guidance is two **independent** Patroni clusters, each with its own etcd: DC1 is the normal cluster, DC2 is a **standby cluster** whose standby leader replicates from DC1 (configured by a `standby_cluster` section with `host`, `port` and `primary_slot_name`). Failover across sites is manual: 1. Confirm DC1 is really down or fence it (STONITH): promoting while DC1 still writes creates two primaries with diverging timelines. 2. Note DC2's last received LSN to estimate which payments may be missing. 3. Run `patronictl promote-cluster paygw-dc2` (Patroni 4.1), which removes the `standby_cluster` section and waits until the leader runs as primary. Do not use `pg_ctl promote`, which bypasses Patroni. 4. Repoint clients, for example by switching the write endpoint's DNS or load-balancer target. 5. When DC1 returns, make it the standby: `patronictl demote-cluster paygw --host … --port …`, which may need `pg_rewind` or a rebuild. The documentation is explicit that automatic promotion is impossible in this layout, because DC2 can never learn the state of DC1. ## Where to put the synchronous standby | Choice | Commit latency | Survives losing DC1 without losing payments | |---|---|---| | Sync standby in DC1 | local round trip | no: both copies are in DC1 | | Sync standby in DC2 | cross-site round trip | yes | | Quorum `ANY 1` over DC1 and DC2 replicas | fastest replica, usually DC1 | no guarantee: the ack may come from DC1 only | | Quorum `ANY 2` over both replicas | slowest of the two | yes, but one replica outage stalls or degrades writes | In `on` mode, the `sync_priority` tag (4.1) lets you prefer the DC2 node as synchronous standby, and `failover_priority` lets you prefer promoting `pay-dc1-b` over the remote node when both are equally caught up. ## What I would choose - **Third location available:** Option A with the synchronous standby in DC2. For a payment gateway, a few milliseconds per commit is cheaper than reconciling lost payments after a site failure. - **No third location:** Option B, with asynchronous replication to DC2, `synchronous_mode` inside DC1 for node failures, and a rehearsed promote-cluster runbook with an agreed RTO. - **Avoid:** two etcd members, or four split two and two — either layout loses its majority when one site fails or when the link between the sites is cut. - **Rehearse:** whichever option you pick, run a scheduled `patronictl switchover` to the DC2 node (Option A) or a full promote-and-demote drill (Option B) before you rely on it, and measure the real recovery time. - **Revisit** when cross-site latency changes, when the business sets a stricter RTO or RPO, or when a third site becomes cheap. The general theory of voting members and witnesses across sites is engine-neutral; this answer is about how Patroni's DCS, sync modes and standby clusters implement it.
- In the standby-cluster design, what must you check before running patronictl promote-cluster in DC2?That DC1's primary can no longer accept writes: powered off, cut off from clients, or demoted. Promoting while it still runs creates two primaries with diverging timelines, which is why Patroni's documentation calls for STONITH first. Also record DC2's last received LSN to estimate missing payments, and keep clients from reaching DC1 when it comes back.
- Why is a synchronous standby in DC1 cheaper but riskier for the payment gateway?A same-site standby adds only a local round trip to each commit, but if DC1 is lost both copies of the latest payments go with it and the DC2 replica may be behind. A DC2 synchronous standby makes every commit wait for a cross-site round trip, yet losing DC1 then costs no acknowledged payment.
- Two etcd members, one per data centre, are enough for automatic cross-site failover.
- A Patroni standby cluster in DC2 promotes itself when DC1 fails.
- Promote the DC2 standby leader with pg_ctl promote.
- Quorum mode removes the need for a third site.
- A synchronous standby in the same site protects against losing that site.
Assumes Patroni 4.1, whose patronictl demote-cluster and promote-cluster commands script the standby-cluster conversion that earlier releases did by editing the standby_cluster section by hand.