Change data feed: Recover a gold table after an outage
A record with four rejects moves from station A to B and is corrected to three rejects. Simply adding the new value overstates the total. The Delta change data feed can reveal both sides of the correction.
Start from a known table version
Our gold table holds totals from a consistent silver snapshot through version 40. A starts at 10 and B at 5. Here the physical silver table contains active records. The change feed types are insert, delete, update_preimage and update_postimage. If your silver table uses logical deletion flags, also account for whether each before and after image represents an active record.
| Version | Change | Effect on A | Effect on B |
|---|---|---|---|
| 41 | Move E7 from A/4 to B/3 | −4 | +3 |
| 42 | Delete a different B/2 record | 0 | −2 |
| 41–42 | Net effect | −4 | +1 |
- Version 40A = 10 · B = 5Initial total 15
- Version 41A = 6 · B = 8Total after the move 14
- Version 42A = 6 · B = 6Total after deletion 12
Subtract the before image and add the after image
For the update, the before image subtracts four from A. The after image adds three to B. Group by the station on each change row. Mapping both images to the new station first can give the correct plant total and incorrect station totals at the same time. For a rate, perform independent delta calculations on the numerator and denominator. Divide only after aggregating.
SELECT station, SUM(
CASE _change_type
WHEN 'insert' THEN rejected
WHEN 'update_postimage' THEN rejected
WHEN 'update_preimage' THEN -rejected
WHEN 'delete' THEN -rejected
END
) AS reject_delta
FROM table_changes('factory.silver.runs', 41, 42)
GROUP BY station;The SQL reads versions 41 through 42 inclusive. It assumes change data is available for that interval and rejected is non-null. The initial snapshot already includes version 40. The next run starts at 43. A commit version orders Delta table changes. It does not determine the operational day of a production record that arrived late.
Survive a crash between writing data and recording progress
Imagine gold has been updated but the application crashes before recording its processed version. Repeating an unconditional addition applies the change twice. One design stores contributions keyed by source table, commit version and station in a Delta table. Compute each contribution deterministically and replace the same key on replay. The gold view sums the initial snapshot and these contributions. For that design, group by _commit_version as well. The example query combines both versions only to reproduce the hand calculation.
Structured Streaming can recover progress from a durable checkpoint dedicated to its query. Custom side effects in foreachBatch still need repeatable writes. After a simulated failure, compare station totals against a complete aggregation of the same silver version. Change data is retained for a limited period. If a required version has expired, establish a new consistent snapshot and record its version before resuming at the following version.
Verify what a correct replay actually writes
For the contribution design, the target needs three keys over the selected interval. A replay replaces the contribution at the same key. Reading version 41 again therefore leaves the same two values. Include source table identity when consuming several tables. A version number is unique only within its own Delta table.
| Commit version | Station | Reject delta |
|---|---|---|
| 41 | A | −4 |
| 41 | B | +3 |
| 42 | B | −2 |
Compare both the three keys and the totals per station before and after a replay. A plant total alone could hide a wrong allocation between A and B. If several pipelines can run concurrently, coordinate writes and progress tracking so a successful run cannot advance beyond a version that another run has yet to process.
Try it yourself
After recovery you see A = 2 and B = 7, although the source remains at version 42. Which failure would explain both values?
Show the solution
The net effect (−4, +1) was applied twice. Starting at (10, 5) gives (6, 6) on the first application and (2, 7) on the second. Inspect contribution keys and commit versions. If necessary rebuild from the last correct snapshot, then repeat the same restart test.