Fault tolerance in Spark means that a job finishes with the correct result even when executors, or whole machines, die halfway through. Spark gets there without copying every intermediate dataset to several machines. It keeps the recipe instead of the data, and rebuilds only what was lost.

When a Spark executor fails, the partitions it held are lost. Instead of replicating data (like HDFS), Spark uses lineage: the recorded sequence of transformations that produced each partition.

The driver detects the failure, identifies the lost partitions, and recomputes them by replaying the transformations from the nearest persisted ancestor. This approach is cheaper than replication for most workloads because transformations are typically narrow (map, filter) and operate on local data.

A reduceByKey job on three workers: one worker's executor or whole node fails, Spark traces the lineage back to the shuffle files on disk, and a new executor recomputes only the partitions that were lost.

Click a worker to fail it, and choose whether only its executor crashes or the whole node is lost. An executor crash takes the partitions cached in its memory, but its shuffle files stay on the node's disk and serve as the recovery point; a lost node takes those shuffle files too, and only the map tasks that wrote them are replayed from HDFS.

What lineage is

Every RDD knows its parents and the function that turns their partitions into its own. Following those links back from any RDD gives its lineage: a graph that ends at data Spark can read again, such as files in HDFS or S3. rdd.toDebugString prints it, indented at each shuffle.

Lineage works as a recovery mechanism because of two properties. RDDs are immutable, so the recipe for a partition describes the same data at any later time. And transformations are expected to be deterministic: run the same function over the same input partition and you get the same output. Under those two conditions, recomputing a lost partition is indistinguishable from never having lost it.

How much has to be recomputed depends on the kind of dependency. Behind a narrow dependency, a lost partition needs only the matching partition of its parent, so recovery is one task's worth of work. Behind a wide dependency, a lost partition needs a slice of output from every parent partition. If all of those had to be recomputed from scratch, one lost reducer would replay the whole upstream stage. That does not happen, because of the shuffle files.

Shuffle files as recovery points

At every stage boundary, each map task writes its output to the local disk of the machine it runs on, split by target partition. These shuffle files are what the next stage reads, and they stay on disk after the reducers have read them, for as long as the shuffle is still referenced. So the boundary between two stages is also a checkpoint the scheduler gets for free: to rebuild a lost reduce partition, Spark only needs to fetch its slices again from the surviving shuffle files.

The catch is that these files live on the executors' hosts, not in replicated storage. Who serves them decides what a failure costs:

  • Without an external shuffle service, the executor process itself serves its shuffle files. When the executor dies, the driver treats its map output as lost, even if the files are still on disk.
  • With the external shuffle service, a separate long-running process on each node serves the files. On YARN it runs inside the NodeManager as an auxiliary service. An executor can crash, or be released by dynamic allocation, and its shuffle output stays available. Only losing the node itself loses the files.

That is the difference between the panel's two buttons. The panel assumes the shuffle service is running.

A worked example: losing the third worker

The job in the panel reads 66 input splits from HDFS, maps and filters them, and runs reduceByKey into 33 partitions, one cached on each of 33 workers. Stage 0 is 66 map tasks, of which Worker 33 ran 33; stage 1 is 33 reduce tasks.

Fail Worker 33 with executor crash selected. Its executor's memory is gone, so the reduce partition cached there is lost: 11 partition. Its 33 shuffle files are still on the node's disk. Recovery starts a new executor on the same worker, reruns 00 map tasks and 11 reduce task, and the reducer fetches its slices from all 66 shuffle files as before.

Now fail the same worker with node lost. The cached partition goes, and so do the 33 shuffle files written on that disk. The reducer that must be rebuilt needs a slice from every map task, so those 33 map tasks are replayed from their HDFS splits on a new executor on Worker 11, and then the 11 lost reduce task runs. The other map outputs and the other reduce partitions are untouched. The recovered sums are identical to the ones before the failure, because every replayed function is deterministic.

In a real cluster the second case unfolds through the DAGScheduler. The rerun reduce task tries to fetch a block from the dead node and fails with a FetchFailed error. The DAGScheduler marks that node's map output as missing, resubmits the parent ShuffleMapStage with only the missing partitions, and then retries the reduce stage. The DAG scheduler article shows how those stages are built.

Kinds of failure and who handles them

  • A task fails with an exception or an out-of-memory error. The TaskScheduler retries it. The job fails once any one task has failed spark.task.maxFailures times, 4 by default.
  • An executor dies. The driver learns of it either from the cluster manager, when YARN reports the container's exit, or from missing heartbeats: executors heartbeat every 10 seconds (spark.executor.heartbeatInterval) and are declared lost after spark.network.timeout, 120 seconds by default. Its running tasks are rescheduled, its cached blocks are forgotten, and, without a shuffle service, its map output is unregistered.
  • A shuffle fetch fails. The map stage reruns for the missing partitions, as above. A stage that fails spark.stage.maxConsecutiveAttempts times in a row, 4 by default, aborts the job.
  • The driver dies. Lineage lives in the driver's memory, so it cannot recover the driver. In YARN cluster mode the ResourceManager can restart the ApplicationMaster with the driver inside it, up to spark.yarn.maxAppAttempts, but the application starts over from the beginning. Long-running streaming queries survive driver restarts through their checkpoint location, which records progress and state on reliable storage.

Checkpointing: cutting the lineage

