IntermediateSpark · 14 min · 12 challenges

Cluster Sizing: Intermediate

Picture a nightly Spark job at Netflix that rolls up a day of viewing events before the morning dashboards refresh. It runs on 2 executors with 30 cores each, and when one of them dies, half the running work dies with it. The fix could be more executors, bigger ones, or a different shape. This lesson gives you the arithmetic to choose before the next run.

The 5-Core Rule

Daily Life
Interviews
An executor is a single JVM with a fixed number of cores and a fixed heap, and every executor in an application gets the same shape. A budget of cores and memory can be cut many ways: lots of small executors, a few large ones, or something between. The cut decides how much memory goes to fixed costs, how many copies of shared data the cluster holds, and how much in-flight work a crash throws away.

Three ways to spend 60 cores

Take a budget of 60 cores and 360GB of heap, which is 6GB per core, and a 40MB lookup table the job broadcasts after raising the broadcast threshold. Here are 3 shapes that spend the whole budget, each giving its executors 6GB of heap per core.
Per shapeTinyBalancedFat
Executors × cores60 × 112 × 52 × 30
Heap per executor6GB30GB180GB
Reserved memory, 300MB each18GB3.6GB0.6GB
Copies of the 40MB broadcast60, 2.4GB12, 480MB2, 80MB
Tasks sharing one JVM1530
Tasks rerun if one executor dies1530
//

What tiny executors cost

Fixed costs are paid per executor, whatever its core count. Spark holds back 300MB of every heap as reserved memory, and every container also carries spark.executor.memoryOverhead, which by default is 10% of the heap and never less than 384MB. A broadcast table lands once in every executor, so the tiny shape stores the lookup table 60 times. Anything held in memory belongs to the JVM that holds it: a cached partition or a broadcast copy serves only the tasks running in that executor, and with 1 core per executor that is a single task.

//

What fat executors cost

A 30-core executor runs 30 tasks inside a single process. They share a single storage client and a single pool of network connections, and storage clients lose throughput when too many concurrent tasks read and write through them, so the extra cores wait on I/O. Garbage collection on a 180GB heap pauses all 30 tasks at once, and those pauses lengthen as the heap grows. A crash reruns 30 tasks, half of everything the cluster had in flight.

Why 5 lands in the middle

Five cores per executor sits where the 2 cost curves cross. Fixed costs split 5 ways, so reserved memory and broadcast copies fall to a fifth of the tiny shape's. Each JVM stays small enough that a crash or a pause touches only 5 tasks, and its storage client serves as many concurrent readers as it handles at full speed. Treat 5 as a starting shape: 4 or 6 behave about the same, and the rule steers you away from 1 and 30.

Cores in the same executor also compete for its execution memory. With N tasks running, Spark guarantees each task at least 1/(2N) of the execution pool and caps it at 1/N. Raising spark.executor.cores without raising spark.executor.memory shrinks every task's share, and the first symptom is spill.

TIP
To price an inherited job built on 1-core executors, multiply its executor count by 300MB of reserved heap plus the 384MB overhead floor. At 200 executors that is about 137GB committed before a single row is read.
Multiple Choice

A teammate proposes 4 executors of 25 cores with 150GB of heap each, 100 cores in total. Which reshaping fixes that shape's problems while keeping 100 cores and 6GB of heap per core?

Partitions, Tasks, and Waves

Daily Life
Interviews

A stage runs 1 task per partition, and the cluster runs 1 task per slot at a time. Slots are executors × spark.executor.cores. Together those facts turn a data size into a schedule you can read before submitting anything: count the tasks in each stage, divide by the slots, and round up. The result is the wave count, the number of rounds the slots work through to finish the stage.

Tasks in a read stage

A file scan cuts its input into splits of at most spark.sql.files.maxPartitionBytes, 128MB by default, measured in bytes on disk. A full read of a 300GB Parquet table gives about 300 × 1024 ÷ 128 = 2,400 splits, so 2,400 tasks. Partition pruning drops whole files, so count only the partitions the query reads. Column projection makes each task read fewer bytes but leaves the task count alone, because splits are cut from file sizes.

On small inputs Spark shrinks the split so every core gets a share of the data. The 128MB arithmetic holds once the input is larger than 128MB per core in the cluster.

Tasks in a shuffle stage

