Scrivio Interview library
ScenarioIntermediate7 min read · updated 13 November 2026

An order database used asynchronous replication. The primary crashed and the most current replica was promoted automatically, but the old primary had about 40 seconds of commits the replica never received, including orders already confirmed to customers by email.

New orders now receive ids that the lost orders had used, and those ids were already sent to the payment provider and to analytics. Explain why, and how you would recover and prevent it.

With asynchronous replication, commits the primary acknowledged but had not yet shipped were lost when the replica was promoted, and the replica's sequence or auto-increment counter was behind, so it handed out the lost orders' ids again. Because the payment provider and analytics already hold those ids, one id now names two orders. We recover by fencing the old primary, extracting its transactions after the divergence point, re-creating the lost orders under new ids with a mapping checked against the provider, and telling downstream systems. We prevent it with id gaps or database-independent ids, and with synchronous or semi-synchronous replication plus a failover policy that promotes only a replica holding every acknowledged commit.

Why this question is asked

This is the failure that makes people take replication modes seriously. A failover that looks successful, with the database up again in seconds, has quietly lost confirmed orders and corrupted the meaning of order ids across three systems. Interviewers ask about it at this level because the answer combines replication internals, incident recovery and system design. In this answer, we will recap asynchronous replication, explain exactly why ids were reused, walk through a recovery, and then look at how to prevent both the reuse and the loss.

1A Quick Recap: What Asynchronous Means

In asynchronous replication, the primary commits a transaction locally, acknowledges it to the client, and ships it to replicas afterwards. Usually the gap is milliseconds, but under load or network trouble it grows. Whatever the primary committed but had not yet sent is held only on the primary.

When the primary crashes and a replica is promoted, the new primary has everything up to the last change it received, and nothing after. Here that is about 40 seconds of commits, including orders whose confirmation emails already went out. To the application, the database looks perfectly healthy; it just has a slightly older past.

2Why the Same Ids Were Handed Out Again

Order ids come from a sequence in PostgreSQL or an auto-increment counter in MySQL. On the old primary, the counter had reached, say, 900,400 when it crashed, and orders 900,121 to 900,400 were created in those last 40 seconds. The replica only saw changes up to order 900,120, so after promotion its counter continues from there and the next new order is 900,121 again.

Inside the new primary everything is consistent: there is exactly one order 900,121. The damage is outside. The payment provider has a charge referencing order 900,121 from the lost order, analytics has an event for it, and the customer has an email. Now a different customer's new order also carries 900,121. Payments can be matched to the wrong order, refunds can go to the wrong person, and analytics counts two orders as one, or one as two.

Step throughHow a lost commit leads to a reused id
ClientOld primaryReplicaPayment provider
create order
commit id 900121, not yet shipped
charge for order 900121
confirmed (email sent)
crash
promoted; counter still at 900120
create another order
confirmed with id 900121 again

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

The provider now holds one id for two different orders.

3Recovering the Lost Orders

The first step is fencing: make certain the old primary can never accept writes or be put back into service as it is, by shutting it down, cutting its network access, or revoking its storage. Two primaries both handing out ids would make things much worse.

Next we find the divergence point, the last change the new primary received from the old one. In PostgreSQL this is the LSN where the new timeline began; in MySQL it is visible by comparing the GTID sets, because every transaction on the old primary that is not in the new primary's gtid_executed was lost. We then extract those transactions from the old primary's data files, WAL or binlog, for example by starting a copy of the old primary's data in isolation and reading the orders created after the divergence point.

Then we re-create each lost order on the new primary under a new id, and keep a mapping table from the lost id to the new id. Each recreated order is checked against the payment provider's records, which are the outside truth: if the provider has a charge for a lost id, that order was real. Finally, downstream systems are told about the remapping through an explicit correction event or a support process, and the reused ids that new orders received are flagged so payments for them are matched by more than the id.

4Preventing Id Reuse

Two simple changes stop a failover from reusing ids. The first is to advance the counter by a large gap as part of every promotion, well beyond anything a few seconds of lost commits could have used. The second, more robust, is to stop using the database counter for any id that leaves the system.

