Jobs, stages & tasks
Apache Spark · Spark Architecture
Theory
Spark turns your code into three levels of work. A job is created each time you call an action such as count(), collect(), show() or write. The driver splits each job into stages at shuffle boundaries: all the narrow steps that can run without moving data between machines (read, filter, map, select) are pipelined into one stage, and a wide step like groupBy, join or reduceByKey starts a new stage. Each stage runs as a set of tasks, one task per partition, and tasks are what executors actually run.
For example, reading orders in 3 partitions, mapping to (region, amount) and then reduceByKey into 2 partitions gives 1 job with 2 stages: stage 1 has 3 tasks that read, map and write shuffle files, and stage 2 has 2 tasks that fetch the shuffled data and add up the amounts. The Spark UI shows exactly this hierarchy, which is where you go to find the slow stage or the one straggling task.
Why we use it
Performance tuning starts by reading jobs, stages and tasks in the Spark UI. Too few tasks leaves cores idle, one huge task signals data skew, and every extra stage means a shuffle. Interviewers often ask how Spark decides the number of stages and tasks.
When to use it
Use this model whenever you read the Spark UI, estimate parallelism (tasks per stage vs available cores) or try to cut shuffles. Note that some DataFrame operations, and adaptive query execution, can trigger extra small jobs, so counts in the UI may not match one-action-one-job exactly.
How it works
- Call an action (collect, count, write)
- Driver creates a job from the plan
- DAG scheduler splits the job at shuffle boundaries into stages
- Each stage becomes one task per partition
- Task scheduler sends tasks to executor cores
- Does another stage depend on this one? Yes: Write shuffle files, then start the next stage. No: Return or write the final result.
- Job complete
Syntax
action -> 1 job (sometimes more)
shuffle boundary -> new stage
partition -> 1 task per stage
sc.setJobGroup("id", "desc"); sc.statusTracker().getJobIdsForGroup("id")Example
from pyspark.sql import SparkSession, functions as F
spark = (SparkSession.builder.master("local[1]")
.config("spark.ui.enabled", "false")
.getOrCreate())
sc = spark.sparkContext
sales = [("North", 55000.0), ("West", 30000.0), ("North", 1500.0),
("South", 52000.0), ("West", 25000.0), ("East", 15000.0)]
sc.setJobGroup("demo", "sales per region")
pairs = sc.parallelize(sales, 3) # 3 partitions -> 3 tasks
totals = pairs.reduceByKey(lambda a, b: a + b, numPartitions=2) # shuffle -> new stage
print(sorted(totals.collect())) # action -> job
tracker = sc.statusTracker()
for job_id in tracker.getJobIdsForGroup("demo"):
stage_ids = sorted(tracker.getJobInfo(job_id).stageIds)
print("job", job_id, "has", len(stage_ids), "stages")
for sid in stage_ids:
print(" stage", sid, "->", tracker.getStageInfo(sid).numTasks, "tasks")
# Output:
# [('East', 15000.0), ('North', 56500.0), ('South', 52000.0), ('West', 55000.0)]
# job 0 has 2 stages
# stage 0 -> 3 tasks
# stage 1 -> 2 tasks
Key points
- Action -> job; shuffle boundary -> new stage; partition -> task
- Narrow steps inside a stage are pipelined together without writing data out
- Number of tasks in a stage = number of partitions it processes
- Shuffle writes happen at the end of a stage, shuffle reads at the start of the next
- One very slow task in a stage usually means data skew