After a shuffle, the stage runs spark.sql.shuffle.partitions tasks, 200 unless you set it. Size it from the shuffle write at roughly 100MB to 200MB per partition: a stage that writes 120GB of shuffle data at 200MB per partition wants about 600. With AQE on, Spark measures the shuffle output and can merge small partitions, so the stage may run fewer tasks than the setting says.

The board below reads a 12-row table as 6 input partitions and groups it into 4 shuffle partitions. Watch each stage's task count: 6 tasks read and 4 tasks aggregate, and a shuffle partition that receives no keys still gets a task. The board draws the plan as written, with no AQE merging.
spark.conf.set("spark.sql.shuffle.partitions", 4)
per_country = views.groupBy("country").sum("seconds")
per_country.collect()
views12 rows
user_id
country
seconds
1
US
120
2
BR
45
3
IN
300
4
US
60
5
IN
90
6
BR
210
7
US
30
8
IN
150
9
BR
75
10
US
240
11
IN
15
12
BR
180
Preparing the animation

Run this on the practice data. It repartitions orders into 4 partitions and counts the rows in each, so every output row is 1 task of the stage that follows the repartition. Change 4 to 3 and run it again: the number of rows follows.

orders = spark.table("orders")

rows_per_task = (
    orders.repartition(4)
    .selectExpr("spark_partition_id() AS task")
    .groupBy("task")
    .count()
    .orderBy("task")
)

rows_per_task.show()

Waves

With 20 executors of 5 cores the cluster has 100 slots. The 2,400-task read stage runs in 2,400 ÷ 100 = 24 waves, and the 600-task shuffle stage in 6. If a read task takes 20 seconds, the read stage needs roughly 24 × 20 = 480 seconds, 8 minutes. A slot picks up its next task the moment it frees, so waves blur together in practice, but when tasks are similar in size the total stays close to waves × task time.
TasksSlotsWavesTasks in last waveIdle slots in last wave
20010021000
25010035050
30010031000
2,400100241000
2,450100255050
Multiple Choice

A job reads 64GB of Parquet in full on 10 executors with 4 cores each. Roughly how many waves does the read stage take?

A partly filled last wave is where slots go to waste. At 250 tasks the third wave takes as long as a full one while half the cluster waits. Two fixes: make the task count a multiple of the slot count, or run enough waves that a single partial one is a small share of the stage. With 25 waves, a half-empty last wave wastes 2% of the stage's slot time; with 3 waves it wastes 17%. Fewer tasks than slots is worse still: 60 tasks on 100 slots run in 1 wave with 40 slots idle from start to finish.
Loading the challenge

Scale Count Before Size

Daily Life
Interviews
Once an executor shape works, a job that needs more capacity can get it 2 ways: more executors of that shape, or bigger executors. Reach for more executors first. The validated shape keeps its memory per core, its broadcast cost and its crash radius, and the slot count rises in step with the executor count.

What doubling the count buys

Take the 2,400-task read stage again, with tasks of 20 seconds. On 100 slots it runs 24 waves and takes 8 minutes; on 200 slots it runs 12 waves in 4 minutes. The capacity consumed stays the same: 100 slots for 8 minutes and 200 slots for 4 minutes are both 800 slot-minutes. While tasks outnumber slots, more executors buy speed at a flat cost.
Executors of 5 coresSlotsWavesStage timeSlot-minutes
10504816 min800
20100248 min800
40200124 min800
8040062 min800
16080031 min800
6003,000120 s1,000
The flat line ends when slots outnumber tasks. At 3,000 slots the stage still has 2,400 tasks, so it runs a single 20-second wave with 600 slots idle, and the run pays for all 3,000. Each stage hits this ceiling at its own task count: in a job with a 2,400-task read and a 600-task shuffle, the shuffle stage stops gaining at 600 slots, long before the read stage does. Past a stage's ceiling, only more partitions make it faster.
//

Why not more cores per executor

The tempting shortcut is to double spark.executor.cores from 5 to 10 on the same executors. Slots double too, but the job walks back into the fat-executor costs: 10 tasks per storage client, a heap with twice the tasks behind every pause, and twice the work rerun per crash. Unless memory doubles with it, each task's share of execution memory halves as well.

Multiple Choice

A job's slowest stage has 180 tasks of about 2 minutes each, running on 24 executors of 5 cores with no spill. What does moving to 48 executors of the same shape do to that stage?

When size is the right lever

