The Half-Finished Job

Concepts covered: Distributed Primitives

Most jobs write more than once: a detail table, then a rollup, then a signal telling a dashboard to refresh. A failure partway through leaves some of those writes done and the rest missing. That state is incomplete, but it is honest: no row is duplicated, and the gap is visible to anyone who looks. The damage comes later, from the rerun.

The nightly job. Step 2 appends to store_daily, step 3 builds region_rollup by reading store_daily back, and step 4 tells the dashboard to refresh.
  • raw store_sales (source, Parquet)
  • write store_daily (storage, mode append); idempotency: none
  • write region_rollup (storage, reads store_daily); on error: fail the job
  • refresh dashboard (consumer, BI)
  • raw store_sales -> write store_daily
  • write store_daily -> write region_rollup
  • write region_rollup -> refresh dashboard

Run by run

Momentstore_daily rows for 2024-03-14region_rollupState of the data
Before the job0emptyNothing written yet
Run 1 fails in step 31,200emptyIncomplete, every row correct
Run 2, a full rerun, succeeds2,4004 regions, revenue doubledCorrupted, and the dashboard shows it

Run 1 died in step 3, after step 2 had written 1,200 correct rows. Run 2 started again from step 1, added those rows a second time, and then built region_rollup by reading store_daily back, so the doubling spread into the rollup. The failure cost an hour of delay. The rerun produced wrong numbers that look plausible, which costs far more.

A single write can stop halfway too

A write is many tasks, so it can stop partway. Spark writes an empty file named _SUCCESS into the output directory only after the whole write completes, so a directory without it may hold partial output. What the half-done state looks like depends on the mode:

A failed append
  • The old data is intact
  • Depending on how the write is committed, files from tasks that finished may already sit in the destination
  • A full rerun adds a complete copy beside them
  • Duplicates stay until someone finds and removes them
A failed overwrite
  • The old data was removed before the new files were written
  • The destination is empty or partial until the next success
  • A full rerun replaces it whole
  • Readers during the gap see missing data, never doubled data

Neither failure, on its own, leaves damage that a successful run can't repair. The append's rerun is what turns a gap into lasting duplicates. That's the pattern to spot: a step that adds to its destination, inside a job that can fail after that step.

The backfill loop

Backfills put the same hazard in a loop. This script rebuilds a week of store summaries, a day at a time.

from datetime import date, timedelta

start = date(2024, 3, 8)
for offset in range(7):
    day = (start + timedelta(days=offset)).isoformat()
    sales = spark.read.parquet(f"/raw/store_sales/{day}/")
    summary = sales.groupBy("store_id").sum("revenue")
    summary.write.mode("append").parquet("/warehouse/store_daily/")

The loop dies on its 4th day, 2024-03-11. March 8 to 10 are complete in the table, and March 11 may have partial files. Rerunning the script from the top appends March 8, 9 and 10 a second time, and March 11 on top of its partial files. Restarting from March 11 avoids the first problem and keeps the second. Writing each day to its own directory with overwrite makes the loop safe to restart from any day.

Do
  • Write each logical date to its own directory, replaced whole on every run
  • Check for _SUCCESS before a downstream step reads a directory
  • Count rows and distinct IDs after a rerun to confirm nothing doubled
Avoid
  • Rerun an appending job without checking what the failed run already wrote
  • Build a rollup by reading back a table the same job appended to
  • Treat a quiet, successful rerun as proof the data is right

A nightly job reads raw events, appends a cleaned copy to /warehouse/events/, then writes a per-user count with overwrite. Tonight an executor was lost during the cleaning stage, and the job still finished. Last night the count step failed, and the on-call engineer reran the whole job. Which run put duplicates in /warehouse/events/?

The cell below rebuilds that situation on the practice data. Even-numbered completed orders stand in for the files a failed run left behind, and the full set stands in for the rerun's append. Read the 3 numbers it returns and decide which of them would have warned you.

The row count on its own looks like a plausible table. The warning is rows_in_table running ahead of distinct_orders, and doubled_orders names how many orders the rerun wrote twice. When every extra row is an exact copy of another, dropDuplicates(["order_id"]) keeps 1 row for each order_id and brings the table back to what a single run wrote.

After a rerun, a table that should hold 1 row per order_id has 1,500 rows and 1,200 distinct order IDs. Every extra row is an exact copy. What brings it back to a single run's output?

About this lesson

This section is part of the Fault Tolerance: Beginner 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.