Key takeaways
- ✓Recovery time (RTO, how long writes stop) is the lease plus a little: with a 2-second lease, a killed primary took writes again after 2.30 seconds at the median, and 5.45 and 10.33 seconds with 5- and 10-second leases.
- ✓Recovery point (RPO, how much confirmed data is lost) was zero in all 20 kills, but a primary that was cut off, and still took writes from its clients, lost 1,856 acknowledged writes across 10 runs, between 162 and 202 in every run.
- ✓Asynchronous replication loses a confirmed write when the primary says yes before the replica has the write, so a cut-off primary keeps saying yes to writes that never reach the replica until it fences itself 1.2 seconds after its last renewal, which here is just under a second of writes at 200 a second.
- ✓Relaxed synchronous replication, where the primary stops waiting for a replica that has disconnected, lost 1,946 writes in 10 runs (165 to 220 each), as many as asynchronous, because it keeps saying yes alone.
- ✓Strict synchronous replication, which never stops waiting for its replica, lost none in 10 runs, but on two nodes no write succeeded for the rest of the test, so the cost is availability, not data.
- ✓A 2-second lease failed over a healthy primary on 5 of 5 two-second pauses, which forced a rebuild for nothing, while a 5-second lease failed over on 0 of 5, so the lease must be long enough that the primary's fencing point, 0.6 of the lease, comes after the longest pause a healthy primary takes.
A primary database was killed 20 times, frozen 10 times, and cut off from its lease store and replica in 10 asynchronous runs and 20 synchronous ones, with a write-checker recording every write the database confirmed. A killed primary failed over in 2.30 seconds and lost none of 62,773 confirmed writes. A primary that was cut off, but still reachable by its clients, lost 162 to 202 confirmed writes in every asynchronous run. This chapter covers the two numbers that describe a failover, the lease that decides who may be primary, and what each replication setting buys and costs.
The examples come from Lab 18 (Failure, Failover and Recovery) of the system design labs. This chapter uses the results of its script 01_failover.py, which breaks the primary with the test running, and of 03_detection.py, which sweeps the lease length. Both ran in Docker on one Apple M4 Max laptop: two Postgres 17 nodes, an etcd 3.5 store, and a small failover controller written for the lab. The lab's write-checker, tools/writer.py, sends 200 writes a second from four clients and records every write that got a yes. Each scenario is one set of recorded runs, not a long benchmark.
RTO and RPO
A failover has two numbers, and they measure different things.
- RTO (recovery time objective) is how long the service is down: from the fault to the first write the new primary confirms. The lab measures it from the clients' side, because that is what users see. A write to the frozen primary waited out the client's 2-second timeout, so writes came back at 3.03 seconds even though the replica was promoted at 2.43 seconds.
- RPO (recovery point objective) is how much data the failover loses. Here it is counted in writes: the writes the client was told succeeded that the new primary does not have.
The two trade against each other. Asynchronous replication is fast and can lose the newest writes. Synchronous replication can lose nothing, but a stalled replica then stalls the primary.
Why asynchronous replication loses confirmed writes
By default, a Postgres primary says yes as soon as the write is on its own disk, and sends the change to its replica a moment later. That is asynchronous replication. The replica usually has the write within a split second, so a primary that dies loses nothing that was confirmed. That is what the 20 kills show: 0 of 62,773 writes lost.
A primary that is cut off is a different case. Here the lab cuts it off from etcd, the small store that holds the lease, and from its replica, but not from its clients. The primary is alive, its clients still reach it, and it keeps saying yes. Every one of those yeses is a write the replica will never receive. The primary cannot know it has been replaced until it fences itself, which is when it stops accepting writes. In the lab, that took 1.2 seconds after its last successful renewal of the lease, and the writes it accepted in that second, about 190 at 200 a second, exist only on the old primary. Across 10 cut-off runs that came to 1,856 writes, between 162 and 202 in each run.
Fencing, here, means the primary stops accepting writes. It happens before the replica can take over, so in every run the old primary had stopped confirming writes before the new one started. The loss is the price of the window between the cut and the fence.
Synchronous and strict synchronous replication
Synchronous replication closes that window. The primary confirms a write only after the replica has it too, so a cut-off primary cannot say yes to a write the replica lacks. In theory that loses nothing. The lab tested two ways of doing it, and they behave differently.
- Relaxed synchronous (the controller's own mode). The primary waits for the replica only while it is connected. When the controller sees the replica disconnect, it removes the replica from the list of synchronous standbys, named by the Postgres setting
synchronous_standby_names, and the primary commits alone. In 10 cut-off runs it lost 1,946 writes, 165 to 220 each, as many as asynchronous. Production failover tools such as Patroni guard this with etcd, recording which standby is synchronous and not letting a replica that was dropped take over. The lab did not test Patroni. - Strict synchronous (plain Postgres). The primary never stops waiting. No replica, no yes. In 10 cut-off runs it lost none. But on two nodes the only standby is the replica that was cut off, so nothing could commit. No write succeeded for the rest of the 18-second test. With two nodes, strict synchronous replication trades all availability for zero loss while a node is missing, until the lost node is rebuilt. The usual way out is a third node, so the primary still has a standby to wait for after one failure. The lab did not build that.
So synchronous replication helps only when the primary, without its replica, stops writing or never fails over to it. The relaxed mode does neither, and it loses as much as asynchronous.
The lease: who is allowed to be primary
The primary is whoever holds the key /pg/leader in etcd. The key is attached to a lease with a time to live (TTL), the number of seconds it lives unless renewed. The primary renews the lease every tick, which is TTL divided by 4 and at most a quarter of a second, so with a 2-second lease it renews four times a second. If it stops, the lease expires after TTL seconds, the key disappears, and the replica takes it, promotes itself and becomes primary. The replica can take it only once, because only one node can create the key.
The controller's rule, in election.py, has one more step. The primary fences itself once it has not renewed for 0.6 of the TTL (the lab's FENCE_AT), which is 1.2 seconds with a 2-second lease. That is before the lease can expire, so the old primary stops first. The fencing rule is why the lease is the upper bound on RTO, and why the cut-off primary's loss is bounded by the same 1.2 seconds.
Recovery time follows the lease. In the lease sweep, with five kills each, RTO was about the TTL plus 0.3 to 0.45 seconds. The next section gives the numbers.
The workflow and the results
Failover (01_failover.py). The write-checker runs for 18 seconds at 200 writes a second, with the fault about 5 seconds in. It repeats the fault for each setting: kill (SIGKILL, the process dies at once), pause (every process frozen for 10 seconds, then resumed), and cut off (Toxiproxy, a network proxy the lab puts between each node and etcd and between the nodes, cut for 12 seconds). Afterwards it counts the writes that were confirmed and are missing on the new primary. The lab's failover_metrics.py computes RTO and the loss from the write-checker's log.
The primary is alive and its clients still reach it, but it cannot reach etcd or its replica for 12 s.
Lost 162–202 per run across 10 runs of 33,895 acknowledged writes in total. The bar is scaled to 2,000.
Writes are 200 a second for 18 s, the fault at about 5 s, lease 2 s. RTO is the time from the fault to the first write the new primary acknowledges. "No writes" means the cluster stopped taking writes for the rest of the test.
| Fault | Replication | Runs | RTO p50 | Replica promoted after (p50) | Acknowledged writes lost | Runs with loss |
|---|---|---|---|---|---|---|
| Killed | asynchronous | 20 | 2.30 s | 2.38 s | 0 of 62,773 | 0 |
| Frozen 10 s, then alive | asynchronous | 10 | 3.03 s | 2.43 s | 0 of 29,960 | 0 |
| Cut off (etcd and replica) | asynchronous | 10 | 2.30 s | 2.38 s | 1,856 (162–202 per run) | 10 |
| Cut off | synchronous, drops the standby | 10 | 2.48 s | 2.56 s | 1,946 (165–220 per run) | 10 |
| Cut off | strict synchronous | 10 | no writes | 2.62 s | 0 | 0 |
Lease (03_detection.py). Five kills and five 2-second pauses at each lease length. The pause is the case where the primary is slow, not dead: a stall, a disk hang, a GC pause.
A failed-over pause means the healthy primary was replaced and had to be rebuilt as a replica, for nothing. Five kills and five 2 s pauses per lease.
| Lease asked for | Kill: RTO p50 | Kill: RTO max | 2 s pause: failed over | Write gap during the pause (p50) |
|---|---|---|---|---|
| 1 s (etcd granted 2 s) | 2.29 s | 2.75 s | 5 of 5 | 2.06 s |
| 2 s | 2.31 s | 2.40 s | 5 of 5 | 2.08 s |
| 5 s | 5.45 s | 5.69 s | 0 of 5 | 2.06 s |
| 10 s | 10.33 s | 10.65 s | 0 of 5 | 2.04 s |
The shorter the lease, the faster the failover and the more often a healthy primary is replaced for nothing. A 2-second lease is faster, but it cannot tolerate a 2-second pause, so a lease must sit above the longest pause a healthy primary can take, with room to spare.
Scope of this chapter. Lab 18 has five scripts. This chapter uses 01_failover.py and 03_detection.py. The split-brain script 02_split_brain.py is the next episode, and the bulkhead and point-in-time restore scripts (04_bulkhead.py, 05_pitr.py) are in a later one.
Caveats
- Few runs, one machine. Each scenario is 10 or 20 runs on one laptop with both nodes in Docker. Network delay between real machines, and the lag it adds, is not in these numbers.
- The loss in the cut-off runs is a lower bound. The lab cuts etcd and the replication link with two separate commands, and the fault time is stamped before both, so the replication link is cut a moment after etcd. An instantaneous cut would lose at least as much.
- RTO is approximate. The fault time is taken on the Mac just before the command, and the acknowledgement times come from the containers. The replica's promotion is logged after the promotion, so the first new write often lands a few hundredths of a second before the logged time.
- Two nodes only for strict sync. The strict result, stopping all writes, is a two-node result. Three nodes with one required standby were not tested.
- The slow-link kill did not test what it meant to. The lab's README says a kill with 200 ms of delay on the replication link lost nothing, but in-flight data reached the replica after the primary died, so it does not measure the case it was built for. It is not reported here.
- Not covered: fencing tokens (passing a term number with each write so storage rejects old ones), a third node, a replica that is too far behind to promote, disk failure, and multi-region failover. The lab lists fencing tokens and a third node as exercises.
- Writes are counted, not bytes. The lost figure counts confirmed writes, one per row, and each run's writes are unique by (run, id), so a repeated id from another run is not counted twice.
The checklist
- Measure RTO and RPO from the client. The clients' view includes their timeouts and reconnects, which is what users see.
- Set the lease above the longest pause a healthy primary can take. Then fence the primary before the lease can expire, as the lab does at 0.6 of the TTL.
- Treat a cut-off primary as the dangerous case. It is still reachable, so it keeps saying yes until it fences itself.
- Check that synchronous replication stops writes when the replica is gone, or that the failover refuses to promote a replica that was dropped. Relaxed mode does neither, and it loses as much as asynchronous.
- Price the zero-loss setting. Strict synchronous replication lost none, but with two nodes it stopped all writes while a node was missing. Plan a third node if writes must keep flowing.
Quiz
1.A primary is killed, and separately a primary is cut off from its lease store and its replica but its clients can still reach it. Both fail over in about 2.3 seconds. Which one loses confirmed writes?
2.Relaxed synchronous replication is meant to keep the primary writing when its replica disconnects. What does that cost when the replica is cut off?
3.With a 2-second lease, a healthy primary freezes for 2 seconds. What happens?
Check your understanding
Try each question before opening its answer.
A killed primary lost none of 62,773 writes, but a cut-off primary lost 1,856. Both failed over in about 2.3 seconds. What is different?
The cut-off primary is still alive and reachable by its clients, so it keeps confirming writes. Its replica never gets them, and it cannot know it has been replaced until it fences itself, 1.2 seconds after its last renewal. A killed primary takes no more writes, and every write it had confirmed had already reached the replica.
Relaxed synchronous replication was supposed to keep writes flowing when the replica disconnects. Why did it lose 1,946 writes?
Because when the replica disconnects, the controller stops waiting for it, so the primary commits writes alone and confirms them. Those writes never reach the new primary after the failover. That is the same mechanism as asynchronous replication, so the loss is the same size.
Why did strict synchronous replication lose none, and what did it cost on two nodes?
It never stops waiting for its replica, so no write is confirmed without one. With two nodes, the only replica is cut off, so no write could be confirmed for the rest of the 18-second test. The cost is availability: zero loss was bought with no writes until the lost node was rebuilt.
Why did a 5-second lease fail over on 0 of 5 pauses, while a 2-second lease failed over on 5 of 5?
A 2-second pause lasts longer than the primary's fencing point of 1.2 seconds with a 2-second lease, so the healthy primary fences itself and the replica then takes the lease. With a 5-second lease, the fencing point is 3 seconds, and the 2-second pause ends before it is reached, so the primary resumes and keeps the lease.
A 2-second lease gives a 2.31-second RTO, and a 10-second lease gives 10.33 seconds. What does that tell you about choosing a lease?
RTO is roughly the lease plus 0.3 to 0.5 seconds, so the lease is the setting that decides how long writes stop after a real failure. Choosing a shorter lease shortens the outage, but it also fails over healthy primaries that pause for longer than the fencing point. Pick the shortest lease that is still above the longest healthy pause you measure.
Next up: Split brain and fencing, the next episode: the cut-off primary does not always stop itself, and a client that keeps writing to it can leave two primaries at once.