The interview problem

Practice prompt

A write service runs in region A with an asynchronous database replica in region B. A network partition makes A unreachable from B, but some customers can still reach A. Design a safe failover. Target five minutes to restore writes, and explain what a thirty-second recovery-point objective does and does not guarantee.

Original system-design practice, not a reported company interview. Timing and throughput are hypothetical. Primary PostgreSQL documentation retrieved September 27, 2026; interview date not applicable.

1. Separate three decisions

Failure suspicion, writer ownership and traffic routing are different decisions. A failed health check means a path did not respond, not that A stopped writing. Changing DNS moves some clients; it does not terminate old connections or revoke the old database's authority.

Require at most one authorized active writer for the service. Accepted-loss promotion can discard an acknowledged suffix; single-writer fencing alone does not preserve every prior write or establish linearizability across that loss. Reads may remain available from replicas under an explicit staleness contract. A client requiring read-after-write behavior must wait for a sufficient replication position or receive an unavailable response; an arbitrary regional replica cannot promise it. Include the database history/timeline in read tokens: a position from a discarded history cannot be satisfied merely because a new history reaches a numerically larger position.

Recovery time objective concerns restoration of the agreed service, while recovery point objective concerns recoverable data. An asynchronous replica can lose acknowledged writes if the primary's durable log cannot be recovered. A recent lag dashboard is evidence about an earlier moment, not proof that loss is bounded during the current partition.

2. Put authority outside the traffic router

independent health observations → failover controller
                                      ↓
                 durable ownership/transition record
                                      ↓
                 fence old writer → verify replica → promote
                                      ↓
                    route traffic → verify business writes

Use a consensus-backed control record with a monotonically increasing ownership epoch and a serialized transition state. Place its quorum across appropriate failure domains. A witness can arbitrate control decisions but does not magically contain missing application data. Losing quorum prevents a safe new ownership decision in this design.

Every path that can mutate data must obey fencing, including direct database clients, batch workers and scheduled jobs. One implementable approach is positive infrastructure fencing: stop or isolate the old writer with a trusted mechanism and confirm it cannot resume accepting writes before promotion. Another uses a write architecture that enforces current authority at commit. Merely recording a higher epoch in the controller does not fence an independent database still accepting old writes.

Lease-based designs require explicit clock, renewal and storage-enforcement assumptions. An isolated process checking a cached lease or relying on a delayed timer is insufficient. If fencing cannot be established, leave writes unavailable rather than promoting a competing writer.

3. Walk through the partition

At epoch 17, A is the writer. B's probes fail, while existing clients still write to A. The controller records a failover attempt without granting B write authority. It pauses new work where possible, but treats these pauses as load management rather than proof of fencing.

The controller obtains verified fencing of A, including restart protection. It then determines B's recoverable log position and whether the proposed recovery point meets the approved loss policy. If the last acknowledged position is unknown, the loss bound is unknown. Record that uncertainty and require the designated incident decision process before any accepted-loss promotion.

After fencing and the recovery decision, serialize promotion under epoch 18. Persist state transitions so retries discover whether promotion already occurred. Update connection routing, invalidate stale pools where supported and run an idempotent business write/read verification. Old clients must fail against fenced A even if DNS never refreshes.

A crash after promotion but before routing must resume the existing transition, not promote another independent history. A crash after fencing but before promotion causes downtime, but preserves single-writer safety. Durable checkpoints make these states diagnosable.

4. Work the data-loss example

Suppose A's last acknowledged committed log position corresponds to transaction 1,000,000 and B has durable recoverable history through 988,000. The missing suffix contains 12,000 transactions, assuming this example's contiguous transaction numbering corresponds to committed transactions. At a steady 400 commits/second that is thirty seconds of work. Real database log positions measure bytes, not transaction counts, so production accounting needs actual commit boundaries and timestamps.

