Quick revision
The short answer
Caught up only meant the stream had applied everything committed up to the moment of the check; transactions still in flight on the old shard committed a moment later, and routing switched before the stream delivered them, so those updates stayed only on the source. We repair bucket 412 by replaying the source's change log from before the switch, comparing by primary key and version and applying what is missing in order. And we change the cutover to fence first: block writes for the bucket at the router and on the source, wait for in-flight transactions, record the source log position, and switch only when the target has applied up to it.
What to remember
In this answer, we saw that the target was caught up only for the moment of the check; transactions in flight on the source committed afterwards and never reached the target because routing had already switched. The fix fences before checking: mark the bucket MOVING at the router and on the source, wait for in-flight transactions, record the LSN or GTID position, and switch only when the target has applied up to it, which costs a short retryable pause. We repaired bucket 412 by replaying the source's change log from before the switch with conflict checks, and made checksum verification a required step before any source copy is deleted.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Caught up only meant the stream had applied everything committed up to the moment of the check; transactions still in flight on the old shard committed a moment later, and routing switched before the stream delivered them, so those updates stayed only on the source. We repair bucket 412 by replaying the source's change log from before the switch, comparing by primary key and version and applying what is missing in order. And we change the cutover to fence first: block writes for the bucket at the router and on the source, wait for in-flight transactions, record the source log position, and switch only when the target has applied up to it.
Why this question is asked
Online data moves are one of the riskiest routine operations in a sharded system, and the classic failure is not a crash but silent loss: a few rows quietly missing updates, found a day later by reconciliation. Interviewers ask this scenario because it tests whether we can reason precisely about time, about what caught up really means, and about the difference between a check that is usually right and a protocol that is always right. In this answer, we will recap how the move works, reconstruct the race, design a fenced cutover, repair the lost updates, and make verification automatic.
1A Quick Recap of the Online Split
Moving bucket 412 online has three phases. First a bulk copy of the bucket's rows from the source shard to the new shard. Second, change data capture: a stream reads the source's write-ahead log (WAL in PostgreSQL, the binlog in MySQL) and applies every later change for bucket 412 to the target, so the target keeps up while users keep writing. Third, the cutover: at some point the router switches bucket 412 to the new shard, and from then on all reads and writes go there.
The cutover is where correctness is decided. Before it, the source is the truth. After it, the target is. The question is whether every change made to the source has reached the target by the moment the target becomes the truth.
2Reconstructing the Race
The procedure checked the stream's lag, saw that it had applied everything committed so far, declared the target caught up and switched routing. The flaw is that caught up is a statement about a moment. At that moment, several transactions on the source were still running: they had written rows for bucket 412 but not yet committed. The change stream only delivers committed changes, so those writes were not yet in the stream at all.
A moment later, those transactions committed on the source. Their changes entered the WAL and would eventually have been delivered by the stream, but the move procedure had already switched routing and, likely, stopped the stream. New reads and writes went to the target, which never received those last commits. Updates committed in the roughly two seconds before the switch were lost from the target, which matches what reconciliation found: about 0.3 percent of rows missing updates from exactly that window.
6 messages in order. Play them, or step through them one at a time.
The check was correct for its moment; the switch happened after the moment changed.
3A Cutover That Guarantees Completeness
The fix is to stop new changes before checking, so that caught up stays true. This is called fencing. The steps, in order, are as follows. Mark bucket 412 as MOVING at the router, so new requests for it get a short, retryable error instead of reaching the source. Also mark it MOVING on the source shard itself, which rejects any write for the bucket, because some router may still have an old map. Wait for the transactions already running on the source for that bucket to finish, either commit or abort; a lock on the bucket's ownership row that every writing transaction holds in shared mode makes this precise, because the MOVING update must wait for them. Then record the source's current log position: in PostgreSQL, pg_current_wal_lsn(); in MySQL, the executed GTID set. Every change for the bucket is now at or before that position. Wait until the CDC stream reports that the target has applied up to that position. Only then switch the directory to the target with a new epoch and unblock.
- Router marks bucket 412 MOVINGnew requests get a retryable error
- Source rejects writes for bucket 412protects against stale routers
- Wait for in-flight transactionsownership row lock makes this exact
- Record source positionLSN or GTID set
- Target applied up to that position?YesSwitch directory with a new epoch, unblockNoKeep waiting
5 steps in order. Play them, or step through them one at a time.
Notice what changed: instead of checking lag at an arbitrary moment, we create a moment after which no change can happen and wait for the target to reach exactly that point. Completeness is now guaranteed by construction, not inferred from a lag metric.
4How Long Writers Wait
Fencing means a write pause for the bucket, so its length matters. It covers the time for in-flight transactions to finish, usually milliseconds for an OLTP system, plus the time for the stream to apply the last few changes, usually well under a second when it was already nearly caught up. With buckets of a reasonable size, the pause is typically well under a second. Clients see a short, retryable error, for example a 503 with Retry-After or a specific error code their retry logic recognises, and retry after a brief backoff. To keep it short, we start the fence only when the stream's lag is already close to zero, and we put an upper bound on the wait: if a long-running transaction does not finish within, say, 5 seconds, we abort the cutover, unblock the bucket on the source and try again later.
5Repairing Bucket 412
The lost updates still exist in the source's history. We first make sure the old copy of bucket 412 on the source is read-only and not yet deleted. Then we read the source's change log for bucket 412 from a point safely before the switch, for example a minute earlier, through the end of its life on the source, using the same CDC tooling or the retained WAL or binlog. For each changed row, we compare the source version with the target by primary key and a version column or updated_at. Where the target is missing the change and the row has not been modified on the target since the switch, we apply the change in commit order. Where the row has also been edited on the target after the switch, a blind replay would overwrite a newer edit, so those few rows are listed and resolved deliberately, with the business owner where needed. Finally, we rerun reconciliation for the bucket to confirm it is clean.
6Verifying Every Move Automatically
This incident was found by reconciliation a day later; it should have been found by the move itself. Every move ends with a verification step before the source copy is deleted: row counts for the bucket on both sides, and checksums per chunk of primary keys, for example a hash of all row contents for keys 1 to 10,000, then 10,001 to 20,000, compared between source and target. Because writes continue on the target after the switch, the comparison is done against the target's state at the fence position, or repeated until stable. A mismatch blocks deletion of the source copy and raises an alert. Only after verification passes, and a safety period has passed, is the source data removed.
7Follow-up questions
How would you keep the fence safe if some router still has an old map?
By enforcing it on the source shard, not only at the routers. The source's ownership row for bucket 412 is set to MOVING, and every write transaction for the bucket checks that row, through a trigger or a guarded statement with a shared lock, together with the epoch the router sent. A router with an old map reaches the source, the check fails, and the router gets an error, refreshes its map and retries against the target after the switch. Routers are hints; the shard holding the data is the authority.
Why not just wait a few extra seconds after the lag reaches zero?
Because there is no safe number. A transaction can stay open for seconds or minutes, for example a slow batch or a stuck connection, and commit after any fixed delay. Only fencing plus waiting for a recorded log position closes the window for every transaction.
8Conclusion
In this answer, we saw that the target was caught up only for the moment of the check; transactions in flight on the source committed afterwards and never reached the target because routing had already switched. The fix fences before checking: mark the bucket MOVING at the router and on the source, wait for in-flight transactions, record the LSN or GTID position, and switch only when the target has applied up to it, which costs a short retryable pause. We repaired bucket 412 by replaying the source's change log from before the switch with conflict checks, and made checksum verification a required step before any source copy is deleted.
Open with
Caught up meant the stream had applied everything committed up to the moment we checked. But some transactions on the old shard were still in flight at that moment; they had written rows for bucket 412 but not committed, so they were not in the stream yet.
Then, if they want more
They committed a second later, and by then routing had switched and the stream was stopped, so those updates only exist on the source. That is the 0.3 percent from the last two seconds. The cutover has to fence first.
Mark the bucket MOVING at the router, and also on the source shard so it rejects writes even from routers with an old map. Wait for in-flight transactions to finish, which an ownership row lock makes exact.
Record the source's log position, the LSN or GTID set, and switch the directory with a new epoch only once the target has applied up to that position. With small buckets the pause is usually well under a second and clients get a retryable error.
To repair, I keep the source copy read-only, replay its change log for bucket 412 from a margin before the switch, compare by primary key and version, apply missing updates in order, and handle rows edited on the target since then by hand. And every move should verify itself with row counts and chunk checksums before the source is deleted.
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 the race that lost these updates, how you would repair the data, and how you...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/cf22cd0894/