The DAGScheduler is the part of the Spark driver that turns an action into a physical plan. It takes the graph of RDDs that the action depends on, cuts it into stages at every shuffle, and hands each stage to the TaskScheduler as a set of tasks, one per partition, once the stages it depends on have finished. It is also the component that notices lost shuffle output and reruns the stage that produced it.

When you submit a Spark job, the DAG scheduler translates the logical plan (chain of RDD transformations) into a physical execution plan:

  1. Build the DAG from the sequence of transformations
  2. Identify stage boundaries at shuffle points (wide dependencies like groupByKey, reduceByKey)
  3. Topologically sort stages to determine execution order
  4. Submit tasks for each stage's partitions to the task scheduler

Stages within the same pipeline (connected by narrow dependencies) can fuse their transformations, avoiding intermediate materialization.

A DAG of ten RDDs and an action, with two joins: the scheduler walks back from the action, cuts the graph into stages at its four wide (shuffle) dependencies, sorts the stages topologically and runs them in waves, and clicking an arrow switches it between narrow and wide.

Step through the plan, or click an arrow to turn a narrow dependency into a shuffle (or a join side into a co-partitioned one) and watch the stages re-split and the waves change. Clicking an RDD names its stage and the dependency that put the boundary where it is.

Jobs, stages and tasks

Spark has three units of work, and the Spark UI is organized around them.

  • A job is created by each action: collect, count, save, foreach, or a DataFrame write. Transformations create no jobs. A few operations run hidden jobs of their own, for example sortByKey, which samples its input to choose the range boundaries before the real sort.
  • A stage is a piece of the job that can run without a shuffle in the middle. All of its tasks run the same code on different partitions.
  • A task processes one partition within one stage and runs on one executor core. A stage over 200 partitions is 200 tasks.

Stages come in two kinds. A ShuffleMapStage ends at a shuffle: each of its tasks (a ShuffleMapTask) runs the fused chain of transformations over its partition and writes the output split by target partition to local disk, registering where it lives with the driver's map output tracker. A ResultStage is the last stage of a job: each ResultTask computes the action on its partition and returns the result to the driver, or writes it to storage. A job has exactly one ResultStage and any number of ShuffleMapStages.

How the scheduler cuts stages

The DAGScheduler starts from the RDD the action was called on and walks the lineage backwards. Along a narrow dependency, the parent joins the current stage: its partition can be computed inside the same task, so map, filter and mapValues collapse into one pass over each record. Along a wide (shuffle) dependency the walk stops. The parent side of that dependency becomes the end of a new ShuffleMapStage, and the walk continues from there.

Each shuffle dependency gets exactly one ShuffleMapStage, identified by its shuffle id. That is why, in a single job, the number of stages is the number of shuffles it depends on plus one for the ResultStage, and why a second job that reuses the same shuffle finds its stage already done.

Once the stages exist, scheduling is a topological order over them. submitStage checks whether a stage's parents have their output available. If they do, it submits the stage's missing tasks; if not, it submits the parents first and parks the stage in a waiting set. When a ShuffleMapStage finishes, every waiting stage whose parents are now complete becomes runnable. Stages that do not depend on each other run at the same time if the cluster has free cores, which the panel draws as waves.

A worked example: the DAG in the panel

The panel's job has 1010 RDDs and an action. Two source RDDs each go through a chain of narrow transformations and meet in a join; the result is mapped and joined with a third source; collect() ends the job. The arrows into the two joins are wide, which makes 44 shuffle dependencies.

Walking back from the action, the scheduler first gathers the ResultStage: the second join and the action, linked by a narrow dependency. Both inputs of that join are wide, so each roots a parent stage. One of them is the third source alone. The other is the map after the first join, which pulls the first join into its stage through a narrow arrow; the first join's own two inputs are wide again, so the two source chains become stages of their own.

The result is 55 stages, numbered in topological order, with the ResultStage as stage 44. They run in 33 waves: the 33 stages that read only source data can all start immediately, the first join's stage waits for the two source chains, and the ResultStage waits for that and for the third source.

Now click the arrow from the first source chain into the first join to make it narrow, as it would be if that side were already hash-partitioned with the same partitioner and partition count as the join. The source chain becomes part of the join's stage, the plan drops to 44 stages, and the other side of the join still needs its shuffle. Making a narrow arrow wide does the opposite: it adds a stage boundary, and often a wave, at that point.

What causes a shuffle

A shuffle happens whenever an output partition depends on many input partitions. In the RDD API that is:

  • Aggregations by key: groupByKey, reduceByKey, aggregateByKey, combineByKey (and the action countByKey, which is built on reduceByKey).
  • Joins and co-groups whose inputs are not partitioned the same way. If both sides share a partitioner with the same number of partitions, the join is narrow.
  • distinct, which is an aggregation by the whole record.
  • repartition, and coalesce(n, shuffle = true).
  • sortByKey, which range-partitions the data.

union is narrow: it concatenates the partition lists of its inputs. coalesce without a shuffle is narrow too; it merges neighbouring partitions in place.

In DataFrame and SQL plans the shuffle is an Exchange node in the physical plan (explain() shows it), introduced for groupBy, joins, distinct, window functions with partitionBy, and repartition. A broadcast hash join replaces the shuffle of both sides with a copy of the small side sent to every executor; Spark chooses it automatically when a table's estimated size is under spark.sql.autoBroadcastJoinThreshold, 10 MB by default.

