Part 2 · Blueprint 10
Replication, Consensus & Automated Failover
15 min reading · Sources · Editorial review pending
A surviving copy is not automatically caught up or entitled to accept writes.
Page details
Published date · Last edited:
Draft edition. Editorial review and supplementary reading recommendations are still being completed.
Format: Architecture walkthrough. Prerequisites: the beginner bootcamp. Chapter prerequisites: Consistency Models: CAP, PACELC & Linearizability, Database Sharding & Consistent Hashing.
Sixty-second mental model
Problem: A surviving copy is not automatically caught up or entitled to accept writes.
Use: Fence the old writer before publishing the new authority.
Design boundary: Distinguish replication for availability from backup for recovery. A replica can rapidly copy a deletion or corruption. Failover requires a rule about which committed history the successor may serve.
Layer 1 · Intuition
Hypothetical scenario: Northwind is designing replication and failover. Maya represents product requirements, Dev implementation and Priya reliability. The team must agree what one commit means and which component owns the replication history.
Think of a prepared replacement clerk taking over a ledger. Separate the requester from replication history, then identify the retained evidence needed after failure.
- Authority
- The entitled state owner.
- Invariant
- A rule preserved across concurrency and failures.
- Durability
- A promise about preserving acknowledged state under a specified failure model. Analogy: A receipt backed by a record that survives the promised disruptions.
- Failover
- Transferring a service role from a failed or unsuitable owner to another. Analogy: Handing the service desk to a prepared replacement.
- Replica
- A copy of data maintained through a replication protocol. Analogy: Another maintained copy of the same ledger.
- Replication lag
- Delay between an authoritative change and its visibility at a replica. Analogy: The time before a copied ledger catches up.
- Split brain
- Concurrent participants acting as authoritative owners without safe coordination. Analogy: Two managers both issuing conflicting final instructions.
Architecture and worked reasoning
Design boundary: Distinguish replication for availability from backup for recovery. A replica can rapidly copy a deletion or corruption. Failover requires a rule about which committed history the successor may serve.
Responsibilities on the request path
Workload and limiting resource
Hypothetical replication lag is 400 ms at 2,000 updates/s: about 800 updates can be in flight under a steady-rate simplification. A 15-minute backup interval is 900 seconds; at the same rate that interval spans 1,800,000 updates. Neither number alone establishes actual data loss, because acknowledgement and log retention determine what can be recovered.
Keep the units attached to each intermediate result. The assumptions above describe a hypothetical exercise, not observed vendor capacity. A real capacity decision also needs a measured service curve, skew, recovery traffic and an explicit admission policy for overload.
Mutation or publication path
- Append changes under the selected durability rule.
- Track replica progress and lag.
- Promote only an eligible successor and fence the previous primary.
Observation and recovery path
- Use a replica only under a declared read contract.
- Route current decisions to eligible authority.
- Test restoration independently of ordinary replica reads.
Failure that changes the decision
Hypothetical scenario: Both primary and replica accept writes after a network split. A later merge cannot automatically repair unique allocations or external effects; prevent dual authority with an appropriate election/fencing protocol.
Attempt this decision before reading the explanation
Can three replicas replace point-in-time backups?
No. Shared deletion, operator error or logical corruption can propagate to all three. Backups need independent retention and a tested restore procedure with declared recovery objectives.
Supporting mechanism analysis
Hypothetical scenario: Northwind. Maya represents product, Dev implementation and Priya reliability. The following workflow develops the underlying mechanism.
Hypothetical scenario. At Northwind, The primary fails while its replica is two seconds behind.
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.
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.
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.
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.
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.
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.
The primary reference establishes this concrete distinction: standby replication exposes distinct shipping, persistence and replay considerations.
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.
Layer 3 · Internals
Trade-offs and limits
| Approach | Provides | Requires |
|---|---|---|
| Synchronous acknowledgment policy | Can require selected replica evidence before returning success. | Slow or unavailable required participants affect write latency and availability. |
| Asynchronous replication | Decouples some replication waiting from acceptance. | Promotion must account for changes not yet present at the selected replica. |
Recovery decision
Hypothetical scenario: Both primary and replica accept writes after a network split. A later merge cannot automatically repair unique allocations or external effects; prevent dual authority with an appropriate election/fencing protocol.
No. Shared deletion, operator error or logical corruption can propagate to all three. Backups need independent retention and a tested restore procedure with declared recovery objectives.
Write your design before revealing the review
Can three replicas replace point-in-time backups?
Drafts and self-review are stored only in this browser. They are separate from reading completion.
Compare your decision
No. Shared deletion, operator error or logical corruption can propagate to all three. Backups need independent retention and a tested restore procedure with declared recovery objectives.
Both primary and replica accept writes after a network split. A later merge cannot automatically repair unique allocations or external effects; prevent dual authority with an appropriate election/fencing protocol.
Check your understanding
Can three replicas replace point-in-time backups?
No. Shared deletion, operator error or logical corruption can propagate to all three. Backups need independent retention and a tested restore procedure with declared recovery objectives.
How would you recover from this failure: Both primary and replica accept writes after a network split. A later merge cannot automatically repair unique allocations or external effects; prevent dual authority with an appropriate election/fencing protocol.
Start with the exact boundary that failed. Append changes under the selected durability rule. Track replica progress and lag. Promote only an eligible successor and fence the previous primary. Then recover evidence through the observation path: Use a replica only under a declared read contract. Route current decisions to eligible authority. Test restoration independently of ordinary replica reads. Preserve the distinction between a confirmed state change and an unavailable response.
Which assumption in the worked calculation would a burst or concentrated tenant invalidate first?
Hypothetical replication lag is 400 ms at 2,000 updates/s: about 800 updates can be in flight under a steady-rate simplification. A 15-minute backup interval is 900 seconds; at the same rate that interval spans 1,800,000 updates. Neither number alone establishes actual data loss, because acknowledgement and log retention determine what can be recovered. Recompute the affected resource rather than multiplying unrelated storage and network budgets by the same factor. Distinguish replication for availability from backup for recovery. A replica can rapidly copy a deletion or corruption. Failover requires a rule about which committed history the successor may serve.
Which component in this topology could be removed from a smaller first deployment?
Primary, Replica log, Standby, Backup archive name responsibilities, not a requirement for separate processes. Co-locate responsibilities when their transaction, trust and capacity boundaries permit it. Append changes under the selected durability rule.
What evidence must a caller have before relying on the observation path?
Use a replica only under a declared read contract. Route current decisions to eligible authority. Test restoration independently of ordinary replica reads. No. Shared deletion, operator error or logical corruption can propagate to all three. Backups need independent retention and a tested restore procedure with declared recovery objectives.
45-minute interview rubric
- 00–05: scope commit and replication history.
- 05–12: identify the limiting resource and derive the domain workload above, retaining units and assumptions.
- 12–28: draw replication history, numbered writes and reads.
- 28–40: challenge recovery: Fence the old writer before publishing the new authority.
- 40–45: defend leader_epoch placement, observations and remaining assumptions.
Go deeper
- PostgreSQL: Documentation: 18: 26.2. Log-Shipping Standby Servers · Primary document · www.postgresql.org. Fence the old writer before publishing the new authority. Verified 2026-10-26.
- PostgreSQL: Documentation: 17: 19.6. Replication · Primary document · www.postgresql.org. Fence the old writer before publishing the new authority. Verified 2026-10-26.
- PostgreSQL: Documentation: 17: 26.2. Log-Shipping Standby Servers · Primary document · www.postgresql.org. Fence the old writer before publishing the new authority. Verified 2026-10-26.
Decision log
Fence the old writer before publishing the new authority.
Preserve commit identity and replication history ownership.
Revisit changed workload, observations or recovery objectives.