Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

After a regional outage the team promoted the cross-region PostgreSQL replica in eu-west and ran there for six hours. When us-east recovered, they pointed traffic back at the old primary. The next morning, orders created during those six hours are missing from reports.

Explain what went wrong in the failback, how you would recover the orders, and what the failback runbook should say.

During the outage, eu-west became the primary and took six hours of orders, but nothing copied those writes back to the old us-east primary, so pointing traffic back at us-east made the database fall back to its pre-outage state and the orders disappeared from view. To recover, we stop writes to us-east, compare both nodes from the point where they diverged and either copy the missing orders across or make eu-west the primary again. A correct failback rebuilds the old primary as a replica of the new one, with pg_rewind or a fresh base backup, waits for it to catch up and then does a planned switchover.

Why this question is asked

Failover gets all the attention in disaster recovery plans, but failback is where data quietly goes missing, because it looks like simply undoing what we did. Interviewers ask this scenario to see whether we understand that after a promotion the two databases have different histories, and that only one of them can be the truth. In this answer, we will recap what promotion does to PostgreSQL's timeline, trace exactly what went wrong, recover the missing orders safely, and write the runbook that a failback should follow next time.

1A Quick Recap: Promotion Starts a New History

With streaming replication, a PostgreSQL replica replays the primary's write-ahead log (WAL), the ordered record of every change, so it holds the same data a little later. Cross-region replication is usually asynchronous: the primary commits without waiting for the replica, so the replica can be seconds behind.

When we promote the replica, it stops following and starts accepting writes. PostgreSQL marks this by moving to a new timeline, a numbered branch of WAL history, for example from timeline 1 to timeline 2. From that moment the two servers have separate histories: eu-west continues on timeline 2, and the old primary, if it comes back, still believes it is on timeline 1. Nothing in PostgreSQL merges them.

2What Went Wrong in the Failback

For six hours, every order went to eu-west on timeline 2. When us-east recovered, its PostgreSQL started with the data it had at the moment of the outage. Nobody made it a replica of eu-west, so none of the six hours of writes reached it. Pointing the application back at us-east by changing DNS made the application read and write the old history. The orders were not deleted; they were simply on the other server, which nobody was reading any more.

There is a second, subtler loss. Because replication was asynchronous, us-east may have committed a few transactions just before the outage that never reached eu-west. Those exist only on us-east. And since the switch back, us-east has taken new orders overnight, which exist only on us-east too. So now both servers hold some orders the other lacks, and we must reconcile carefully rather than copy one over the other.

Step throughHow six hours of orders went missing
Appus-east (old primary)eu-west (replica)
stream WAL (asynchronous)
outage; last few commits not yet sent
promote eu-west: timeline 2 begins
six hours of orders
us-east recovers with pre-outage data
DNS switched back: app uses us-east
reports read us-east: six hours missing

7 messages in order. Play them, or step through them one at a time.

Nothing ever copied eu-west's timeline 2 writes back to us-east.

3Recovering the Missing Orders

First, stop the bleeding: put the application into maintenance or a write freeze so neither server diverges further while we work. Then find the divergence point. On eu-west, the timeline history file (for example 00000002.history) records the WAL position where timeline 2 began; everything before it is shared history.

Next, compare the two servers from that point. Orders have ids and creation timestamps, so the practical comparison is by table: list orders on eu-west created during the six-hour window, orders on us-east created after the switch back, and orders on us-east from the last seconds before the outage that eu-west never received.

Then pick the simpler direction. Often the cleanest choice is to make eu-west the primary again, since it holds the six hours, and copy the smaller set of us-east-only orders (the overnight ones and the pre-outage tail) into it with their ids, checking for id collisions if sequences overlapped. The alternative, copying six hours of orders and every related row (payments, line items, stock movements) into us-east, is larger and easier to get wrong. Either way, the copy is a reviewed script run inside transactions, and we reconcile counts and totals per hour afterwards.

SQL
-- on eu-west: where did timeline 2 start?
SELECT * FROM pg_control_checkpoint(); -- shows the current timeline id
-- 00000002.history in pg_wal: 1 0/5A000110 before 2026-11-02 03:14:07+00
-- orders that exist on only one side (run on each, compare by id)
SELECT id, created_at, total FROM orders
WHERE created_at >= '2026-11-02 03:14:07+00'
ORDER BY id;

4The Correct Failback Procedure

