DEV Community

Cover image for PostgreSQL failover drill with Patroni: 35 seconds, 0 lost writes
Denys Zarubin
Denys Zarubin

Posted on Originally published at moveinfra.eu

PostgreSQL failover drill with Patroni: 35 seconds, 0 lost writes

Originally published on moveinfra.eu.

A three-node PostgreSQL cluster run with Patroni lost its primary and accepted writes again after 36.4 seconds in our first drill and 34.6 seconds in the second. In both runs every write that the database had confirmed to the client was still there afterwards: 0 of 146 and 0 of 147 rows missing. We measured this on 17 November 2026 on UpCloud in Helsinki, on the smallest servers and with a near-empty database, so read it as a floor for the mechanism and not as a promise for your data.

The reason to publish it is a discussion that repeats on Hacker News every few months. In the July 2026 thread on a startup's Postgres guide, one side says that running PostgreSQL yourself saves "absolute peanuts" compared with a managed service. The other side says a pair of servers with backups costs under €50 a month and is not hard. Both sides argue from memory. Almost nobody posts what happened when they pulled the plug.

Key takeaways

  • Failover took 35 to 36 seconds. 30 of those seconds are a setting, the Patroni leader lease. The rest is the election and the client reconnecting.
  • No confirmed write was lost, because replication was synchronous: the primary confirms a write only after a second server has it.
  • Losing all three servers at once did lose data: 6 of 717 confirmed rows, the last 1.6 seconds. By design this can be up to about 60 seconds.
  • Restoring is the part to rehearse. A restore of the newest backup took 12 to 13 seconds for a 30 MB database; that time grows with size, and ours is far too small to predict yours.
  • The saving against a managed database is real but small. The honest reasons to run it yourself are a provider without managed PostgreSQL, or needing control that a managed service does not give.

What we built

The cluster is the open reference build that MoveInfra (moveinfra.eu) publishes as Terraform: postgres-ha-upcloud.

  • Three database servers, each on a different physical host, with PostgreSQL 18.
  • Patroni on each server and etcd as the place where they agree who is primary.
  • Synchronous replication: one replica is the synchronous standby.
  • pgBackRest to object storage: a full backup weekly, a differential daily, and every WAL segment within 60 seconds.
  • A fourth small server for Prometheus, alerts and a weekly restore test.

Applications get one connection string that lists all three servers with target_session_attrs=read-write. The PostgreSQL driver tries each server and keeps the one that accepts writes. There is no load balancer and no floating address in front of the database, so there is one part fewer that can fail during a failover.

The drill

The script is in the repository as drill/failover-drill.sh, so you can repeat it:

  1. A client inserts one row every 0.2 seconds and records every insert the database confirmed.
  2. After 30 seconds the primary is powered off through the cloud API, with no shutdown. To PostgreSQL this is the same as a pulled power cable.
  3. The client keeps trying. The script prints the longest time without a successful write.
  4. At the end it counts the rows in the table and compares them with the confirmed list.
Test Run 1 Run 2
Longest time without a successful write 36.4 s 34.6 s
Confirmed rows missing after failover 0 of 146 0 of 147
Old primary powered on again, back as a replica 33 s 146 s
Commands typed by a person none none

The old primary came back by itself: it noticed that it was no longer the leader, rewound its data to the point where the two histories split, and followed the new primary. Most of the 146 seconds in the second run was the server booting.

Why 35 seconds, and why we did not make it shorter

Patroni keeps a leader key in etcd that expires. In our configuration the lease (ttl) is 30 seconds and the check interval (loop_wait) is 10 seconds. When the primary dies without saying goodbye, the replicas must wait until the lease runs out before one of them may take over. The Patroni documentation describes ttl as the time before the automatic failover process starts.

You can lower it. The price is false failovers: a primary that is only slow for 10 seconds, because of a noisy neighbour or a short network problem, loses its lease and the cluster changes primary for no good reason. Each unnecessary failover interrupts writes that would otherwise have gone through. We kept the default.

For comparison, AWS states that an RDS Multi-AZ deployment with one standby fails over "as quickly as 60 seconds", and the variant with two readable standbys "typically under 35 seconds". Those are the vendor's numbers for its service, and ours is one measurement on an empty database, so this is not a race. It shows that the mechanism is in the same range, which is what the Hacker News thread could not settle.

The two tests that matter more

A failover is the easy case. Two others decide whether you keep your data.

