Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Design high availability for a PostgreSQL cluster that backs payments: RPO zero, failover within 30 seconds, and two availability zones plus a third for quorum.

I would run a primary in one zone and two standbys, with synchronous_standby_names = 'ANY 1 (...)' so every commit is on disk in a second zone before it returns, which gives RPO zero. Patroni, with an etcd quorum spread over all three zones, decides failover through a leader lease and fences the old primary, which avoids split brain and fits a 30-second target. Clients reach whoever is primary through a stable endpoint or a multi-host JDBC URL, and the application is built to retry idempotent work, because a design like this is only as good as its tested failovers.

Why this question is asked

A payments database is where high availability stops being a nice extra. Losing a committed payment is not acceptable, and being down for minutes means failed checkouts. Interviewers ask this design question to see whether we can turn two numbers, RPO zero and 30-second failover, into concrete settings, and whether we remember the parts outside the database: split brain, client reconnection, and the application's behaviour during the switch. In this answer, we will recap what RPO and RTO mean, place the nodes, configure synchronous replication, add failover management, route the clients, and finish with how to prove it works.

1A Quick Recap: RPO, RTO and Quorum

RPO, the recovery point objective, is how much committed data we may lose in a failure. RPO zero means none: if the database told the application a payment was committed, that payment must survive the loss of any one server or zone. RTO, the recovery time objective, is how long we may be unavailable; here it is 30 seconds.

Quorum means a majority vote. If two servers each think the other is dead, they cannot settle it between themselves, and both might act as primary. That situation is called split brain, and for payments it is worse than downtime, because two primaries accept different writes that cannot be merged. A third zone gives us a tie-breaker: a majority of three can always be found as long as two zones can talk.

2Placing the Nodes

We put the primary in zone A, one standby in zone B, and a second standby either in zone B or in zone A. The consensus store, etcd with three members, gets one member in each zone, including zone C, which holds no database at all. Zone C exists only to vote.

Step throughHA layout across three zones
  1. ClientsSpring services with a multi-host JDBC URL or a proxy endpoint
  2. Zone Aprimary PostgreSQL + Patroni, etcd member 1
  3. Zone Bstandby 1 PostgreSQL + Patroni, etcd member 2
  4. Zone A or Bstandby 2 PostgreSQL + Patroni
  5. Zone Cetcd member 3 only, the tie-breaker

5 layers, from the top down. Step through them one at a time.

Three etcd members in three zones keep a majority after any single zone fails.

With this layout, losing any single zone leaves a majority of etcd members alive and at least one standby that has every committed transaction, as long as synchronous replication is configured as described next.

3Synchronous Replication for RPO Zero

With asynchronous replication, a commit returns before the standby has the WAL, so a primary crash can lose the last few transactions. For RPO zero, the commit must wait until at least one standby has flushed the WAL to its own disk. That is what synchronous_standby_names with synchronous_commit = on does.

Java
# postgresql.conf on the primary (Patroni manages these values)
synchronous_commit = on
synchronous_standby_names = 'ANY 1 (standby1, standby2)'
# A commit returns once either standby has flushed its WAL.
# Losing one standby does not stop commits; the other confirms.

Notice the ANY 1 form. If we named only one standby, its failure would make every commit wait. With two candidates, either one can confirm, so one standby can be down for maintenance without blocking payments. The cost is latency: each commit now includes a round trip to another zone, typically 1 to 2 ms, which is acceptable for payments. To keep RPO zero across zones, at least one of the two standbys must be in a different zone from the primary, and ideally both candidates are, so the confirming copy is always in another zone.

4Failover Without Split Brain

Patroni runs next to each PostgreSQL server and keeps the cluster state in etcd. The primary holds a leader key with a time to live, the leader lease, and must renew it every few seconds. If the primary dies or loses contact with the etcd majority, the lease expires, and the healthiest synchronous standby takes the key and promotes itself. Settings such as ttl = 20 and loop_wait = 5 put detection plus promotion inside our 30 seconds.

Fencing is what stops the old primary from carrying on. An isolated primary that cannot renew its lease demotes itself to read-only, and a hardware or software watchdog reboots the node if Patroni itself hangs. In Patroni's synchronous mode, only a standby that was synchronous may be promoted, so the new primary is guaranteed to have every committed transaction. A managed service with a consensus-based failover can replace Patroni, as long as it gives the same guarantees.

Step throughZone A fails during traffic
Primary (A)etcd quorumStandby 1 (B)Clients
renew leader lease every 5 s
zone A lost: lease not renewed
lease expired after 20 s
take leader key (I was synchronous)
promote to primary
reconnect: targetServerType=primary
accepting writes, about 25 s after failure

7 messages in order. Play them, or step through them one at a time.

Only a standby that was synchronous may take the lease, so no committed payment is lost.

5Getting Clients to the New Primary

Clients need a stable way to find the primary. We can use a DNS name or a virtual IP that Patroni moves, a proxy such as HAProxy that checks Patroni's REST API to see who is primary, or the PostgreSQL JDBC driver's multi-host support, which tries each host and keeps the one that is primary.

Properties
spring.datasource.url=jdbc:postgresql://pg-a:5432,pg-b:5432,pg-c:5432/payments?targetServerType=primary&connectTimeout=3&socketTimeout=30
spring.datasource.hikari.connection-timeout=5000
spring.datasource.hikari.max-lifetime=600000

The timeouts matter as much as the URL. If the driver waits 2 minutes for a dead host, failover takes 2 minutes no matter how fast Patroni is. So connect and socket timeouts are set well below the failover window, and HikariCP validates connections so broken ones are dropped instead of handed to requests.

6The Application's Part

During failover, in-flight transactions fail and new ones fail for a few seconds. The application must retry, but only work that is safe to repeat. For payments that means idempotency keys: each payment request carries a unique key stored with a unique constraint, so a retried insert either succeeds once or finds the existing row. This also covers the hardest case: a connection that drops during COMMIT, where the application cannot know whether the commit happened. With the key, it checks instead of guessing.

We also need a rule for the standbys being down. If both synchronous candidates fail, commits wait. That keeps RPO zero but stops payments. Patroni's strict synchronous mode chooses this behaviour; without it, Patroni would fall back to asynchronous replication and keep running with a risk of loss. For payments we usually choose to stop, and we write that decision down with an alert, so the on-call engineer knows the system is waiting on purpose.

7Proving It With Regular Failover Tests

A design like this is a claim until it is tested. We run planned switchovers and forced failovers in staging every week and in production every quarter, killing the primary under realistic load. We measure the real numbers: time from failure to first successful write, number of failed requests, and a check that the count of committed payments before and after matches exactly. If the measured gap is 45 seconds, we tune the timeouts and lease settings until it is reliably under 30.

8Follow-up questions

What happens to commits when the only synchronous standby fails?

If the standby list has only one name, every commit on the primary waits for a confirmation that never comes, so writes hang until the standby returns or someone changes synchronous_standby_names. Reads still work. That is why we name two candidates with ANY 1: losing one standby leaves the other to confirm. If all candidates are lost, waiting is the safe behaviour for payments, and a fallback to asynchronous mode should be an explicit, recorded decision, not a silent default.

Why not let the application write to two databases instead?

Because coordinating two independent writes needs distributed transactions or reconciliation, and either is harder to get right than synchronous replication. Letting PostgreSQL ship the WAL keeps one source of truth with one commit order.

Does synchronous replication protect against a bad UPDATE?

No. The bad change is replicated immediately. Protection against mistakes comes from backups with point-in-time recovery, for example base backups plus archived WAL, which is a separate part of the design.

9Conclusion

In this answer, we turned RPO zero and a 30-second failover into a concrete design: a primary and two standbys across zones with synchronous_standby_names set to ANY 1 of two, Patroni with a three-zone etcd quorum, leader leases and fencing to prevent split brain, and promotion of synchronous standbys only. Clients reach the primary through a stable endpoint or a multi-host JDBC URL with targetServerType=primary and short timeouts, the application retries idempotent work using idempotency keys, and we decide in advance what happens when no synchronous standby is available. Regular failover tests prove the numbers.

What to say in the interviewAbout 90 seconds aloud

Open with

For RPO zero I need every commit on disk in a second zone before it returns, so I would run a primary and two standbys and set synchronous_standby_names to ANY 1 of the two, with synchronous_commit on.

Then, if they want more

Using ANY 1 means one standby can be down without blocking writes, and the cross-zone round trip costs a millisecond or two per commit. For failover I would use Patroni, or a managed service with the same guarantees, with etcd spread across the three zones, so the third zone acts as the tie-breaker.

The primary holds a leader lease in etcd; if it cannot renew it, it demotes itself, and a watchdog fences a hung node, which prevents split brain. Patroni's synchronous mode only promotes a standby that was synchronous, so nothing committed is lost, and with a lease of about 20 seconds failover fits in 30.

Clients use a stable endpoint: a proxy checking Patroni, or a multi-host JDBC URL with targetServerType=primary, and connect and socket timeouts shorter than the failover window, with HikariCP validating connections. The application retries only idempotent work, using idempotency keys on payments, which also answers the case where the connection drops during commit.

I would decide upfront that if both standbys are down, commits wait rather than silently going async. And I would test failover regularly under load and measure the real gap.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design high availability for a PostgreSQL cluster that backs payments: RPO zero...": https://scrivio-interview-library.netlify.app/databases/postgresql/11d9de0367/