Skip to the page

Spark · core

How a Spark job runs: jobs, stages and tasks

The driver plans the work, executors run tasks, and a shuffle splits a job into stages. Transformations are lazy and only actions start a job.

The notes

What you must know

  • The driver runs your program, builds the logical plan, lets the Catalyst optimizer make a physical plan, splits it into stages, schedules tasks and tracks them. It does not process the bulk of the data.
  • The cluster manager (YARN, Kubernetes or standalone) starts executors: JVM processes with cores (task slots) and memory. They run tasks, cache data and write shuffle files.
  • Each action starts a job. A job splits into stages at shuffle boundaries (wide transformations). A stage runs one task per partition.
  • Narrow transformations (filter, select) move no data between partitions. Wide ones (groupBy, join, sort) shuffle.
  • Task counts: a file scan uses input splits of about 128 MB (spark.sql.files.maxPartitionBytes). After a shuffle, spark.sql.shuffle.partitions (200 by default), which adaptive query execution can merge into fewer partitions.
  • Laziness lets Catalyst optimise the whole plan: filter pushdown into Parquet, column pruning and whole-stage code generation.
  • Without cache, every action reruns the whole lineage from the scan. orderBy followed by limit becomes a top-K, not a full sort. Count after writing, from the written table.
  • Client mode runs the driver on the submitting machine. Cluster mode runs it inside the cluster, which production jobs use. If the driver dies, the job fails; a streaming query resumes from its checkpoint.
  • Spark Connect (Spark 4.x) uses a thin client that sends plans over gRPC to a server where the driver runs, so a crash in your Python app does not kill the driver.

The red pen

Common traps: mistake → fix

  • Mistake: The driver processes the data

    Fix: executors do; the driver plans and coordinates.

  • Mistake: Every line of PySpark runs a job

    Fix: only actions do; transformations build a plan.

  • Mistake: show() caches the result

    Fix: it does not; each action recomputes without cache.

Easy to hard

Questions interviewers ask, easy to hard

  1. Walk me through what happens when you run a PySpark job.
  2. What is the difference between a job, a stage and a task?
  3. Which lines in this code start a job?
  4. Why does orderBy followed by limit not sort the whole dataset?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview