Partitions, Tasks, and Waves
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()| 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 |
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.
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.
| 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?
- Two waves: 64 tasks, 1 for each GB of input, spread over 40 slots
- Fifty-two waves: 512 tasks spread over 10 slots, 1 slot per executor
- Thirteen waves: 512 tasks over 40 slots, with 32 tasks in the last wave
- Five waves: 200 tasks, the shuffle partition default, over 40 slots
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.
About this lesson
This section is part of the Cluster Sizing: Intermediate 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.