Bigger executors help when a single task needs more memory than it gets. A task's memory comes from its own executor's unified pool, shared with the other tasks running beside it, so it depends on heap per core and never on how many executors exist. Adding 50 executors leaves every task with the memory it had. Before raising memory per core, try to shrink the task's input: more shuffle partitions, or a salted hot key. When the input can't shrink, raise spark.executor.memory with cores held at 5, or keep the heap and drop to 4 cores.

✓Add executors
  • The stage has more tasks than slots
  • Tasks finish without spilling
  • Many short waves make up the stage time
  • The goal is a shorter run at about the same cost
✗Grow each executor
  • One task spills or fails on memory
  • Its partition can't be split any further
  • A large broadcast takes much of each heap
  • Memory per task, not slot count, is the limit
When more executors won't help
  • ▸The stage already has fewer tasks than slots, so the new slots get no work.
  • ▸One straggler holds the stage open: the other tasks finish sooner and the stage still waits on it.
  • ▸The driver is the bottleneck, as with a large collect(), and executors idle while it works.
  • ▸The source caps read throughput, so extra readers queue behind the same limit.
Multiple Choice

A stage has 2,000 shuffle partitions of about 200MB. One task processes 9GB for a single customer ID and spills heavily while the others finish cleanly. Which change targets the problem?

What a Node Keeps for Itself

Daily Life
Interviews
Executors run on nodes, and no node hands all of its hardware to Spark. These processes need CPU and memory on every machine before any executor starts:
Operating system services and the kernel's page cache
Operating system services and the kernel's page cache
The cluster manager's node agent, which launches and monitors containers
The cluster manager's node agent, which launches and monitors containers
Log shippers forwarding executor logs off the machine
Log shippers forwarding executor logs off the machine
Metrics and monitoring collectors
Metrics and monitoring collectors
Storage daemons, when the same nodes also hold data
Storage daemons, when the same nodes also hold data
The cluster manager offers containers only what remains after them. An executor request bigger than that remainder either stays pending or fits on fewer nodes than you planned.

The reservation and the container

This lesson plans with 1 core and 8GB held back on every 16-core, 128GB node, a common starting reservation; nodes that also run storage or monitoring daemons keep more. The cluster manager's configuration lists what it actually offers to containers, so read the numbers there instead of the hardware spec. Each container is larger than the heap inside it: it holds spark.executor.memory plus spark.executor.memoryOverhead, which defaults to 10% of the heap with a 384MB floor and covers off-heap buffers, thread stacks and, in Python jobs, the Python worker processes.

Hold back a core as well as memory. When executors claim every core, the node agent competes with a busy task on each of them for CPU time, and its heartbeats to the cluster manager can fall behind.

Fitting executors on the node

StepCoresMemory
Node hardware16128GB
Held back for OS and daemons18GB
Offered to containers15120GB
Per 5-core executor container540GB
Executor heap, container ÷ 1.1036GB
Overhead, 10% of heap3.6GB
Left unclaimed per container0.4GB
Fifteen offered cores make 3 executors of 5. The 120GB splits into 3 containers of 40GB, and since a container is its heap plus 10%, the heap can be at most 40 ÷ 1.1 ≈ 36.4GB. Round down to 36GB and the overhead is 3.6GB, leaving 0.4GB of slack in each container.
7.2GB
heap per core on this node
6.25%
node memory held back

Inside each 36GB heap, Spark keeps its 300MB reserve and gives spark.memory.fraction, 0.6 by default, of the rest to the unified pool: 36GB less 0.3GB, times 0.6, is about 21.4GB. With 5 tasks running and nothing cached, each can claim up to about 4.3GB.

The same 2 divisions work on any node: offered cores ÷ 5 gives executors per node, and offered memory ÷ that count gives each container's size.

The container that doesn't fit

Ask for a 40GB heap instead and the container grows to 44GB. Three of those need 132GB, more than the 120GB on offer, so the cluster manager places 2 per node. Each node now runs 10 tasks on 15 offered cores, and a third of the capacity you pay for sits idle. A request that looked like 11% more memory per executor cost a third of the slots.
36GB heap
MeasureValue
Containers per node3
Container memory per node120GB
Cores in use per node15 of 15
Slots across 14 nodes210
40GB heap
MeasureValue
Containers per node2
Container memory per node88GB
Cores in use per node10 of 15
Slots across 14 nodes140
//

The driver takes a space too