SQL
-- part of the promotion runbook (PostgreSQL)
SELECT setval('orders_id_seq', nextval('orders_id_seq') + 1000000); -- 1900121: far past any lost id
-- MySQL equivalent
ALTER TABLE orders AUTO_INCREMENT = 1900121;
-- or ids that do not depend on the counter at all
-- order_ref = UUIDv7 or a Snowflake-style id: time + node + sequence
-- 0192f3a8-6c1e-7b4a-9d2e-5f0a1c3b7e21 unique even if the database loses commits

Notice that a gap only prevents reuse; the lost orders are still lost. Time-based ids such as UUIDv7 or a Snowflake-style generator are created by the application, so even after a failover the new primary can never produce an id that existed before. Many systems keep a bigint primary key internally and expose only such an external reference to payment providers and partners.

5Preventing the Loss Itself

To not lose acknowledged commits, the primary must not acknowledge a commit until at least one replica has it. In PostgreSQL that is synchronous replication: synchronous_standby_names lists the standbys, and synchronous_commit = on makes each commit wait until a synchronous standby has flushed it. In MySQL it is semi-synchronous replication, where with rpl_semi_sync_source_wait_point = AFTER_SYNC the source waits for a replica to receive the transaction before committing it visibly.

The cost is latency, one network round trip per commit, which is around a millisecond inside one region, and a decision about what happens when no synchronous replica is reachable. That is why we apply it to order data, not necessarily to everything. The other half is the failover policy: the orchestration tool must promote only a replica known to have every acknowledged commit, the synchronous one, and refuse to promote a lagging replica automatically. With that, failover loses nothing that was confirmed to a customer.

6Follow-up questions

How would you detect such divergence automatically after every failover?

Make it a step of the failover itself. Before or right after promotion, the orchestrator compares the old primary's last known position with what the new primary received: in MySQL, whether the old primary's executed GTID set is a subset of the new primary's (GTID_SUBSET), and in PostgreSQL, whether the old primary's last flushed LSN is past the point where the new timeline began. If the old primary is unreachable, its last position reported to monitoring is used. Any difference raises an incident with the count of lost transactions, and a job compares order ids issued after the failover with ids already sent to the payment provider.

Does semi-synchronous replication guarantee no loss?

Only if the setup is right. If the source times out waiting for a replica, it falls back to asynchronous mode after rpl_semi_sync_source_timeout and keeps committing, and the failover tool must also promote the replica that actually received the transactions. Check both settings, and alert when the source drops to asynchronous mode.

Why not just tell the payment provider to cancel the lost orders?

Because those customers paid and were told their order was confirmed. The right outcome is to honour them, which needs the lost data, a new id and a mapping, not a cancellation.

7Conclusion

In this answer, we saw that asynchronous replication lost the last 40 seconds of acknowledged commits on failover, and the promoted replica's counter, being behind, reissued their ids, so ids already sent to the payment provider and analytics now named two orders. We recovered by fencing the old primary, extracting transactions after the divergence point, re-creating them under new ids with a mapping verified against the provider, and informing downstream systems. Counter gaps and time-based external ids prevent reuse, and synchronous or semi-synchronous replication with a careful promotion policy prevents the loss.

What to say in the interviewAbout 90 seconds aloud

Open with

The root cause is asynchronous replication. The old primary acknowledged orders and sent confirmation emails before shipping those commits to the replica, so when it crashed, the last 40 seconds existed only on the old primary.

Then, if they want more

The promoted replica had the data up to that point, and so its sequence or auto-increment counter was also behind. It simply continued from the last id it knew, handing out the lost orders' ids to new orders.

Inside the database that is consistent, but the payment provider and analytics already hold those ids, so one id now names two different orders. To recover, I would first fence the old primary so it can never come back as a writer.

Then I find the divergence point, from the GTID sets in MySQL or the timeline switch LSN in PostgreSQL, extract the old primary's transactions after it, and re-create the lost orders under new ids with a mapping table, verified against the payment provider's charges.

Downstream systems get a correction for the remapping, and payments on the reused ids are matched by more than the id. To prevent reuse, the promotion runbook bumps the sequence by a large gap, and better, anything we send outside uses an application-generated id like UUIDv7.

To prevent the loss itself, order data uses synchronous or semi-synchronous replication, and failover only promotes a replica known to have every acknowledged commit.

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 "New orders now receive ids that the lost orders had used, and those ids were already...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/c572071656/