Cluster Sizing: Intermediate
Three ways to spend 60 cores
| Per shape | Tiny | Balanced | Fat |
|---|---|---|---|
| Executors × cores | 60 × 1 | 12 × 5 | 2 × 30 |
| Heap per executor | 6GB | 30GB | 180GB |
| Reserved memory, 300MB each | 18GB | 3.6GB | 0.6GB |
| Copies of the 40MB broadcast | 60, 2.4GB | 12, 480MB | 2, 80MB |
| Tasks sharing one JVM | 1 | 5 | 30 |
| Tasks rerun if one executor dies | 1 | 5 | 30 |
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
Why 5 lands in the middle
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.
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?
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.
spark.conf.set("spark.sql.shuffle.partitions", 4)per_country = views.groupBy("country").sum("seconds")per_country.collect()
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
| Tasks | Slots | Waves | Tasks in last wave | Idle slots in last wave |
|---|---|---|---|---|
| 200 | 100 | 2 | 100 | 0 |
| 250 | 100 | 3 | 50 | 50 |
| 300 | 100 | 3 | 100 | 0 |
| 2,400 | 100 | 24 | 100 | 0 |
| 2,450 | 100 | 25 | 50 | 50 |
A job reads 64GB of Parquet in full on 10 executors with 4 cores each. Roughly how many waves does the read stage take?
What doubling the count buys
| Executors of 5 cores | Slots | Waves | Stage time | Slot-minutes |
|---|---|---|---|---|
| 10 | 50 | 48 | 16 min | 800 |
| 20 | 100 | 24 | 8 min | 800 |
| 40 | 200 | 12 | 4 min | 800 |
| 80 | 400 | 6 | 2 min | 800 |
| 160 | 800 | 3 | 1 min | 800 |
| 600 | 3,000 | 1 | 20 s | 1,000 |
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.
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.
- 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
- 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
- ▸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.
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?
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.
Fitting executors on the node
| Step | Cores | Memory |
|---|---|---|
| Node hardware | 16 | 128GB |
| Held back for OS and daemons | 1 | 8GB |
| Offered to containers | 15 | 120GB |
| Per 5-core executor container | 5 | 40GB |
| Executor heap, container ÷ 1.10 | 36GB | |
| Overhead, 10% of heap | 3.6GB | |
| Left unclaimed per container | 0.4GB |
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
| Measure | Value |
|---|---|
| Containers per node | 3 |
| Container memory per node | 120GB |
| Cores in use per node | 15 of 15 |
| Slots across 14 nodes | 210 |
| Measure | Value |
|---|---|
| Containers per node | 2 |
| Container memory per node | 88GB |
| Cores in use per node | 10 of 15 |
| Slots across 14 nodes | 140 |
The driver takes a space too
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?
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
Step 3: pick the slot count
| Slots | Stage 1 waves | Stage 1 time | Stage 2 waves | Stage 2 time | Total |
|---|---|---|---|---|---|
| 100 | 82 | 41 min | 20 | 15 min | 56 min |
| 150 | 55 | 27.5 min | 14 | 10.5 min | 38 min |
| 200 | 41 | 20.5 min | 10 | 7.5 min | 28 min |
| 300 | 28 | 14 min | 7 | 5.3 min | 19.3 min |
Step 4: slots to executors to nodes
| Decision | Value | Where it came from |
|---|---|---|
| Read tasks | 8,192 | 1TB ÷ 128MB splits |
| Shuffle partitions | 2,000 | 400GB at about 200MB each |
| Slots | 200 | Smallest count inside 30 minutes |
| Executors | 40 × 5 cores, 36GB heap | The 5-core rule and the node fit |
| Nodes | 14, each 16 cores and 128GB | 3 executors per node, plus the driver |
| Expected runtime | 28 min | 20.5 min + 7.5 min |
Step 5: check the first real run
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?
> 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.
spark.sql.shuffle.partitions tasks, or fewer once AQE merges them.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
- 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
- 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
- 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
- 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
- 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