In cluster mode the driver runs in a container on one of the worker nodes, in room an executor could have used. Give the driver a container no larger than an executor's and subtract 1 executor from the cluster's capacity. On 14 nodes that costs 1 space in 42; on 2 nodes it costs 1 in 6.
TIP
Check any node plan by dividing offered memory by offered cores. Here 120GB over 15 cores is 8GB of container per core, so a 5-core executor gets a 40GB container and a heap a little over 36GB.
Multiple Choice

A node has 32 cores and 256GB. You hold back 2 cores and 16GB for the OS and daemons, run 5-core executors, and keep the default 10% overhead. What is the largest heap that still packs the most executors onto the node?

A Worked Sizing

Daily Life
Interviews
Here is the whole method on a single job. A daily batch reads 1TB of Parquet events, filters them, joins a 6MB country table, aggregates by day and title, and writes the result. It must finish within 45 minutes of starting, and the team keeps a third of that as headroom, so the compute target is 30 minutes.
Parquet files
events (1TB)
broadcast
countries (6MB)
Spark stage
Stage 1: scan, filter, join
PARALLELISM8,192 partitions
Spark stage
Stage 2: aggregate
PARALLELISM2,000 partitions
Parquet files
daily rollup
SLA45 min

The 6MB country table is under the 10MB broadcast threshold, so it joins inside stage 1 without a shuffle. Stage 1 also runs the partial aggregation, and the groupBy adds the job's only shuffle, about 400GB, between the 2 stages.

Step 1: bytes to tasks

At 128MB per split, 1TB on disk, which is 1,024GB, becomes 8,192 read tasks. A test run on a 10% sample wrote 40GB of shuffle data, which scales to about 400GB for the full input. At roughly 200MB per partition that calls for 2,048 partitions; round to 2,000, a count that will divide evenly into the slot count chosen below, and set spark.sql.shuffle.partitions to it.

Step 2: measure task time

Task durations come from a measurement. On the sample run a stage 1 task took about 30 seconds and a stage 2 task about 45, with shuffle partitions set to 200 so each held close to 200MB. Task time depends on the bytes a task handles, and the sample's tasks handled the same sizes as the full run's will, so the durations carry over even though the sample had a tenth as many tasks.

Step 3: pick the slot count

SlotsStage 1 wavesStage 1 timeStage 2 wavesStage 2 timeTotal
1008241 min2015 min56 min
1505527.5 min1410.5 min38 min
2004120.5 min107.5 min28 min
3002814 min75.3 min19.3 min
The 200-slot row is the smallest that meets the 30-minute target, and its stage 2 runs exactly 10 full waves. Stage 1's last wave holds 192 tasks on 200 slots, so even its tail keeps 96% of the slots busy. The 300-slot row finishes under 9 minutes sooner at almost the same cost: 300 slots for 19.3 minutes is about 5,790 slot-minutes against 5,600, roughly 3% more. That 3% is idle time in partial waves, since stage 2's seventh wave holds only 200 tasks and a third of the cluster waits through it. Both rows meet the deadline, so take the smaller cluster: fewer nodes to place, and no partial wave in stage 2.
93 task-hours
total task time in one run
Spread across 200 slots, that work comes to about 28 minutes, which agrees with the wave arithmetic when every wave is full.

Step 4: slots to executors to nodes

At 5 cores each, 200 slots are 40 executors. The node from the previous section, 16 cores and 128GB with 1 core and 8GB reserved, holds 3 executors with 36GB heaps, which gives each task about 4.3GB of execution memory; the sample run showed no spill at that size. Forty executors need 14 nodes, since 40 ÷ 3 rounds up to 14. Those nodes hold 42 executor spaces, the driver takes 1 in cluster mode, and 41 remain for the 40 the plan needs.
DecisionValueWhere it came from
Read tasks8,1921TB ÷ 128MB splits
Shuffle partitions2,000400GB at about 200MB each
Slots200Smallest count inside 30 minutes
Executors40 × 5 cores, 36GB heapThe 5-core rule and the node fit
Nodes14, each 16 cores and 128GB3 executors per node, plus the driver
Expected runtime28 min20.5 min + 7.5 min
Loading the challenge

Step 5: check the first real run

Planning numbers are predictions, so set them beside the first full run in the Spark UI: the task count of each stage, the median task duration in each, and any spill. If stage 2 shows fewer than 2,000 tasks, AQE merged small partitions after measuring them. If median task times came in above 30 and 45 seconds, redo the slot arithmetic with the measured values before adding nodes; if spill appears, revisit memory per core.
Multiple Choice

Next quarter the events grow to 2TB. Per-task times stay the same, and the shuffle write doubles to 800GB at the same partition size. Which plan keeps the run near 28 minutes?