The right way back is to turn the old primary into a replica of the new one, let it catch up, and then switch over in a planned, lossless way. pg_rewind is the tool for the first step: it compares the old primary's data directory with the new primary, finds where they diverged and rewinds the old one to that point, so it can then follow timeline 2. It needs wal_log_hints = on or data checksums enabled. If pg_rewind cannot be used, we take a fresh base backup from eu-west with pg_basebackup.

But pg_rewind throws away the old primary's divergent tail, the commits that only it has. So before running it, we extract those transactions, using the order comparison above or by decoding WAL with pg_waldump, and keep them for reconciliation. Discarding them silently would turn one data loss into another.

Once us-east is replicating from eu-west and the lag is zero, we do a planned switchover: freeze writes, confirm the replica has replayed everything, promote us-east, point eu-west at it as a replica, and lift the freeze. Done this way, the switchover loses nothing.

Step throughA safe failback runbook
  1. Freeze writes to the old primaryit must not take traffic yet
  2. Extract commits only the old primary haskeep them for reconciliation
  3. pg_rewind or fresh base backupold primary becomes a replica of the new
  4. Replication caught up?
    YesPlan the switchoverNoWait and watch lag
  5. Write freeze, promote, repoint DNSplanned switchover, nothing lost
  6. Verify counts and recent ordersthen lift the freeze

6 steps in order. Play them, or step through them one at a time.

5What the Runbook Should Say

The runbook should treat failback as a planned operation with its own checklist, never as reversing DNS. It names who decides, the preconditions (old primary rebuilt as a replica, lag at zero, no unreconciled divergent commits), the steps in order and a verification step after each: row counts per hour for the outage window, the newest order id on both sides, and a test order that must appear in reports.

It should also be rehearsed. Running the full failover and failback in a staging environment once a quarter, with timings recorded, catches missing permissions and forgotten settings before an outage does. Tools such as Patroni can automate the rejoin with pg_rewind, but the decision to fail back and the reconciliation of divergent data stay with people.

6Follow-up questions

What would you automate in this runbook and what would you keep manual?

I would automate the mechanical, checkable steps: rebuilding the old primary as a replica with pg_rewind or a base backup, monitoring lag, the switchover sequence itself and the verification queries, for example with Patroni or scripts. I would keep manual the decision to fail back, the review of divergent commits and the reconciliation, because those need judgement about business data and are rare enough that speed matters less than care.

Why not fail back immediately once us-east is healthy?

Because us-east is not current. Until it has replayed every write from eu-west, switching to it loses data. There is also no urgency: eu-west is serving traffic fine, so the failback can wait for a quiet hour.

Could synchronous replication have prevented this?

It would have removed the pre-outage tail, because commits would wait for eu-west, but at the cost of a cross-region round trip on every commit. It would not have prevented this incident, which came from switching back to a server that had never received the new writes.

7Conclusion

In this answer, we saw that promoting eu-west started a new timeline, and that switching traffic back to us-east without rebuilding it made the application use the old history, hiding six hours of orders. We recovered by freezing writes, finding the divergence point, comparing orders on both sides and reconciling in the simpler direction. The correct failback rebuilds the old primary as a replica with pg_rewind or a base backup, after extracting its divergent commits, waits for it to catch up and switches over in a planned, verified, rehearsed way.

What to say in the interviewAbout 80 seconds aloud

Open with

What went wrong is that failback was treated as reversing DNS. When we promoted eu-west, it started a new timeline and took six hours of orders. When us-east came back, it still had only its pre-outage data, and nothing had replicated eu-west's writes to it, so pointing traffic back made the application read the old history.

Then, if they want more

The orders aren't gone, they're on eu-west. Us-east may also have a few commits from just before the outage that never replicated, plus whatever it took overnight. To recover, I'd freeze writes, find the divergence point from eu-west's timeline history, and compare orders on both sides by id and creation time.

Usually the simplest fix is to make eu-west the primary again and copy the smaller set of us-east-only orders into it, with a reviewed script and reconciled totals.

The runbook should say that failback is a planned operation: rebuild the old primary as a replica of the new one with pg_rewind, which needs wal_log_hints or checksums, or with a fresh base backup; before rewinding, extract the commits only the old primary has; wait until lag is zero; then freeze writes, promote, repoint and verify with counts and a test order. And we rehearse it every quarter so it isn't improvised during an incident.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain what went wrong in the failback, how you would recover the orders, and what the...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/0bf6e4e3b6/