Jobs, stages, and tasks
How Spark breaks one line of code into the units of work that actually run on a cluster.
Job
A is created every time your code triggers an , something that forces Spark to actually produce a result (write, count, collect) rather than just describe a transformation.
Transformations alone (select, filter, join) don't run anything; they build up a plan. The action is what fires that plan.
Stage
A job is split into at every boundary, a point where data must be redistributed across the cluster (for a join or groupBy, for example).
Everything inside one stage can run without talking to other partitions; everything that requires repartitioning data starts a new stage.
Task
A stage is split into , one per partition. A task is the actual unit of work an executor runs: it applies the stage's operations to one partition's worth of data.
If a stage has 200 partitions, it has 200 tasks, and those tasks run in parallel across however many executor cores are available.
Visual example: total sales per customer
Filter positive sales with sales.filter("amount > 0"), then chain .groupBy("customer"), .sum("amount"), and .collect(). The filter and aggregation build a plan; collect() requests the result. Assume three input partitions, three shuffle partitions, and a simple execution without adaptive changes, extra planning jobs, or retries.
Job 0 contains two stages. Stage 0 runs three tasks, one for each input partition, to read, filter, and produce partial customer totals. The shuffle redistributes those totals by customer, so all partial totals for a customer reach the same downstream partition.
Stage 1 runs three more tasks, one for each shuffle partition, to fetch the shuffled data and finish the totals. Six tasks are scheduled across the two stages; the downstream stage waits for the required shuffle output, and available executor cores limit how many tasks run at once.
The task count can change between stages: three input partitions and two shuffle partitions would produce three tasks followed by two tasks, for five in total. Retries can add task attempts; Spark UI counts can differ when adaptive execution or extra jobs are involved.
Practice / self-test
1. What triggers a new job?
Reveal answer
An action: an operation that forces Spark to produce a concrete result, such as write, count, or collect. Transformations alone only build a plan.
2. What causes a job to split into multiple stages?
Reveal answer
A shuffle boundary, a point where data needs to be redistributed across the cluster, such as for a join or a groupBy.
3. If a stage has 400 partitions, how many tasks does it have?
Reveal answer
400. One task per partition in that stage.
4. A two-stage job has three input partitions and two shuffle partitions. Without retries, how many tasks run?
Reveal answer
Five: three tasks in the first stage and two in the second. Count the partitions separately for each stage; the stages do not have to have the same task count.