Broadcasts That Outgrow Memory
When a shuffle stage finishes, the driver holds its map output statistics: the bytes each map task wrote for each reducer. If a side of a pending sort-merge join sums to less than spark.sql.adaptive.autoBroadcastJoinThreshold, AQE rewrites the join as a broadcast hash join. That setting is unset by default, so it inherits spark.sql.autoBroadcastJoinThreshold, which is 10MB unless someone raised it.
Those bytes are shuffle blocks as they sit on disk: rows in binary row format, compressed, because spark.shuffle.compress is true by default. A broadcast holds the same rows uncompressed, inside a hash table built for lookups. So the measurement gives the exact size on disk and says nothing about the size in memory.
From shuffle files to a copy in every executor
Take the title-catalog side of a nightly join. The team raised the broadcast threshold to 100MB for small lookup tables. Tonight the catalog side, after its filter, lands in a shuffle stage whose blocks compress about 7:1, because genre, rating and country values repeat row after row. The cluster runs 40 executors with 4 cores and 4GB of heap each.
- 90MB
- 630MB
- 24.6GB
| Holder | What it keeps | For how long |
|---|---|---|
| Driver | Every build-side row collected from the shuffle files, then the built hash relation | While the relation is built and sent |
| Each executor | A deserialized copy of the relation, shared by all its tasks | Until the broadcast is cleaned up |
| Each task | Its own probe-side partition, streamed past the shared relation | For the life of the task |
A 630MB relation on a 4GB executor sits beside the execution memory its 4 tasks need for sorts and aggregates. Before that, the driver, 1GB by default through spark.driver.memory, has to collect the rows and build the relation. The hard check Spark runs on a built relation rejects only those of 8GB or more, so nothing stops this plan, and the job dies with OutOfMemoryError on the driver or the executors after AQE followed its rule exactly.
Watching a broadcast reach every task
The program below forces a broadcast so you can see the plan's shape: the small table goes whole to every task of the large side, and no row of the large side moves. The board draws titles reaching each task. In memory, the tasks on an executor share a single deserialized copy, so the example cluster holds 40 copies of 630MB, one per executor, rather than 160, one per task. The engine draws the plan as written, before any runtime rewrite.
from pyspark.sql import functions as F
joined = views.join(F.broadcast(titles), "title_id")
joined.collect()| view_id | title_id | minutes |
|---|---|---|
| 1 | 10 | 42 |
| 2 | 11 | 15 |
| 3 | 10 | 88 |
| 4 | 12 | 30 |
| 5 | 11 | 61 |
| 6 | 10 | 12 |
| 7 | 12 | 95 |
| 8 | 11 | 27 |
| 9 | 10 | 54 |
| title_id | genre |
|---|---|
| 10 | drama |
| 11 | comedy |
| 12 | documentary |
Sizing the runtime threshold
Size the adaptive threshold from memory, then divide by compression. Decide how much of each executor's heap a broadcast may take, say 175MB on these 4GB executors, and divide by the ratio you measured: 175MB at 7:1 allows 25MB of shuffle bytes. Set spark.sql.adaptive.autoBroadcastJoinThreshold to that and leave the static threshold alone, since it judges table statistics with errors of their own. For a join that must never broadcast, a merge hint on either side pins a sort-merge join, and AQE honours the hint.
The ratio is row bytes over shuffle bytes. Binary row format stores an 8-byte null bitset, 8 bytes per column, and each string's bytes after that. The cell below adds those up for products as if it were a build side, ignoring the padding of strings to 8-byte words. The 6 * 8 is the 6 columns of products at 8 bytes each, so change the 6 to the column count of whatever side you are sizing. The cell gives the numerator only: its global agg shuffles a single partial row per task, so its own shuffle write says nothing about these rows.
The denominator is the shuffle write of the same rows. In the real job, read it from the stage page of the build side's own stage, the stage AQE measured before converting the join. To measure it in isolation, run the build side through a shuffle with no other work, products.repartition(8).write.format("noop").mode("overwrite").save(), and read that stage's shuffle write. Divide the per-executor budget by the ratio, and set only the adaptive threshold, which reads shuffle bytes at runtime. The static threshold keeps judging table statistics at planning.
A team runs with spark.sql.autoBroadcastJoinThreshold set to 200MB. AQE measures 150MB of shuffle bytes on a join's smaller side, converts the join, and executors fail with OutOfMemoryError. Which change stops this conversion while keeping static broadcasts of small, well-measured tables?
- Set spark.sql.adaptive.autoBroadcastJoinThreshold to a lower value sized from decompressed row bytes
- Set spark.sql.shuffle.partitions lower so the smaller side measures fewer bytes at runtime
- Set spark.sql.adaptive.enabled to false so every join is chosen from planning estimates
- Raise spark.sql.adaptive.skewJoin.skewedPartitionFactor so the smaller side counts as skewed
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.