Idempotency Across Several Tables
The nightly run for ds = '2026-03-14' writes 3 tables in order: fact_bookings, then agg_daily_revenue built from it, then host_payout_summary built from both. At 02:31 the third task fails on a lost connection. The first 2 tables hold fresh March 14 data. The third still holds whatever an earlier attempt for that day left, or nothing.
- Raw bookings (source, Raw table)
- Build fact (transform, Orchestrator); idempotency: partition overwrite
- fact_bookings (storage, Partitioned by ds)
- Build revenue (transform, Orchestrator); idempotency: partition overwrite
- agg_daily_revenue (storage, Partitioned by ds)
- Build payouts (transform, Orchestrator); on error: failed at 02:31
- host_payout_summary (storage, Partitioned by ds)
- Raw bookings -> Build fact
- Build fact -> fact_bookings
- fact_bookings -> Build revenue
- Build revenue -> agg_daily_revenue
- fact_bookings -> Build payouts
- agg_daily_revenue -> Build payouts
- Build payouts -> host_payout_summary
| Table | After the failure at 02:31 | After the rerun at 06:10 |
|---|---|---|
| fact_bookings | March 14 replaced, 41,200 rows | Replaced again with the same 41,200 rows |
| agg_daily_revenue | March 14 replaced | Replaced again with identical totals |
| host_payout_summary | Earlier contents for March 14 | March 14 replaced for the first time this run |
The rerun converges if each table's write is idempotent by itself and scoped to the run's logical date: tables 1 and 2 are replaced with identical contents, and table 3 catches up. Retrying only the failed task also works, provided the tables it reads still hold this run's output. Rerunning the whole run is the safer default whenever an upstream table might have changed in between.
The window between failure and rerun
Convergence fixes the end state. It does nothing for readers between 02:31 and 06:10. A dashboard that joins agg_daily_revenue to host_payout_summary shows corrected revenue next to stale payouts, and an analyst who exports it at 05:00 ships numbers that never existed together. Publishing last or writing a completion marker closes that window.
Publish last
Each task writes to a staging copy of its table, scoped to the run's date, and readers never query staging. A final publish task copies each staging partition into the table readers use, by partition overwrite. A failure in any build task leaves the reader tables on the previous complete version of March 14. The publish task still writes 3 tables. But it runs in seconds and holds no business logic, so the window shrinks from hours to moments. Like every other write, the publish is idempotent.
A completion marker
The run's last task writes a row to run_status saying March 14 is complete. It uses MERGE on ds, so a rerun updates that row instead of adding a second one. Downstream jobs wait for the row, and readers filter on it. On a rerun or backfill, the marker already says complete from the earlier success, so the rerun's first task must set it to running before touching any table. Otherwise readers trust March 14 while its tables change underneath them.
SELECT
r.ds,
r.revenue_usd,
p.total_payout_usd
FROM agg_daily_revenue AS r
JOIN host_payout_summary AS p
ON p.ds = r.ds
JOIN run_status AS s
ON s.ds = r.ds
WHERE s.status = 'complete'A run builds 3 tables in staging, publishes them by partition overwrite, and then writes the complete marker. On the day's first run, the publish succeeds for 2 tables and fails on the third. What do readers who filter on the marker see for that day?
- No data for the day until a rerun publishes all 3 tables and writes the marker
- The 2 published tables, with the third table missing from the join
- The staging contents of the third table in place of its reader table
- An error, since the published tables and the marker disagree
A single appending write breaks the run
The run is idempotent only if every write in it is. A step that appends a row to payout_events on each run, or calls the payments service to pay hosts, turns a safe rerun into duplicate events or double payouts. Key each such write on the logical date and the step, and MERGE on that key. Send that key with each payment call so the service skips repeats. A local log of sent keys leaves a gap: a crash between payment and log write pays the host twice. The advanced lesson builds this out.
A run writes fact_bookings by partition overwrite. It then MERGEs dim_guest on a uuid() generated each run, and finally marks the day complete in run_status. The marker task fails, and the whole run is retried. What is true after the retry succeeds?
- Every table is correct, since the first attempt never wrote the marker
- fact_bookings holds duplicates, since it was written twice
- dim_guest holds every guest twice, under 2 different keys
- run_status holds 2 complete rows for the day
About this lesson
This section is part of the Idempotent Reruns: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.