The migration that passed reconciliation
Failure story 15, in migration, backfills and cutovers. This one has a lab you can run, below.
The challenge
Section titled “The challenge”The old and new inventory systems contain the same number of reservations and the same total reserved quantity. Is it safe to cut over?
Look at the four rows in the setup before you answer.
The setup
Section titled “The setup”| Source (old system) | Reserved | Aspect | Target (new system) | Reserved |
|---|---|---|---|---|
| SKU-A | 7 | SKU-A | 3 | |
| SKU-B | 3 | SKU-B | 7 | |
| 2 rows | 10 units | 2 rows | 10 units |
| Assumption | Value |
|---|---|
| What reserved means | Units held for customer orders that haven’t shipped |
| How the data moved | A migration job mapped each product’s old internal ID to its new one, through a mapping table |
| The cutover gate | Row counts match, and total reserved quantity matches, for every table |
What happened
Section titled “What happened”| Step | What happens |
|---|---|
| 1 | Two entries in the mapping table are swapped, so SKU-A’s reservations are written to SKU-B and SKU-B’s to SKU-A. |
| 2 | The reconciliation job runs: 2 rows and 10 units on each side. The gate passes. |
| 3 | Cutover. The new system believes only 3 units of SKU-A are reserved, when 7 are. |
| 4 | It sells the 4 “spare” units of SKU-A, which were already promised to other customers: 4 orders that can’t be fulfilled. |
| 5 | It also believes 7 units of SKU-B are reserved, when 3 are, so it refuses to sell 4 units that are actually free. |
Why it went wrong
Section titled “Why it went wrong”Counts and sums are aggregates, and aggregates can’t see errors that cancel out. Swapped values, a missing row offset by an extra one, and a sign error matched by another all leave totals untouched. The check answered did roughly the right amount of data arrive? The cutover needed is every record right?, which only a comparison by key can answer.
Two designs
Section titled “Two designs”| Aspect | Design A: table counts and sums | Design B: keyed comparison |
|---|---|---|
| Compares | Totals per table | Every key, and every column that matters, on both sides |
| Finds swapped values | No | Yes: SKU-A −4, SKU-B +4 |
| Finds a missing row offset by an extra one | No | Yes: one missing from target, one only in target |
| Reports | Pass or fail | Each wrong key, its signed difference and the net difference across them |
| Cost | Trivial | A join or a hash comparison; see the exercise for large tables |
Run the lab
Section titled “Run the lab”Load a scenario, or edit the rows yourself, then run each check. The lab runs entirely in your browser.
Run either check to see its result here.
The same lab as SQL, for PostgreSQL or SQLite: download reconciliation-lab.sql. The keyed comparison is a full outer join on the key:
SELECT COALESCE(s.sku, t.sku) AS sku, s.reserved_qty AS source_qty, t.reserved_qty AS target_qty, COALESCE(t.reserved_qty, 0) - COALESCE(s.reserved_qty, 0) AS differenceFROM source_reservations sFULL OUTER JOIN target_reservations t ON t.sku = s.skuWHERE s.sku IS NULL OR t.sku IS NULL OR s.reserved_qty <> t.reserved_qty;The check
Section titled “The check”A cutover check is good enough when, for the four rows above, its report:
- identifies both incorrect SKUs, SKU-A and SKU-B;
- gives their signed differences, −4 and +4; and
- shows the zero net difference that concealed them, so nobody is tempted to trust the totals again.
Then add the business invariants the data must satisfy, which catch errors in data the comparison can’t see: reserved quantity never exceeds stock on hand, and every reservation belongs to an open order.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- As a quick early warning during a long migration, counts and sums are cheap and catch gross failures, such as a table that didn’t copy at all. They just can’t be the cutover gate.
- When the data has no meaningful key and is only ever used in aggregate, such as anonymous daily totals.
For educators
Section titled “For educators”Discussion question. Before running the lab, ask the group to invent another pair of tables that passes counts and sums but is wrong. How many different kinds of error can they find?
Exercise. The real table has 40 million rows, in two databases on different platforms, and a full outer join across them would take hours. Design a keyed comparison that runs in minutes and still names every wrong row.
Worked answer
Compare hashes, then drill down. In each database, compute a hash of each row’s key and the columns that matter, such as hash(sku || '|' || reserved_qty). Group rows into buckets by key, for example 4,096 buckets by hash(sku) mod 4096, and add up the row hashes in each bucket as 64-bit integers. Each side now produces 4,096 numbers, which are cheap to compare. A swapped value changes the row hashes, so the buckets containing SKU-A and SKU-B differ. Only for the buckets that differ, pull the rows from both sides and run the keyed comparison on those. Use sums of hashes rather than XOR: XOR makes two identical rows cancel out, which hides duplicates. With 40 million rows and a handful of errors, you compare 4,096 numbers, then a few thousand rows.
Related pages
Section titled “Related pages”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.