Driver, executors & cluster managers

Apache Spark · Spark Architecture

Theory

Every Spark application has one driver and many executors. The driver is the process that runs your main program (your PySpark script or notebook): it holds the SparkSession, turns your code into a plan, splits it into tasks and tracks their progress. Executors are worker processes on the cluster machines; each one has a number of cores (task slots) and memory, runs the tasks the driver sends, and can cache data for reuse.

The cluster manager is the service that hands out machines: Spark's own Standalone manager, Hadoop YARN or Kubernetes, while local mode runs everything in one process. For example, with 10 executors of 4 cores each, Spark can run 40 tasks at the same time; if a 400-partition dataset is processed, the driver schedules 40 tasks at a time across the executors until all 400 are done. Calling collect() brings results back to the driver, so large results can crash it.

Why we use it

Knowing which code runs on the driver and which runs on executors explains most performance and memory problems: out-of-memory on the driver after collect(), idle executors from too few partitions, or slow jobs from too few cores. It is also a standard interview question.

When to use it

Think about this whenever you size a cluster (executor count, cores, memory), choose a deploy mode, or debug failures. In client mode the driver runs where you submit (handy for notebooks); in cluster mode it runs inside the cluster (better for production jobs).

How it works

  1. spark-submit or notebook starts the driver
  2. Driver asks the cluster manager for executors
  3. Cluster manager launches executor processes
  4. Driver turns actions into stages and tasks
  5. Executors run tasks on their partitions
  6. Did an executor die? Yes: Driver re-schedules its tasks elsewhere. No: Executors return results.
  7. Driver finishes the job and releases resources

Syntax

spark-submit --master yarn --deploy-mode cluster \
  --num-executors 10 --executor-cores 4 --executor-memory 8g \
  --driver-memory 4g job.py
--master local[*] | spark://host:7077 | yarn | k8s://https://host:443

Example

from pyspark.sql import SparkSession, functions as F

spark = (SparkSession.builder.master("local[1]")
         .config("spark.ui.enabled", "false")
         .getOrCreate())

orders = spark.createDataFrame([
    (101, 1, "North", "Laptop", 1, 55000.0, "2026-01-05"),
    (102, 2, "West",  "Phone",  2, 30000.0, "2026-01-06"),
    (103, 1, "North", "Mouse",  3,  1500.0, "2026-01-06"),
    (104, 3, "South", "Laptop", 1, 52000.0, "2026-01-07"),
    (105, 4, "West",  "Tablet", 1, 25000.0, "2026-01-08"),
    (106, 2, "East",  "Phone",  1, 15000.0, "2026-01-08"),
    (107, 5, "North", "Tablet", 2, 48000.0, "2026-01-09"),
    (108, 3, "South", "Mouse",  4,  2000.0, "2026-01-10"),
], "order_id INT, customer_id INT, region STRING, product STRING, "
   "qty INT, amount DOUBLE, order_date STRING"
).withColumn("order_date", F.to_date("order_date"))

sc = spark.sparkContext
print("master:", sc.master)
print("default parallelism (task slots):", sc.defaultParallelism)

# Executors work on partitions; spark_partition_id() shows which one holds each row
parts = orders.repartition(3)
print("partitions:", parts.rdd.getNumPartitions())
(parts.groupBy(F.spark_partition_id().alias("partition"))
      .count().orderBy("partition").show())

# collect() brings rows back to the DRIVER: fine for small results only
top = orders.orderBy(F.desc("amount")).limit(2).collect()
print([r.order_id for r in top])

# Output:
# master: local[1]
# default parallelism (task slots): 1
# partitions: 3
# +---------+-----+
# |partition|count|
# +---------+-----+
# |        0|    3|
# |        1|    3|
# |        2|    2|
# +---------+-----+
#
# [101, 104]

Key points

Apache Spark topics

Spark Fundamentals

Spark Architecture

RDDs

DataFrames

Aggregations & Joins

Spark SQL

Window Functions

Cleaning & Complex Types

UDFs

Performance Tuning

Structured Streaming

MLlib & Ecosystem

Interview Prep

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master Apache Spark

PySpark and Spark SQL from the ground up: DataFrames, joins, window functions, partitions and performance tuning, Structured Streaming and more. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice PySpark Write real PySpark — DataFrames, joins, window functions and Spark SQL — in a live editor with hidden tests and instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Spark  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

{{ sec.h }}

{{ sec.body }}

{{ sec.body }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
On screen — where to click

{{ rich.ui.title }}

{{ u.text }}
  1. {{ st.n }}{{ st.text }}
{{ rich.ui.title }}
{{ u.text }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
Want to run this yourself? Try the same API on Practice PySpark Online right in your browser, or run it locally after pip install pyspark (needs Java 17+), or paste it into a Databricks Free Edition notebook.
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic