Failures a Retry Cannot Fix
Concepts covered: Spark Execution Model
Machines fail during jobs: a cloud provider reclaims a node, a process runs out of memory, a read from storage times out. When a task fails, Spark launches the same task again on the same input partition, usually on a different executor, and the job carries on. Retries make a cluster far more dependable than any single machine in it.
The setting behind this is spark.task.maxFailures, which defaults to 4: any single task may fail 4 times before Spark stops retrying and aborts the whole job. A retry repeats the work exactly: same partition, same rows, same code. That fact sorts every failure into 2 groups.
What changes between attempts
- Can differ on the next attempt
- The executor and machine running the task
- The network path to storage and to other executors
- Memory used by other tasks on the same executor
- Identical on every attempt
- The input partition, row for row
- Your transformations and the UDFs they call
- The size of the partition and of each key in it
A failure caused by something on the left is transient: the next attempt lands somewhere healthier and succeeds. A failure caused by something on the right is deterministic: the next attempt meets the same rows, code and sizes, and fails the same way. Spark can't tell the 2 kinds apart, so it retries both, and a deterministic failure burns all 4 attempts before the job aborts. Each attempt redoes the work up to the crash, so that abort can take a long time to arrive.
| Failure | Root cause | On retry | What the error shows |
|---|---|---|---|
| Executor lost | Machine reclaimed or process killed | Rescued | `ExecutorLostFailure` |
| Storage read times out | Brief network trouble | Rescued | `SocketTimeoutException` |
| Shuffle data unreachable | The executor holding it died | Rescued once the data is rebuilt | `FetchFailedException` |
| UDF raises on a bad value | A malformed row in the input | Repeats on all 4 attempts | `PythonException` with the traceback |
| Out of memory on a single task | A hot key makes a partition huge | Usually repeats | `OutOfMemoryError`, or the executor is killed |
A deterministic failure, attempt by attempt
This daily cleanup job receives quantities as text and converts them with a Python UDF.
@F.udf("int")
def parse_qty(raw):
return int(raw)
sales = spark.read.parquet("/raw/store_sales/2024-03-14/")
clean = sales.withColumn("qty", parse_qty("qty_text"))
clean.write.parquet("/clean/store_sales/2024-03-14/")One input file holds a row whose qty_text is 'N/A'. The task that reads it calls int('N/A'), which raises ValueError. Spark reschedules the task on another executor. That attempt reads the same file, reaches the same row and raises the same error. After the 4th failure the driver aborts with a message like Task 37 in stage 2.0 failed 4 times, most recent failure: Lost task 37.3 in stage 2.0.
The number after the dot is the attempt, counted from 0, so 37.3 is the 4th attempt at task 37. The same task index failing with the same error on 4 different executors is the signature of a deterministic failure. More executors, more memory and more retries change nothing. The fix goes into the code:
@F.udf("int")
def parse_qty(raw):
if raw is None or not raw.strip().isdigit():
return None
return int(raw)Now 'N/A' becomes a null, the partition finishes on its first attempt, and the job completes. Count the nulls in qty afterwards so bad input stays visible.
The skewed partition that runs out of memory
An out-of-memory error looks like hardware trouble, which tempts people to trust the retry. If a single key holds 40% of the rows, the partition holding that key is the same size on every attempt and needs the same memory. The retry may land on an executor with slightly more free memory, but usually it dies at the same point, 4 times in a row. An OOM that keeps hitting the same task index is skew, and no retry can shrink a partition.
A nightly job fails. Which of these failures would Spark's automatic task retry most likely rescue?
- A UDF calls float() on the value '12,50' stored in one input file
- The cloud provider reclaims the machine running an executor mid-task
- A customer ID holds 40% of the rows feeding a join, and that task runs out of memory
- The input path has a typo, and the read raises an error for a missing path
Telling them apart in the Spark UI
- Stages tab: open the failed stage, sort its task table by Status, and read the Attempt and Errors columns.
- Same task index, same error, attempts 0 to 3 on different executors: deterministic. Fix the code or the data.
- Executors tab: dead executors around the failure time point to lost machines, a transient cause retries handle.
The driver log ends with Task 12 in stage 5.0 failed 4 times, most recent failure: Lost task 12.3 in stage 5.0. All 4 attempts show the same ArithmeticException, on 4 different executors. What should you do?
- Raise spark.task.maxFailures to 8 so the task gets more chances
- Add executors so the task lands on fresh hardware next time
- Rerun the job unchanged in the morning, when the cluster is quieter
- Find the input row behind the exception and handle it in the code
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.