Lineage has a cost: recovery time grows with the length of the chain. An iterative algorithm that derives each RDD from the previous one for hundreds of iterations builds a lineage hundreds of steps deep. Losing a late partition means replaying all of it, and very deep lineages can even overflow the stack when tasks are serialized.

rdd.checkpoint() saves the RDD to the directory set with sc.setCheckpointDir, normally on HDFS or S3, and replaces its lineage with a pointer to the saved files. Two details matter. The write happens in a separate job after the first action that computes the RDD, so without persist() beforehand the RDD is computed twice. And a checkpoint is permanent storage you pay for, so it belongs at the points where recomputation is expensive, typically every few iterations of a loop. localCheckpoint() truncates the lineage too but stores the data on the executors, which is faster and gives up recovery from executor loss.

Persisting with a replicated storage level, such as MEMORY_AND_DISK_2, is the middle ground: the lineage stays, but a lost block usually has a second copy on another executor.

Speculative execution

Some failures are slow rather than dead: a degraded disk, a noisy neighbour, a node swapping. With spark.speculation enabled, Spark watches each stage, and once a fraction of its tasks have finished, it launches a copy of any task running much longer than the median; the first copy to finish wins and the other is killed. In the current documentation the defaults are a quantile of 0.9 and a multiplier of 3, checked every 100 ms. Speculation is off by default, because two copies of a task that writes output non-idempotently can produce duplicate or conflicting results; Spark's own file committers are designed for it, custom sinks often are not.

When recomputation is not safe

Lineage assumes that recomputing a partition gives the same result. Some transformations break that: random sampling without a fixed seed, zipWithIndex or round-robin repartition over input whose order is not stable, or a map that reads the current time or an external service. If such a partition is recomputed after its consumers have already read the earlier version, different tasks of the next stage see different data.

Spark tracks whether a stage's output is deterministic. When an indeterminate map stage has to be rerun, it reruns the whole downstream stage rather than mixing old and new output, and fails the job if a result has already been committed. Writing deterministic transformations, or checkpointing right after a non-deterministic one, avoids the question entirely.

Common pitfalls

  • No shuffle service with dynamic allocation. Executors released for being idle take their shuffle output with them. Run the external shuffle service, or rely on shuffle tracking, which keeps such executors alive instead.
  • Spot or preemptible nodes holding shuffle data. Losing a node loses its shuffle files and triggers map-stage reruns, sometimes in a cascade. Keep shuffle-heavy executors on stable capacity, or enable decommissioning so blocks are migrated before a node goes away.
  • Checkpointing without persisting. The checkpoint job recomputes the RDD from scratch.
  • Retries that hide a real bug. A task that fails four times with the same exception is not a transient failure; read the first attempt's stack trace, not the last.
  • Treating cache as durability. A cached RDD is a performance hint. Losing it is normal and silently costs recomputation.

Short answers

How does Spark achieve fault tolerance?

Through lineage. Every RDD records which parent RDDs and which deterministic function produced each of its partitions, so a partition that is lost with an executor can be recomputed from the nearest data that still exists: cached blocks, shuffle files on disk, or the original input. Spark does not replicate intermediate data by default.

What is RDD lineage in Spark?

The graph of transformations that leads from the input data to an RDD: for each RDD, its parents and the function that derives its partitions from theirs. rdd.toDebugString prints it. The scheduler uses the same graph to plan stages and to rebuild lost partitions.

What happens when a Spark executor fails?

The driver notices the lost executor, marks its running tasks as failed and reschedules them elsewhere. Partitions cached in its memory are recomputed from lineage when next needed. Its shuffle files survive if an external shuffle service serves them; otherwise the map tasks that wrote them are rerun when a reducer fails to fetch them.

What is the difference between cache and checkpoint in Spark?

cache or persist keeps partitions in executor memory or local disk and keeps the lineage, so a lost block is recomputed. checkpoint writes the RDD to reliable storage such as HDFS or S3 and truncates the lineage, so recovery reads the saved copy instead of replaying a long chain of transformations.

What is a FetchFailedException in Spark?

The error a reduce task reports when it cannot read a shuffle block from the executor or node that wrote it. Spark treats it as lost map output, not as a bug in the task: the DAGScheduler reruns the map tasks whose output is missing and then retries the reduce stage, aborting the job after 4 consecutive failed attempts of a stage by default.

What is speculative execution in Spark?

With spark.speculation enabled, Spark launches a second copy of a task that runs much slower than the median of its stage, takes whichever copy finishes first and kills the other. It addresses slow nodes rather than failures, is off by default, and is safe only when the task's output is committed idempotently.

Further reading

  • M. Zaharia et al., "Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing", NSDI 2012. Lineage-based recovery, the case against replicating intermediate data, and checkpointing of long lineages.
  • Apache Spark documentation, RDD Programming Guide, sections on RDD persistence and checkpointing.
  • Apache Spark documentation, Configuration, sections on scheduling and dynamic allocation: task and stage retries, speculation, and the external shuffle service.
  • M. Zaharia, A. Konwinski, A. D. Joseph, R. Katz, I. Stoica, "Improving MapReduce Performance in Heterogeneous Environments", OSDI 2008. Speculative execution of stragglers and why naive speculation goes wrong.
  • J. Dean, S. Ghemawat, "MapReduce: Simplified Data Processing on Large Clusters", OSDI 2004. Re-execution of lost map output and backup tasks, the ideas Spark's recovery builds on.
  • On this site: RDD transformations and the shuffle, the DAG scheduler and dynamic executor allocation on YARN.