If 988,000 is only the last observed replay position, B may already have later log records durably received but not replayed. Applying that log can reduce loss. Conversely, after an additional twenty seconds of isolated writes at 400/second, the gap grows to 20,000 transactions, or fifty seconds at that rate. The earlier thirty-second lag measurement did not enforce a thirty-second bound.

Zero loss of acknowledged writes requires an acknowledgement policy tied to durable replication to a failover-eligible data replica, plus a promotion rule selecting a sufficiently up-to-date survivor. This adds network latency and can stop writes when that replica is unavailable. A control-plane majority alone does not establish the data durability guarantee.

5. Budget recovery and catch-up

In an illustrative sequential recovery: detection takes 45 seconds, fencing 30, log verification/replay 60, promotion 20, routing/connection recovery 40 and business verification 25. Total is 220 seconds, or three minutes forty seconds, leaving eighty seconds against a five-minute objective. These are scenario timings, not a worst-case guarantee; inaccessible fencing or slow clients may exceed the budget.

For a planned transition, suppose replication backlog is 18 GB, apply throughput 150 MB/second and incoming log generation 50 MB/second, using decimal units. Net catch-up is 100 MB/second, so backlog clears in 180 seconds if these rates stay steady. If writes are stopped first, the same backlog applies in 120 seconds. If incoming generation exceeds apply throughput, waiting alone cannot catch up.

Do not substitute a zero lag gauge for a verified cutover position. Planned switchover should stop old writes, establish a final durable position, verify its receipt and replay at the new primary as required, fence the old role, then transfer ownership. Its stronger observability differs from an unplanned outage.

6. Recover external work without duplicate effects

Client timeouts leave unknown outcomes. Reuse stable business idempotency keys, and replicate their outcome records with the business mutation. However, asynchronous loss can remove both the mutation and its dedup record from B even though an external action already happened. A local idempotency table alone cannot prevent replaying that external action.

Use downstream idempotency where supported and reconcile ambiguous outcomes before resubmission. Outbox workers and message consumers must also be fenced from the former region. A promoted database does not automatically move queue ownership, object-storage consistency or third-party credentials into a safe state.

Keep unknown operations visible rather than marking them failed simply because their records are absent after failover. Reconcile against authoritative external receipts. Where the external system lacks idempotency or queryable status, manual resolution or a paused workflow may be necessary.

7. Treat failback as a new operation

When A returns, prevent its automatic primary startup. Preserve its divergent log for investigation and reconcile any recoverable acknowledged operations under the incident policy. Do not merge independent database histories by copying whichever rows have newer wall-clock timestamps.

Rebuild or safely rewind A from the new authoritative history using the database's supported procedure and prerequisites. Verify replication before considering a planned switch back. Remaining on B may be safer than immediately adding another failover. Restore redundancy and test backups; a successfully promoted single region is still a degraded resilience state.

Exercise partitions where both application regions are healthy, controller crashes at every transition, delayed DNS, old pooled connections and returning background workers. Success means no competing accepted writes and explainable recovery outcomes, not merely that the homepage loads.

8. Trade-offs and self-review

Asynchronous replication favors write latency and local availability at the cost of possible data loss. Synchronous remote durability improves recovery guarantees but couples writes to network and replica health. Conservative fencing may increase downtime; optimistic promotion risks irreconcilable histories.

Follow-ups: Does DNS failover fence a database? No. Can a witness recover missing writes? No. Does a higher epoch help if storage ignores it? No. Does a replica's replay lag equal its durable data gap? Not necessarily. Is automated failback always desirable? No.

Score 0–2 each for enforced fencing, explicit recovery-point uncertainty, recoverable transition states, timing arithmetic and external-effect reconciliation.

Sources and next steps

  • PostgreSQL 17: Failover — old-primary fencing, promotion and restoration of standby protection; page publication date not established.

Architecture and calculations are original practice. See database backup and restore for recovery from stored backups rather than a regional writer transition.

Continue your preparation

Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.