Someone deletes the wrong rows. We deleted 990 of 1,000 rows and then restored a copy of the database to five seconds before the delete, on a separate server. All 1,000 rows were back after 18 seconds. The backup we restored from was older than a failover, so the restore had to follow the cluster across a change of primary. It did.

Everything is gone. We powered off all three database servers at once during writes and replaced them with new, empty servers. Nothing was left except the bucket with the backups. The first new server found the backup, restored it and replayed the archived WAL without any command from us. The cluster accepted writes about 5 minutes after the new servers existed. 711 of 717 confirmed rows were back. The last 1.6 seconds were lost.

That loss is by design. WAL reaches the bucket when a segment is full or after 60 seconds, whichever comes first. If all servers disappear together, up to a minute of confirmed writes can be gone. A single server failing loses nothing.

Three mistakes the build had to avoid

These are the answers to a question from the thread that nobody answered: what are the pitfalls of running PostgreSQL yourself?

  1. An empty database must never replace a lost one. A new server that finds no cluster will happily create a fresh, empty database. Ours looks in the bucket first. If a backup exists it restores it, and if the bucket cannot be reached it retries instead of starting empty.
  2. Packages start services with default settings. On Debian and Ubuntu, installing PostgreSQL starts a default cluster at once. Patroni then finds a database it did not create. The setup script blocks service starts during installation and starts each service after its configuration is written.
  3. A change to the setup script must not rebuild a database server. In Terraform, the script is part of the server's definition. Without ignore_changes on it, fixing a typo in the script plans the replacement of all three database servers.

What this does not prove

  • Size. 30 MB tells you nothing about restoring 300 GB. Restore time grows with size and with the amount of WAL to replay. Measure it on a copy of your own data.
  • Load. One insert every 0.2 seconds is not production traffic. We did not measure how much synchronous replication slows writes.
  • One zone. All servers are in one data centre zone. The cluster survives a server or a physical host failing, not the whole site. The backups survive that; the running cluster does not.
  • Network inside the private network is not encrypted in this version, and etcd has no authentication. The boundary is the private network.
  • One day. Built twice, drilled twice, rebuilt once from the bucket. It has not run for months.

What it costs, and the honest comparison

At the default sizes (three servers with 2 CPU and 4 GB, one with 1 CPU and 2 GB, object storage) the cluster costs roughly €90 to €110 a month at UpCloud list prices.

A managed PostgreSQL with 2 vCPU and 8 GB costs €60 a month on UpCloud (one node), €80 on Scaleway and €133 on AWS in Frankfurt (one instance, one zone), according to the EU Cloud Price Index for November 2026. These are single nodes, so the comparison is not like for like: a managed setup with a standby costs more than the single node, and our cluster has three nodes of a smaller size.

So the "peanuts" side of the argument has a point. The difference is tens of euros a month, and one hour of an engineer costs more than that. Running it yourself makes sense in three cases: your provider has no managed PostgreSQL, you need an extension or a setting the managed service does not allow, or you want the restore path in your own hands and tested. If none of these applies, buy the managed database and spend the time on a restore test, because a managed database with an untested restore is in the same position as ours before the drill.

Frequently asked questions

How long does a Patroni failover take?

In our two drills, 34.6 and 36.4 seconds from power-off to the next successful write. About 30 seconds of that is the leader lease (ttl). A planned switchover, where the primary hands over on purpose, does not wait for a lease to expire; we did not time one.

Does synchronous replication lose no data at all?

It loses no confirmed write when one server fails. It does not protect against losing all servers at once: then you fall back to the backup and the archived WAL, and the newest seconds can be missing. In our total-loss test that was 1.6 seconds.

What are the common pitfalls of running PostgreSQL yourself?

The ones we had to design around: a new server creating an empty database in place of a lost one, packages starting a default cluster before the configuration exists, and infrastructure code that replaces a database server after a harmless change. Beyond those, backups that nobody has restored.

Is self-run PostgreSQL cheaper than a managed service?

A little. At list prices the gap is tens of euros a month at this size, and the work is yours: updates, alerts and restore tests. On a provider that has no managed PostgreSQL the question is different, because the alternative is another provider.

How do I test this on my own database?

Run the drill script against a copy, with your data size and your write rate. Then restore the newest backup on a separate server and time it. Those two numbers are your real recovery time, and they belong in your rollback plan.

Sources


Drafted with AI assistance and fact-checked by the MoveInfra team. The original, kept up to date, is at moveinfra.eu. Free tools for pricing a move to an EU cloud: moveinfra.eu/tools.

Top comments (0)