Why Replays Are Safe

Concepts covered: Spark Execution Model

Lineage means a task can run 2, 3 or 4 times inside a single job. Running work twice is the classic way data gets doubled, like a payment sent twice. Spark's own computation never doubles, and the reason is 3 design choices working together.

The input is identical on every attempt

Calling orders.filter("status = 'Completed'") reads the orders files and leaves them as they were. A transformation produces new rows from its input and writes nothing back into it, so a rerun task finds the same split, holding the same rows, that the failed attempt started from.

Same rows in, same rows out

A filter, a selectExpr and a groupBy with a sum each compute their output from their input and nothing else. Give attempt 1 the partition attempt 0 had, and it produces the rows attempt 0 would have produced. A replay recomputes a value from scratch, with nothing earlier to add onto.

This holds while the computation depends on its input alone. Functions that read something else, such as rand() without a seed or the current time, can give a replay different values, and a later lesson covers them.

One accepted result per partition

The driver tracks each task by its partition index. A task's result travels to the driver as a single package when the task finishes, never row by row, so a task that dies midway has sent nothing. When an attempt succeeds, the driver marks that partition done and ignores any other attempt's result for it. Suppose the task reading orders 5 to 8 runs on executor 3, which is lost partway through, and the retry runs on executor 5:

The executor's memory, and the order 5 it held, disappeared together. The driver receives orders 5, 7 and 8 exactly once, from attempt 1.

The animation below runs the same filter over 12 orders and collects the 9 completed ones. No shuffle is involved: each task filters its own partition and sends its rows to the driver as 1 result, and the driver assembles them in partition order.

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

A rerun of any of those tasks would leave the board unchanged: 1 result per partition, 9 rows in total. Shuffle output follows the same rule. A rerun map task writes its buckets again, and the driver records a single output location per map task, so reducers read each map task's rows once.

A count() runs over 50 partitions. The task for partition 9 fails twice with executor-lost errors and succeeds on its 3rd attempt. How many times are partition 9's rows included in the count?

Written files follow the same rule

When a stage writes files, each task attempt writes into its own temporary directory under the destination. Only the attempt Spark accepts has its files moved into the final location, and the temporary directory is cleaned up when the job finishes, so a task that died after writing half a file leaves that half where no reader looks.

Try a replay yourself. The cell below runs the same recipe over the practice order_items table twice, as 2 separate computations, and summarises each run in a row.

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.