Overwrite vs Append
Concepts covered: Caching & Persistence
The commit protocol makes a single run all-or-nothing. Pipelines run many times: the job for 2024-03-14 fails at 01:00 and is rerun at 03:00, a bug fix triggers a backfill of 30 days, a scheduler retries a run it thought had hung. Each rerun writes into a table that already holds data, and the save mode decides what happens to the rows that are there. Here is a daily job that counts views per page for 1 day of clickstream_raw.
from pyspark.sql import functions as F
run_day = "2024-03-14"
daily = (spark.table("clickstream_raw")
.filter(F.col("event_day") == run_day)
.groupBy("event_day", "page_id")
.agg(F.count("*").alias("views")))
daily.write.mode("append").partitionBy("event_day").parquet("/warehouse/page_counts")The first run adds its part files under event_day=2024-03-14. The rerun adds a second set: each write names its files with an id unique to that job, so the new files land beside the old ones without replacing any. The day now holds every row twice, and a dashboard summing views reports double.
Overwrite deletes more than the day
Switching to mode("overwrite") stops the duplication and introduces a worse failure. With partitionBy, overwrite follows spark.sql.sources.partitionOverwriteMode, whose default is static. In static mode Spark deletes every partition matching the write's partition spec before writing, and a DataFrame write carries no spec, so it matches all of them. The run for 2024-03-14 empties the table and leaves 1 day in it.
Dynamic mode replaces the days you wrote
spark.conf.set("spark.sql.sources.partitionOverwriteMode", "dynamic")
daily.write.mode("overwrite").partitionBy("event_day").parquet("/warehouse/page_counts")In dynamic mode, tasks write into a staging directory, .spark-staging- plus the job id, under the destination. At job commit the driver deletes each partition directory the job produced rows for and moves the new one into its place, while partitions the job never wrote stay as they were. Rerunning 2024-03-14 swaps that day's files for new files holding the same rows; the table after 5 runs is identical to the table after 1. A 30-day backfill becomes 1 job that filters the 30 days and writes them once, replacing each partition exactly once.
When the fixed day has no rows
Dynamic mode replaces only partitions that receive rows. Suppose 2024-03-14 was loaded from a corrupt file and the fixed rerun filters out every row. The job writes nothing for that day, so nothing is replaced, and the corrupt partition outlives a run that reported success. When a day must equal the job's output even if that output is empty, write to the partition's path:
(daily.drop("event_day")
.write.mode("overwrite")
.parquet(f"/warehouse/page_counts/event_day={run_day}"))Overwrite on that path clears the directory whatever the row count. Dropping event_day keeps the column out of the files, since a reader of the table takes it from the directory name.
| Write | A rerun of 2024-03-14 | Watch for |
|---|---|---|
| `append` | Adds the day's rows again | Duplicates on every retry |
| `overwrite`, static, with `partitionBy` | Deletes every day, writes 1 | It is the default setting |
| `overwrite`, dynamic, with `partitionBy` | Replaces 2024-03-14 only | A day with 0 rows is left untouched |
| `overwrite` to `event_day=2024-03-14` | Replaces that day, even with 0 rows | 1 partition per write |
A job runs with default settings and ends with out.write.mode("overwrite").partitionBy("ds").parquet(path). The table at path holds 90 days. The job is rerun for ds 2024-03-10 to fix a bug. What does the table hold afterwards?
- It keeps 90 days with 2024-03-10 replaced, because overwrite touches only the days in the output
- It holds 90 days with 2024-03-10 written twice, because overwrite adds the rerun beside the first copy
- It still holds 90 days, unchanged, because overwrite refuses to run against a partitioned path
- It holds 1 day, because the default static mode deletes every partition before writing
About this lesson
This section is part of the Fault Tolerance: 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.