Idempotent Re-runs

Concepts covered: Configuration Tuning

Everything so far happened inside a single job, and Spark's protection ends where the job ends. When an orchestrator retries a failed job, or you rerun last night's job after fixing a bug, Spark starts a fresh job with no memory of the earlier one. Whatever the earlier run wrote is still at the destination, and the new run's write lands on top of it.

The property to check is idempotence: an operation is idempotent when running it twice leaves the same result as running it once. Setting a light switch to off is idempotent; flipping it is not. Whether a Spark job is idempotent depends almost entirely on its final write.

What each write mode does to existing output

ModeIf the destination already holds dataRunning the same job twice
`errorifexists` (the default)The write fails with an errorThe second run fails loudly, and the first run's output is untouched
`append`New files are added beside the old onesEvery row appears twice
`overwrite`Old files are removed, new ones writtenSame output as a single run
`ignore`Nothing is written, and no error is raisedThe first run's output stays, even if the new input changed

The append that doubles revenue

This job writes a summary row per store for a day of sales.

daily = spark.read.parquet("/raw/store_sales/2024-03-14/")
summary = daily.groupBy("store_id").sum("revenue")
summary.write.mode("append").parquet("/warehouse/store_daily/")

The first run writes 1,200 rows, a row per store. Suppose a later step fails and the orchestrator retries the job. The retry computes those 1,200 rows correctly and appends them beside the first copy, so the table now holds every store twice. Nothing raises an error, and a dashboard that sums revenue per store reports exactly double.

Make the destination belong to the run

run_date = "2024-03-14"
daily = spark.read.parquet(f"/raw/store_sales/{run_date}/")
summary = daily.groupBy("store_id").sum("revenue")
summary.write.mode("overwrite").parquet(f"/warehouse/store_daily/{run_date}/")

Run this once or 5 times and /warehouse/store_daily/2024-03-14/ holds the same output, because the date fixes the destination and overwrite replaces it whole.

Do
  • Pass the logical date in as a parameter and build paths from it
  • Overwrite a destination that holds exactly one run's output
  • Keep errorifexists where a second run should never happen
Avoid
  • Append a whole job's output to a shared path with no guard
  • Put the current time into an output path
  • Use ignore when a rerun is meant to apply a fix

The practice data shows the append damage. The cell below treats the completed orders as 1 run's output, unions them with themselves to stand in for a table after 2 appends, and summarises both.

The extra rows repeat existing order_id values, which is the tell. In a table where each ID should appear once, comparing the row count with the distinct ID count is the cheapest check for an append that ran twice.

A reporting job ends with this write, and the dashboard reads every directory under /warehouse/store_daily/. Is the job safe to run twice?

from datetime import datetime

stamp = datetime.now().strftime("%Y-%m-%d-%H%M")
summary.write.mode("overwrite").parquet(f"/warehouse/store_daily/{stamp}/")

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.