Skip to content

The backfill that overwrote a live update

Failure story 13, in migration, backfills and cutovers.

Live replication delivered revision 8 of a customer to the new database. A few minutes later, the new database holds revision 7. Nothing failed. What happened?

Assumption Value
Migration Customers move from the old database to a new one while the old one stays live
Live path Change data capture (CDC) streams every change from the old database to the new one, within seconds
Backfill A batch job copies all 4 million existing customers, 10,000 at a time; it started at the same time as the live path
Writes to the new database Both paths upsert unconditionally: insert, or replace the whole row
Time Backfill Live path Customer C-42 in the new database
10:00:00 Reads a batch including C-42 at revision 7 (absent)
10:00:05 The customer changes their email in the old system: revision 8 (absent)
10:00:06 Writes revision 8 revision 8
10:00:40 Writes its batch, including C-42 at revision 7 revision 7
Later Finishes No more changes to C-42 revision 7, permanently

The customer’s new email is lost in the new system. The old system has it, so a row-count reconciliation passes and nobody notices until the customer stops receiving mail.

Two writers delivered versions of the same row, and the target applied whichever arrived last, not whichever was newest. The backfill held its data for 40 seconds between reading and writing, which is ordinary for a batch job, so it was always going to be the later writer for any row that changed during that window. With 4 million rows and a busy system, that’s not a rare race; it’s thousands of rows.

Aspect Design A: unconditional writes Design B: source-revision-aware writes
What each write carries The row The row and its revision in the source: a version column, or the source log position (LSN)
What the target does Replaces whatever is there Applies the write only if it’s newer than the stored revision
Arrival order matters Yes No
INSERT INTO customers AS t (id, email, name, source_rev)
VALUES (:id, :email, :name, :rev)
ON CONFLICT (id) DO UPDATE
SET email = EXCLUDED.email, name = EXCLUDED.name, source_rev = EXCLUDED.source_rev
WHERE t.source_rev < EXCLUDED.source_rev; -- older writes do nothing

The comparison must use the source’s notion of order. Timestamps from two different machines, or the time the write arrived, don’t work: they’re exactly what went wrong.

Run the race deliberately, in both orders.

  1. Live first, backfill second. Write revision 8 through the live path, then revision 7 through the backfill. Expected: revision 8.
  2. Backfill first, live second. Write revision 7 through the backfill, then revision 8 through the live path. Expected: revision 8.
  3. Full comparison. After a backfill that runs while a load generator updates customers, compare every row’s revision between source and target. Expected: no row in the target has a lower revision than the source.

Check 3 is the one that finds the problem in a real migration. Counts and sums won’t (see the migration that passed reconciliation).

This check is in the checks catalog as test templates in Java and Python.

  • The source is frozen while the backfill runs, so nothing changes during it. That’s the simplest correct migration, if the business can accept the downtime.
  • The live path starts only after the backfill finishes and replays every change from a log position recorded before the backfill started. Then the live path’s later writes always win, and they’re newer.

Discussion question. Why do teams start the backfill and the live path together, when the second option above avoids the race?

Exercise. The source table has no version column, and you can’t add one. What can the backfill use as the revision, so it compares correctly with the live path’s log positions?

Worked answer

Use the source database’s log position as the revision for both paths. Many databases can export a consistent snapshot tied to a log position: the backfill reads from that snapshot and stamps every row with the snapshot’s position, and the live path stamps each change with its own position. Any change made after the snapshot has a higher position, so it always wins. Change-data-capture tools take this further, interleaving snapshot chunks with the live stream and using watermarks to drop snapshot rows that the stream has already superseded. What doesn’t work: the backfill machine’s clock.

Written and edited by Abhinav Taduka, enterprise software architect.