PUTTING IT ALL TOGETHER

> Picture the Netflix viewing rollup from the intro after a re-plan. The team measures task times on a 10% sample, counts 8,192 read tasks for 1TB and 2,000 shuffle partitions for 400GB, picks 200 slots to land inside the morning deadline, and replaces the 2 fat executors with 40 executors of 5 cores placed 3 to a node on 14 nodes. When a holiday doubles the viewing data, they double the executors and the shuffle partitions together and keep the 28-minute runtime.

The 5-core rule fixed the executor shape, so every later change was a change of executor count alone.
Wave arithmetic turned 8,192 read tasks and 2,000 shuffle tasks into a 28-minute estimate before the first full run.
The node reservation set 3 executors per 16-core, 128GB node, and keeping the heap at 36GB kept all 3 placed.
Doubled data doubled the tasks, and doubling executors alongside them held the waves at 41 and 10, so scaling count beat scaling size.
KEY TAKEAWAYS
About 5 cores per executor balances fixed per-executor costs against crowded JVMs: 4 to 6 work, 1 and 30 do not.
A read stage gets 1 task per 128MB split of the files it reads; a stage after a shuffle gets spark.sql.shuffle.partitions tasks, or fewer once AQE merges them.
Waves are tasks ÷ slots, rounded up. A thin last wave, or fewer tasks than slots, leaves paid-for cores idle.
Scale by adding executors of a proven shape while tasks outnumber slots; raise memory per core only when one task's input can't shrink.
Plan nodes from what the cluster manager offers after the OS and daemons, and size each container as heap plus overhead so the planned count fits.
Size from measured task times: bytes to tasks, tasks and a time target to slots, slots to executors, executors to nodes.

Turn bytes into tasks, tasks into waves, and waves into executors and nodes you can defend.

Category
Spark
Difficulty
intermediate
Duration
14 minutes
Challenges
12 hands-on challenges

Topics covered: The 5-Core Rule, Partitions, Tasks, and Waves, Scale Count Before Size, What a Node Keeps for Itself, A Worked Sizing

Lesson Sections

  1. The 5-Core Rule

    An executor is a single JVM with a fixed number of cores and a fixed heap, and every executor in an application gets the same shape. A budget of cores and memory can be cut many ways: lots of small executors, a few large ones, or something between. The cut decides how much memory goes to fixed costs, how many copies of shared data the cluster holds, and how much in-flight work a crash throws away. Three ways to spend 60 cores Take a budget of 60 cores and 360GB of heap, which is 6GB per core, an

  2. Partitions, Tasks, and Waves

    Tasks in a read stage On small inputs Spark shrinks the split so every core gets a share of the data. The 128MB arithmetic holds once the input is larger than 128MB per core in the cluster. Tasks in a shuffle stage The board below reads a 12-row table as 6 input partitions and groups it into 4 shuffle partitions. Watch each stage's task count: 6 tasks read and 4 tasks aggregate, and a shuffle partition that receives no keys still gets a task. The board draws the plan as written, with no AQE merg

  3. Scale Count Before Size

    Once an executor shape works, a job that needs more capacity can get it 2 ways: more executors of that shape, or bigger executors. Reach for more executors first. The validated shape keeps its memory per core, its broadcast cost and its crash radius, and the slot count rises in step with the executor count. What doubling the count buys Take the 2,400-task read stage again, with tasks of 20 seconds. On 100 slots it runs 24 waves and takes 8 minutes; on 200 slots it runs 12 waves in 4 minutes. The

  4. What a Node Keeps for Itself

    Executors run on nodes, and no node hands all of its hardware to Spark. These processes need CPU and memory on every machine before any executor starts: The cluster manager offers containers only what remains after them. An executor request bigger than that remainder either stays pending or fits on fewer nodes than you planned. The reservation and the container Hold back a core as well as memory. When executors claim every core, the node agent competes with a busy task on each of them for CPU ti

  5. A Worked Sizing

    Here is the whole method on a single job. A daily batch reads 1TB of Parquet events, filters them, joins a 6MB country table, aggregates by day and title, and writes the result. It must finish within 45 minutes of starting, and the team keeps a third of that as headroom, so the compute target is 30 minutes. Step 1: bytes to tasks Step 2: measure task time Task durations come from a measurement. On the sample run a stage 1 task took about 30 seconds and a stage 2 task about 45, with shuffle parti