Over-Coalescing
Coalescing reads a single number per reducer partition, its shuffle bytes, and merges neighbouring partitions until each merged task approaches a target size. The target comes from 3 settings. spark.sql.adaptive.advisoryPartitionSizeInBytes is the ceiling, 64MB. spark.sql.adaptive.coalescePartitions.minPartitionSize is the floor, 1MB. spark.sql.adaptive.coalescePartitions.parallelismFirst, true by default, shrinks the target so the stage keeps about a task for every slot of the cluster's default parallelism. Set it to false, as the Spark docs advise for busy shared clusters, and every stage packs toward 64MB.
The overhead coalescing trades away
Every reducer partition, even an empty one, launches a task and reports back to the driver, and coalescing merges neighbours to save that overhead. The board shows the plan before the runtime merge, so every reducer is drawn.
from pyspark.sql import functions as F
spark.conf.set("spark.sql.shuffle.partitions", 6)
totals = sales.groupBy("region").agg(F.sum("amount").alias("revenue"))
totals.collect()| order_id | region | amount |
|---|---|---|
| 1 | east | 120 |
| 2 | west | 80 |
| 3 | north | 45 |
| 4 | east | 60 |
| 5 | west | 30 |
| 6 | north | 99 |
| 7 | east | 15 |
| 8 | west | 72 |
| 9 | north | 10 |
| 10 | east | 40 |
| 11 | west | 55 |
| 12 | north | 25 |
With 6 reducers on 3 regions, at least 3 of them are empty, and merging those saves 3 launches. Merging all 6 into a single task saves 2 more and puts the whole stage on 1 core while the other cores the job holds wait. Coalescing pays only while the merged task count stays at or above the cores free to run it. Below that line, each merge buys milliseconds of overhead with a core's worth of wall time, which is the trade the next table prices.
When the target ignores what happens next
Shuffle bytes measure what a reducer reads, and a stage often spends its time on what it does with those rows afterwards. A stage that explodes arrays, joins to a fanned-out table or runs a heavy function can turn 64MB of input into minutes of CPU. Packing such a stage trades a few milliseconds of task overhead for idle cores.
Take a shuffle that delivers 2.3GB to a stage that expands each row 40 times, so the stage burns about 4 seconds of CPU per MB of input. The cluster has 400 cores, and spark.sql.shuffle.partitions is 200.
| Settings | Tasks | Input per task | Task time | Cores busy of 400 |
|---|---|---|---|---|
| No coalescing | 200 | 11.8MB | 47 s | 200 |
| parallelismFirst true (default) | 200 | 11.8MB | 47 s | 200 |
| parallelismFirst false | 37 | about 64MB | 256 s | 37 |
| initialPartitionNum 800, parallelismFirst true | 400 | 5.9MB | 24 s | 400 |
With parallelismFirst false, the stage runs more than 5 times longer while 363 cores wait. The default leaves the stage at 200 tasks, which is no worse than static planning and no better, because coalescing merges partitions and never splits them. Only a larger starting count fills all 400 cores.
Packing can also create skew. Coalescing equalises bytes, so any cost that does not track bytes ends up uneven across fewer, larger tasks. Partitions whose keys compress well hold many more rows per MB, and a merged task built from 10 of them can carry several times the rows of its neighbour at the same 64MB. On the stage page this shows as task times spreading apart while shuffle read sizes stay level.
Rebalancing a stage that was packed too tight
Fixes, narrowest first
- repartition(400, "page_id"): an explicit count pins that shuffle. AQE leaves a count you wrote untouched, and a following groupBy on the same key reuses the partitioning.
- A lower advisoryPartitionSizeInBytes for the job whose later stages expand rows.
- parallelismFirst left at true for jobs dominated by expanding stages.
- A higher initialPartitionNum so coalescing has partitions worth keeping.
from pyspark.sql import functions as F
events = spark.table("page_events")
per_page = (
events.repartition(400, "page_id")
.groupBy("page_id")
.agg(F.count("*").alias("views"))
)
One shuffle and 2 stages: the groupBy finds its input already hashed on page_id and adds no exchange of its own, and the 400 stays 400 whatever the bytes measure. Pin a count when you know the stage's cost per byte. When input volume swings 10 times from day to day, pin the count that suits the largest day.
Watch the largest task grow
The cell buckets order_items rows by product_id modulo buckets, a stand-in for reducer partitions. Run it with 4, then change buckets to 2 and to 1. The top row is the stage's slowest task: halving the bucket count roughly doubles it, which is what over-coalescing does to wall time when every core could have taken a share.
In the cell, buckets is a number you pick. In a real job that number is the explicit count in repartition(n, key), and the key you pass decides what follows. Pass the key the groupBy uses and the aggregate reads the existing partitioning with no exchange of its own. Pass a different key, or none, and the groupBy hashes the rows a second time, so the stage you pinned is no longer the one doing the work.
About this lesson
This section is part of the Adaptive Query Execution: Advanced 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.