Spark Execution Model

Every question in this family opens with a version of the same request: describe what happens after the job is submitted. A complete answer names 3 kinds of process and 4 units of work, then walks a real job through them. The interviewer is checking whether you can reason about a failure from the architecture alone, before anyone opens a log, because in an incident the architecture tells you which log to open first.

The 3 processes and what each one owns

Every application runs these

The driver requests 20 executors from the cluster manager, then sends tasks to them directly. Executors read the 10,000 input files in parallel and write the output table.
  • Viewing events (source, 10,000 files of 128 MB)
  • Driver (transform, Spark driver); retries: 4
  • Cluster manager (custom, Resource scheduler)
  • Executor 1 (transform, Spark executor, 16 GB); parallelism: 4 task slots
  • Executor 2 (transform, Spark executor, 16 GB); parallelism: 4 task slots
  • Executor 20 (transform, Spark executor, 16 GB); parallelism: 4 task slots
  • daily_watch_time (storage, Columnar files)
  • Driver -> Cluster manager
  • Cluster manager -> Executor 1
  • Cluster manager -> Executor 2
  • Cluster manager -> Executor 20
  • Viewing events -> Executor 1
  • Viewing events -> Executor 2
  • Viewing events -> Executor 20
  • Executor 1 -> daily_watch_time
  • Executor 2 -> daily_watch_time
  • Executor 20 -> daily_watch_time
  • Input: Viewing events
  • Control: Driver, Cluster manager
  • Compute: Executor 1, Executor 2, Executor 20
  • Output: daily_watch_time

From an action to 10,000 tasks

Nothing runs until the code asks for a result: writing a table, counting rows, collecting rows to the driver. Each such request is an action, and each action becomes 1 job. The driver cuts that job into stages wherever rows must regroup by key across machines, and runs each stage as a set of tasks, 1 per partition. A partition is the slice of data that a single task processes from start to finish; for a file scan it is usually a file, or a 128 MB piece of a larger one.

UnitCreated byIn the nightly job
ApplicationSubmitting the program1
JobEach action; here, the final write1
StageEach point where rows regroup by key2: scan, then aggregate
TaskEach partition of a stage10,000 in stage 1, 200 in stage 2

The nightly job reads 1.28 TB of viewing events stored as 10,000 files of 128 MB, so stage 1 has 10,000 tasks. The cluster runs 20 executors with 4 cores each, which makes 80 task slots. Tasks queue for those slots and run in waves, and each scan task here takes about 15 seconds.

Start any slow-job question with this arithmetic, because more executors cut wall-clock time only while a stage has more tasks than slots. Stage 2 has 200 tasks on 80 slots: 3 waves, and the third keeps only 40 slots busy. Tripling the cluster to 240 slots finishes stage 2 in a single wave with 40 slots idle. For stage 2, every executor added beyond that costs money and saves no time, though stage 1's 10,000 tasks would still speed up.

What happens when a task fails

The driver records how every partition was derived from its inputs, a chain called lineage, and that record is what makes failures recoverable. When a task fails, the driver schedules it again, possibly on another executor, up to spark.task.maxFailures attempts, 4 by default, before it fails the whole job. Losing an executor costs more: its local shuffle files vanish with it, so tasks in the next stage that try to fetch them report a fetch failure, and the driver reruns the upstream tasks that produced the lost files.

Claim heard in interviews
  • The code runs line by line as written
  • More executors always make a job faster
  • A failed task fails the job
  • The driver processes the rows
What the system does
  • Nothing runs until an action, and then the whole plan is optimized together
  • Only while a stage has more tasks than slots
  • The task gets up to 4 attempts first
  • The driver plans and schedules; executors read and transform the rows

About this lesson

This section is part of the Distributed Compute: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.