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 holdsAfter it dies
The DAG, and which stages and tasks finishedGone: it lived only in driver memory
Which executor holds each shuffle blockGone, so no reducer can find its input
The executors' registrationsEach executor exits once it loses its driver
Cached partitions and broadcast variablesGone 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 settingsGone 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.

The control table is read once at start, and each day is recorded only after its partition has committed.
  • 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?

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.