Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 65/ 160
04. Big Data Processing & Apache Spark

Jobs, Stages, Tasks & Narrow vs Wide Dependencies

Read how Spark splits work into jobs, stages, and tasks, and why wide deps force shuffles.

IntermediateStages · Tasks · Wide Deps3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Submit a complete execution job. Next, wide exchanges create stage boundaries. Finally, failed tasks rerun on available executors.

Think of it like…

A job is a construction project, stages are trade handoffs, and tasks are individual rooms; a shuffle is moving all materials between teams so the right room receives them.

Where you’ve seen it

A map and filter remain in one stage; join/groupBy creates a shuffle and at least new reduction stages, with one task processing each of spark.sql.shuffle.partitions output partitions.

Words you’ll hear

Stages
A sequence of stages forming one Spark action's execution graph.
Tasks
A single scheduling unit per partition inside a stage, running the same closure on one data slice.
Wide Deps
Transformations whose output may combine records from many partitions, such as joins and aggregations, requiring a shuffle.
Operational boundary
Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized. Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

Visual diagram

Loading interactive diagram

Theory & deep dive

Stages: the working mechanism

Each Spark action creates one or more jobs; a shuffle or execution boundary divides a job into stages, and a stage runs one task per input or shuffle partition.

Submit a complete execution job. Wide exchanges create stage boundaries, then one unit per partition per stage; this exposes the state governed by Wide Deps.

Jobs, Stages, Tasks & Narrow vs Wide Dependencies in one concrete design

A map and filter remain in one stage; join/groupBy creates a shuffle and at least new reduction stages, with one task processing each of spark.sql.shuffle.partitions output partitions.

Key takeaway: Each Spark action creates one or more jobs

Jobs, Stages, Tasks & Narrow vs Wide Dependencies: failure mode and production response

Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.

Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies. Track Task parallelism as "executors times cores, but not all can be kept busy through one skewed stage", and make Shuffle partitions visible to the dataset owner.

Key takeaway: Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

How it works, step by step

Step 1 of 4
Stages

1. Action

Submit a complete execution job. Each Spark action creates one or more jobs; a shuffle or execution boundary divides a job into stages, and a stage runs one task per input or shuffle partition.

submit a complete execution job
Key numbers at this step
Task parallelism
executors times cores, but not all can be kept busy through one skewed stage
Tasks
A single scheduling unit per partition inside a stage, running the same closure on one data slice.

When to use it & trade-offs

What it is

Each Spark action creates one or more jobs; a shuffle or execution boundary divides a job into stages, and a stage runs one task per input or shuffle partition. A map and filter remain in one stage; join/groupBy creates a shuffle and at least new reduction stages, with one task processing each of spark.sql.shuffle.partitions output partitions.

Why it matters: Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized. Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

When to reach for it

  • Read how Spark splits work into jobs, stages, and tasks, and why wide deps force shuffles.
  • Choose Stages because it provides this capability: A sequence of stages forming one Spark action's execution graph.
  • Keep evidence for Task parallelism at "executors times cores, but not all can be kept busy through one skewed stage" so the last production stage-Retry/complete - failed tasks rerun on available executors-remains testable.

When to avoid or be careful

  • Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.
  • Do not conflate Tasks with Stages. The second concept contributes different behavior: A single scheduling unit per partition inside a stage, running the same closure on one data slice.
  • Release only after this guardrail is in place: Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

Stages / Tasks / Wide Deps - roles, limits, and operating cost

Stages
Best for: A sequence of stages forming one Spark action's execution graph.

Strengths: A sequence of stages forming one Spark action's execution graph. Each Spark action creates one or more jobs.

Watch out: Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.

Tasks
Best for: A single scheduling unit per partition inside a stage, running the same closure on one data slice.

Strengths: A single scheduling unit per partition inside a stage, running the same closure on one data slice. In this workflow it supports: Task scheduling - one unit per partition per stage.

Watch out: The operating cost is real: set from shuffled bytes and target task duration.

Wide Deps
Best for: Transformations whose output may combine records from many partitions, such as joins and aggregations, requiring a shuffle.

Strengths: Transformations whose output may combine records from many partitions, such as joins and aggregations, requiring a shuffle. Its output can be checked with Stage retries.

Watch out: Operating guardrail: Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Stages, and how would Task parallelism reveal it?”
  • “Why is Tasks distinct from this behavior: transformations whose output may combine records from many partitions, such as joins and aggregations, requiring a shuffle?”
  • Production clue: Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.
  • Design probe: Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

Benchmarks & rules of thumb

Task parallelism -
executors times cores, but not all can be kept busy through one skewed stage
Shuffle partitions -
set from shuffled bytes and target task duration
Stage retries -
driver resubmits failed partitions unless failures exhaust policy

Common mistakes & follow-ups

Common interview mistakes

  • Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.
  • Reporting success from a job exit code without checking Task parallelism and Shuffle partitions.
  • Treating this example as complete without replay, rollback, or access boundaries: A map and filter remain in one stage; join/groupBy creates a shuffle and at least new reduction stages, with one task processing each of spark.sql.shuffle.partitions output partitions.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A sequence of stages forming one Spark action's execution graph.
  • What does the recovery plan do when this occurs? Thousands of tiny tasks waste driver scheduling time, while a few huge partitions make one task dominate and create the false impression that the cluster is fully utilized.
  • When is Stages insufficient for Shuffle partitions, and what does adding Tasks cost in Task parallelism?
Interview tip: Lead with the data path and failure boundary: Each Spark action creates one or more jobs; a shuffle or execution boundary divides a job into stages, and a stage runs one task per input or shuffle partition. Then make the answer operational: Count stage input and shuffle partitions, target task partitions around executor parallelism and hundreds of MB, choose narrow transformations where possible, and break or salting skewed wide dependencies.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Jobs, Stages, Tasks & Narrow vs Wide Dependencies with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Transformations vs Actions & Lazy Evaluation
Next topic
Resilient Distributed Datasets (RDDs) vs DataFrames vs Datasets