Skip to content

Replication and Failover: What Gets Lost When the Primary Dies

11 min reading

Specify surviving state and authority before promising recovery from primary failure.

Page details

Distribute the data · Lesson 09

Published · Sources · 11 min reading · Last edited:

Know first: Numbers Every Engineer Should Know; Reading Architecture Diagrams.

Editorial correction:

Earlier design notes used unsupported universal performance or reliability claims. This revision explains conditional mechanisms and explicitly hypothetical examples; no author-run benchmark or production incident is asserted.

More
20px

Northwind: Replication: acknowledgements, lag, and safe failover

Outcome. After this lesson, you can distinguish received, durable and applied changes.

Hypothetical scenario. At Northwind, The primary fails while its replica is two seconds behind.

In short. Define acceptable loss before promoting a replica. Fence the previous primary before allowing new writes.

A copy exists, but can it take over?

Hypothetical scenario. A seller updates an item's price and receives success. The database primary then becomes unreachable. An operator promotes a standby, but the price is missing. The standby was healthy enough to answer queries; it had not necessarily received the change covered by the application's acknowledgement.

Replication copies state or a sequence of changes. Its usefulness depends on which changes reach which failure domains, what is durable, what is visible and who may accept the next write. Counting replicas does not establish those properties.

Partitioning selected the owner of a subset of records. This chapter follows one such subset through acknowledgement, lag, promotion and recovery. The goal is to define what the application can promise when the current owner fails, without treating a successful health check as proof of safe authority.

Reachable is not current or authoritative

A replica can be connected but behind, durable but not yet replayed, or current at an observed position while lacking permission to become leader. These are different states. A dashboard that compresses them into a green icon can hide the condition that matters during an incident.

Likewise, a timeout does not establish that the primary has stopped. It may have lost only one network path while continuing to serve other clients. Promoting another writer solely because one monitor cannot reach the first can produce conflicting authorities.

Separate failure detection from the decision to transfer ownership. Detection supplies evidence under uncertainty. A safe transition must also exclude obsolete writers and select state consistent with the acknowledgement contract. Routing traffic away from the old primary is helpful, but stale clients or background workers can still contact it.

Follow the change through each boundary

A useful conceptual pipeline distinguishes a change accepted by the primary, transmitted to another node, durably stored there, and applied so that reads can observe it. Concrete systems combine or expose those boundaries differently. Read the selected acknowledgement setting rather than assuming the word synchronous names one universal behavior.

Acknowledgement boundaries in a replicated writeA primary accepts a change and sends it to a standby. The standby receives, persists and applies the change. The client acknowledgement must identify which boundary it covers, while promotion separately requires authority.Primary logReceivedPersistedAppliedWhat does the client acknowledgement cover?Promotion also requires excluding obsolete write authority.

Asynchronous acknowledgement can return before a standby holds the change. Waiting for another durable copy adds a dependency to the write path. Waiting for replay can establish a different visibility boundary again. These choices change failure exposure and waiting behavior; they do not remove the need for explicit promotion rules.

The PostgreSQL 17 standby documentation describes streaming, synchronous replication and standby operation. Its replication settings specify concrete acknowledgement configurations. These references are examples of why a database's actual settings belong in the service contract.

Distinguish received, durable and applied changes

Northwind's primary acknowledges a checkout update and then becomes unreachable. A standby can be reachable while still missing that change, or it can have received the change without making it visible to a query yet. PostgreSQL's replication documentation separates relevant processing boundaries. A promotion policy needs to identify which boundary supports the promised durability and read behavior.

Assume, purely for sizing, a steady primary rate of five hundred committed updates per second and a standby that is two seconds behind. The lag represents approximately 500 × 2 = 1,000 updates in this steady-rate model. It is an exposure estimate, not a count of transactions certainly lost: bursts, received-but-unapplied records and durable positions can change the actual result. Inspect the relevant log positions before stating the recovery outcome.

If the replica replays seven hundred updates per second while the primary continues generating five hundred, net catch-up is 700 − 500 = 200 per second. A backlog of one thousand then takes 1,000 / 200 = 5 seconds to drain under these fixed-rate assumptions. If replay falls below generation, there is no positive catch-up margin. A healthy network connection cannot compensate for a persistently slower apply path.

Failover also transfers authority. The old primary can return after a network partition, so the design needs an effective way to reject obsolete writes. Reconcile uncertain acknowledged operations against the surviving authoritative history. Replicas are valuable for machine failures, but a mistaken logical deletion can be faithfully replicated everywhere. Retained history and independently tested restoration serve a different purpose from making more current copies.

Derive loss exposure and catch-up time

Hypothetical scenario. Choose a workload of 200 acknowledged transactions per second. Suppose none of the last three seconds of those transactions reached any surviving copy, and the primary's relevant storage is permanently lost. Under this deliberately uniform model, 200 × 3 = 600 acknowledged transactions are exposed to loss.

This is not a general conversion from a lag dashboard to lost business actions. Transactions have different sizes, acknowledgements may cover other replicas, and some operations may be recoverable from separate records. The example makes its missing-copy assumption explicit because that assumption determines the result.

For a separate recovery calculation, choose a replay backlog of 120 MB. The replica can apply 30 MB per second while the primary generates 20 MB per second of new changes. The net catch-up rate is 30 − 20 = 10 MB per second, so 120 ÷ 10 = 12 seconds under constant rates. Dividing by 30 would incorrectly ignore continuing work.

If generation increases to 35 MB per second while replay remains 30, the backlog grows by 35 − 30 = 5 MB per second. A connected replica can therefore fall further behind. Adding a network link does not solve an apply bottleneck.

Finally, choose serial recovery steps of 5 seconds to detect a problem, 8 to establish authority, 12 to finish required replay and 5 to restore routing. Their sum is 5 + 8 + 12 + 5 = 30 seconds. Real steps may overlap or fail; this is an illustrative recovery budget, not a measured recovery-time guarantee.

In the wild: Distinguish received, durable and applied changes

The primary reference establishes this concrete distinction: standby replication exposes distinct shipping, persistence and replay considerations.

A failure domain is more than a machine

Two replicas can share power, network control, operator credentials or a destructive deployment. Placing copies on different hosts addresses only some failures. State the event the design is intended to survive and identify dependencies that can still remove all usable copies.

Replication can faithfully propagate an unwanted deletion or a faulty application update. Historical backups and recovery points serve a different purpose from a current standby. A backup also needs an independently checked restore procedure and the records necessary to reach the intended point.

There is no universally correct trade between acknowledgement latency and survival guarantees. A service that waits for a remote copy can lose write availability when that dependency is unavailable. A service that returns early must communicate its corresponding exposure. Evaluate these choices against the actual user action and failure model.

The previous primary returns

Hypothetical scenario. A standby is promoted during a network interruption. The previous primary later reconnects with changes that were never included in the promoted history. Restarting it as though nothing happened permits two incompatible histories to serve clients.

The recovery procedure must identify the authoritative history, preserve evidence of divergent accepted actions, and rejoin or rebuild the old node through the database's supported procedure. An operator should not casually merge physical database files or accept whichever timestamp looks newer.

Preventing obsolete writes can require infrastructure fencing, authority epochs enforced by the resource, or another documented exclusion mechanism. A lease only helps under its actual clock and enforcement assumptions. The application may still need to reconcile actions whose client-visible outcome was uncertain during the transition.

Three properties replication does not imply

“A replica is a backup.” A current copy can contain the same logical mistake. Recovering an earlier correct state needs retained history and a restore process.

“Synchronous means every read sees the write.” Identify which replicas and which receive, persistence or apply boundary the setting covers. The actual read routing also matters.

“Automatic failover means immediate safe recovery.” Automation still needs authority, suitable state, routing, resource capacity and a way to handle divergent or uncertain work.

Choose a promotion policy before an outage

Use replicas to meet a stated read, recovery or durability need. Decide which copies may serve each read contract, which may be promoted, and what an acknowledgement promises if a named failure occurs. Keep enough evidence to determine whether a transition respected those rules.

Do not use an arbitrary lag threshold as a substitute for identifying missing acknowledged changes. A seconds-based target can be a useful operational objective, but it is not automatically a transaction-level proof. During an ambiguous failure, refusing a write can be preferable to making an unsupported promise about its survival.

The reliability module next examines the client's uncertain outcome. Even a sound replication protocol cannot ensure that a client receives the response after a committed effect. Replication, retry safety and business reconciliation must fit together.

Limits of this model

The lag and catch-up estimates assume fixed rates. Durable receipt, replay visibility and promotion eligibility need separate evidence. Correlated failures, obsolete writers, missing history and logical corruption require explicit recovery decisions beyond the existence of another replica.

Check yourself: inspect the recovery promise

The standby answers health checks but has an increasing replay backlog. Is it a current read target?

Not on that evidence. Inspect the required visibility boundary and replay position. Connectivity does not establish currency, and a growing backlog indicates that its current processing rate is insufficient.

A monitor cannot reach the primary. Is promoting the nearest copy sufficient?

No. Establish that the candidate has suitable state and that obsolete writers cannot continue. The monitor's timeout alone does not prove the primary stopped.

A deletion was replicated to every copy. Did replication fail?

It may have worked exactly as configured. Recovering from an unwanted logical operation requires appropriate retained history, independent protection and a tested restoration decision.

Northwind decision log

Define acceptable loss before promoting a replica.
Fence the previous primary before allowing new writes.
Reconcile acknowledged operations after promotion.

Go deeper: Distinguish received, durable and applied changes

Related blog: Provider failover after an uncertain outcome.