Executors, Cores, and Memory

Concepts covered: Configuration Tuning

An executor is a JVM process that the cluster starts for your application and keeps running until the application ends. Each executor owns some CPU cores and a fixed block of memory, and the driver sends it work as tasks, 1 task per partition. Spark lets you choose how many executors start, how many cores each one gets, and how much memory each one gets, through 3 settings. This lesson calls them the 3 knobs, because together they decide how much of the cluster your job can use.

KnobWhat it setsWhat it limits
`spark.executor.instances`How many executor processes startThe job's totals, by multiplying the other 2 knobs
`spark.executor.cores`CPU cores in each executorHow many tasks 1 executor runs at the same moment
`spark.executor.memory`Heap size of each executor's JVMHow much data the tasks on that executor hold at once

Cores turn into slots

Each core runs 1 task at a time. An executor with 4 cores works on 4 partitions side by side, and its next task waits until one of those 4 finishes. Across the whole job, executors multiplied by cores gives the slot count, the ceiling on how many tasks run at any instant. A machine in the cluster that these knobs did not claim adds nothing to that count, however many cores it has.

Memory belongs to the executor

spark.executor.memory sets the heap of 1 executor JVM. Every task running on that executor draws from it, and so does any data you cached there. Tasks on different executors share nothing: a task on executor 3 cannot borrow free memory from executor 7. Spark reads the value as a size string such as 16g or 512m; write the unit every time, so the next reader doesn't have to guess it.

A configured session

This is the session for the nightly rollup from the intro. The knobs go on the builder, before getOrCreate() starts the application, and each value is passed as a string.

from pyspark.sql import SparkSession

spark = (
    SparkSession.builder
    .appName("nightly_watch_time")
    .config("spark.executor.instances", "25")
    .config("spark.executor.cores", "4")
    .config("spark.executor.memory", "16g")
    .getOrCreate()
)

Executors times cores bounds how fast a stage moves, and executors times memory bounds how much data the job can hold across the cluster at once. Between them, those 2 products tell you what this session can do.

The driver hands tasks to 25 executors, and each runs 4 of them at a time on its own 16g heap. Executors 3 to 24 are left out of the drawing.
  • Driver (custom, plans and schedules)
  • Executor 1 (transform, 16g heap); parallelism: 4 slots
  • Executor 2 (transform, 16g heap); parallelism: 4 slots
  • Executor 25 (transform, 16g heap); parallelism: 4 slots
  • Driver -> Executor 1
  • Driver -> Executor 2
  • Driver -> Executor 25

Reading a config at a glance

Configs found in older notebooks read the same way. Here are 3, each reduced to its 2 products:

InstancesCoresMemorySlotsTotal heap
4832g32128GB
5028g100400GB
101616g160160GB

The middle row matches the nightly rollup on both products, carved into 50 smaller executors where the rollup uses 25 larger ones. The last row has the most slots and the least memory, which the Memory per Core section shows is the shape most likely to run out of room.

To confirm the knobs took effect, open the Executors tab of the Spark UI, which lists 1 row per executor with its core count. Its memory column shows less than the heap you set, because it counts only the part Spark manages for running tasks and cached data; a later lesson takes that part apart.

A job runs on 10 executors, each with 2 cores and 8g of memory. You change only spark.executor.cores to 4 and restart. What does the job get now?

About this lesson

This section is part of the Cluster Sizing: 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.