When the Driver Dies
Concepts covered: Caching & Persistence
When an executor dies, Spark reruns its lost tasks from lineage and the job carries on. When the driver dies, the application ends. The driver is the single process that knows what the job is, and it keeps that knowledge in its own memory, where no successor can read it.
| The driver holds | After it dies |
|---|---|
| The DAG, and which stages and tasks finished | Gone: it lived only in driver memory |
| Which executor holds each shuffle block | Gone, so no reducer can find its input |
| The executors' registrations | Each executor exits once it loses its driver |
| Cached partitions and broadcast variables | Gone with the executors that held them |
| A running write's commit state | `_temporary` stays on disk and no `_SUCCESS` is written |
| Temp views and session settings | Gone with the session |
A restarted driver is a rerun
Spark can relaunch a driver in cluster mode: on YARN the application gets up to spark.yarn.maxAppAttempts attempts, and the standalone manager relaunches drivers submitted with --supervise. Either way the new driver runs your program from its first line and builds a fresh DAG. The last attempt's shuffle output is gone, and so is any record of what that attempt finished. That's why the overwrite and upsert patterns above matter beyond manual reruns: they make an automatic relaunch safe.
Resume by skipping finished chunks
The new driver remembers nothing, so whatever the job needs to remember has to live outside Spark: in durable outputs, plus a record of which ones are complete. Split a long job into chunks that each end in a committed write, such as 1 day or 1 month of a backfill, or 1 region. On start, the driver reads which chunks are done and runs only the rest.
days = ["2024-03-%02d" % d for d in range(1, 13)]
done = {r.load_date for r in spark.table("etl_control")
.filter("status = 'done'")
.select("load_date")
.collect()}
for day in days:
if day in done:
continue
build_day(day).write.mode("overwrite").parquet(f"/warehouse/payouts/ds={day}")
record_done(day)The guarantee depends on the order of the last 2 lines. record_done runs only after write returns, and write returns only after job commit and _SUCCESS. If the driver dies between them, the day is written but unrecorded, and the next attempt rebuilds it with an overwrite that changes nothing. Swap the 2 lines and a crash can record a day that was never written, which every later attempt skips.
Take a backfill of 12 days that runs for 4 hours and whose driver runs out of memory partway through the last day.
- 3h 40m+
- 20 min
- etl_control (storage, finished days)
- Driver loop (transform, spark-submit, cluster mode); retries: 1
- Build 1 day (transform, Spark job); idempotency: overwrite ds={day}
- payouts ds={day} (storage, Parquet + _SUCCESS)
- Record done (quality gate, append to etl_control)
- etl_control -> Driver loop
- Driver loop -> Build 1 day
- Build 1 day -> payouts ds={day}
- payouts ds={day} -> Record done
Streaming resumes the same way
A Structured Streaming query survives a driver restart because its checkpoint directory has this pattern built in. Before a micro-batch runs, the driver writes the batch's offsets to the offset log, and after the sink finishes it adds an entry to the commit log. A new driver started with the same checkpointLocation finds the last batch with offsets and no commit, runs that batch again, and continues. A batch job gets the same recovery only when you write the control record and the idempotent chunk write yourself.
A job in cluster mode on YARN, with default settings, appends a result after each of its 6 hourly chunks. Its driver runs out of memory during chunk 5. What happens next?
- A second driver reruns the program from the start, so chunks 1 to 4 are appended a second time
- A second driver resumes at chunk 5, because the scheduler recorded which chunks had finished
- The executors keep working on chunk 5 and report to the new driver once it registers
- The application fails with no retry, so the table holds chunks 1 to 4 exactly once
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.