Lineage as a Recipe

Concepts covered: Spark Execution Model

Retrying a task assumes its input still exists. A dead executor takes things with it: every partition it had cached, the task it was halfway through, and the shuffle files on its local disk. Spark keeps no second copy of any of these by default, so before it can retry, it has to recreate whatever the task needs.

Every DataFrame knows how it was made

A DataFrame is a description: the transformation that produces it and the parent DataFrame that transformation applies to. Following those parent links back ends at the files the job read. That chain is the DataFrame's lineage, and it works like a recipe: with the ingredients still in the pantry, the dish can be cooked again at any time.

orders = spark.read.parquet("/raw/orders/")
completed = orders.filter("status = 'Completed'")
gross = completed.selectExpr("region", "amount * 1.2 AS gross_amount")
by_region = gross.groupBy("region").sum("gross_amount")
The lineage of by_region. Each arrow runs from a parent to the step built on it. Rebuilding a lost partition walks the arrows backwards until it reaches data that still exists.
  • orders files (source, Parquet); parallelism: 3 partitions
  • filter (transform, status = 'Completed')
  • selectExpr (transform, amount * 1.2)
  • shuffle by region (custom, exchange)
  • sum per region (transform, groupBy)
  • by_region (consumer, result)
  • orders files -> filter
  • filter -> selectExpr
  • selectExpr -> shuffle by region
  • shuffle by region -> sum per region
  • sum per region -> by_region

Replicas versus recipes

Keep copies (replication)
  • Every block stored 2 or 3 times on different machines
  • Recovery reads a surviving copy, which is fast
  • Pays the storage cost on every run, failure or not
  • Works even if the original source is gone
Keep the recipe (lineage)
  • The steps are stored once, a few lines of plan
  • Recovery recomputes the lost partition, which costs CPU
  • Pays only when something actually fails
  • Needs the source files to still be readable

Spark keeps recipes for intermediate data because almost every partition survives its job, and copying every intermediate result 3 times would slow every job to guard against a rare loss. The source files sit on storage that keeps its own copies, so every recipe starts from ingredients that last.

A narrow chain rebuilds one partition

In that code, filter and selectExpr are narrow: partition 2 of gross comes only from partition 2 of completed, which comes only from split 2 of the files. If the executor holding partition 2 of gross dies, Spark reruns 1 task that reads split 2, filters it and computes gross_amount. No other partition is touched.

Behind a shuffle, the rebuild reaches wider

The groupBy adds a shuffle, so the job runs as 2 stages. In the first, each map task filters its split, adds up a partial sum for each region it saw, and writes those partial sums to local disk in buckets by region. In the second, each reduce task fetches its region's partial sums from every map task and adds them. A cut-down version of the job plays below: watch each region's partial sums cross between the stages.

completed = orders.filter("status = 'Completed'")
by_region = completed.groupBy("region").sum("amount")
by_region.collect()
orders
order_idregionstatusamount
1EUCompleted40
2USCompleted25
3APACCancelled30
4USCompleted60
5EUCompleted15
6USCancelled20
7APACCompleted50
8EUCompleted35
9USCompleted45
10APACCompleted10
11EUCancelled70
12USCompleted30

The reducer that produces EU's total of 90 fetched from every map task, because any of them could hold EU rows. That's what a wide dependency costs on recovery. If a reduce task dies while every map output is still on a healthy executor's disk, Spark reruns that reduce task alone, and it fetches the same buckets again. If the executor that died also held map output, the reducers trying to fetch it fail with FetchFailedException. Spark then reruns the lost map tasks from the files, using their lineage, and reruns the reducers after them.

What was lostWhat Spark reruns
A task in a narrow chainThat task, from its own input split
A reduce task, with every map output intactThat reduce task, fetching the same shuffle files
An executor holding output from 20 of 200 map tasksThose 20 map tasks, then the reduce tasks that could not fetch
A cached partitionThe steps from the nearest parent that still exists, and the result is cached again
The source filesNothing: the recipe has no ingredients left

The map stage of this job ran 200 tasks across 10 executors. During the reduce stage, a machine fails and takes 1 executor with it. That executor had run 20 of the map tasks and was running 3 reduce tasks. What does Spark rerun?

events = spark.read.parquet("/raw/events/")
clicks = events.filter("event_type = 'click'")
per_user = clicks.groupBy("user_id").count()
per_user.write.parquet("/out/clicks_per_user/")

A result built through 3 shuffles is persisted with MEMORY_AND_DISK. An executor holding 6 of its cached partitions is lost. What happens the next time an action reads the result?

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.