From stage to running task

When a stage is ready, the DAGScheduler creates one task for each partition whose output is missing and passes them as a TaskSet to the TaskScheduler. The TaskScheduler wraps each TaskSet in a manager that tracks attempts, and assigns tasks whenever the cluster backend offers free executor cores.

Placement follows locality: a task prefers the executor that already caches its partition (PROCESS_LOCAL), then the same host (NODE_LOCAL), then the same rack, then anywhere. With delay scheduling, the scheduler waits up to spark.locality.wait, 3 seconds by default, for a better slot before it accepts a worse one. Between concurrent jobs in one application the default order is FIFO; spark.scheduler.mode=FAIR shares executors between pools instead.

Failures and stage retries

The two schedulers split failure handling. A task that throws, or whose executor dies, is retried by the TaskScheduler; a job fails when one task has failed spark.task.maxFailures times, 4 by default.

A fetch failure is different. When a reduce task cannot fetch a shuffle block, because the executor or node that wrote it is gone, retrying the reduce task would fail again. The task reports FetchFailed to the DAGScheduler, which marks the map output from that location as lost, resubmits the parent ShuffleMapStage to recompute only the missing map partitions, and then resubmits the failed stage. A stage that fails this way spark.stage.maxConsecutiveAttempts times in a row, 4 by default, aborts the job. The fault tolerance article walks through exactly this case.

Adaptive query execution

For DataFrame and SQL jobs, adaptive query execution (on by default since Spark 3.2) uses the stage boundary as a re-planning point. When a shuffle map stage finishes, Spark knows the real size of every shuffle partition, and it can coalesce many small partitions into fewer tasks, switch a sort-merge join to a broadcast join when one side turned out small, and split skewed partitions of a join into several tasks. The stage structure the DAGScheduler sees can therefore change while the query runs; in the UI this shows up as more jobs than the query has actions.

Common pitfalls

  • More stages than expected. Some step introduced a shuffle you did not plan for: a distinct, a join on a key the inputs were not partitioned by, a repartition left over from debugging. Read the DAG visualization of the job in the Spark UI, or explain() for DataFrames.
  • A stage with one slow task. The stage cannot finish until its last task does, and the next stage cannot start. That is usually skew in the shuffle keys; AQE's skew join handling or salting the key helps.
  • Recomputing the same lineage in several actions. Each action is a separate job. Shuffle output is reused automatically and shows up as skipped stages, but anything before the first shuffle is recomputed unless you cache() it.
  • Too few shuffle partitions. spark.sql.shuffle.partitions defaults to 200 whatever the data size; for large shuffles that means huge tasks that spill to disk. AQE coalescing fixes the opposite case, too many small partitions, but not this one.
  • Reading stage numbers as order. Stage ids are assigned across the whole application, and skipped or retried stages leave gaps. The order that matters is the dependency order in the DAG.

Short answers

What is a stage in Spark?

A stage is a set of tasks that run the same chain of narrow transformations on different partitions, with no shuffle inside it. Spark ends a stage wherever the next step needs data from many partitions, so every stage boundary is a shuffle, and a stage can start only when the stages that feed it have finished.

What causes a shuffle in Spark?

Any operation where an output partition needs records from many input partitions: groupByKey, reduceByKey, aggregateByKey, distinct, repartition, sortByKey, and join or cogroup when the two sides are not already partitioned the same way. In DataFrame plans the shuffle appears as an Exchange node; a broadcast join avoids it for a small table.

What is the difference between a job, a stage and a task in Spark?

A job is the work triggered by one action, such as collect or a write. The DAGScheduler splits the job into stages at shuffle boundaries. Each stage runs as one task per partition, and a task is the unit an executor core actually runs.

What is the difference between the DAGScheduler and the TaskScheduler?

The DAGScheduler works on stages: it builds them from the RDD lineage, decides which are ready, and resubmits a stage when shuffle output is lost. The TaskScheduler works on tasks: it receives a ready stage as a TaskSet, places each task on an executor with locality in mind, and retries individual tasks that fail.

What is the difference between a ShuffleMapStage and a ResultStage?

A ShuffleMapStage ends in a shuffle: its tasks write their output split by target partition for a later stage to fetch. A ResultStage is the last stage of a job: its tasks compute the action itself and send results to the driver or write them out. Every job has exactly one ResultStage.

Why does the Spark UI show skipped stages?

A stage is skipped when the shuffle output it would produce already exists, usually because an earlier job in the same application computed it. Spark keeps shuffle files for as long as the shuffle is referenced, so a second action over the same lineage reuses them instead of recomputing.

Further reading

  • M. Zaharia et al., "Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing", NSDI 2012. Section 5.1 describes the job scheduler: stages cut at wide dependencies, pipelined narrow transformations, and resubmission of lost map output.
  • Apache Spark documentation, Job Scheduling and Monitoring and Instrumentation. Scheduling within an application, fair pools, and how jobs, stages and tasks appear in the web UI.
  • Apache Spark documentation, Performance Tuning for Spark SQL. Adaptive query execution, broadcast joins and shuffle partitions.
  • B. Chambers, M. Zaharia, Spark: The Definitive Guide, O'Reilly, 2018. Chapter 15 follows a job through stages and tasks on a cluster.
  • On this site: RDD transformations and the shuffle, fault tolerance via lineage and dynamic executor allocation on YARN.