The 10MB Broadcast Threshold
Concepts covered: Broadcast Join
Question 2 depends on one setting, spark.sql.autoBroadcastJoinThreshold. The default, 10485760 bytes, is 10MB. Setting it to -1 turns automatic broadcasting off. At planning time Spark compares each buildable side's estimated size with this number and broadcasts a side at or under it without being asked. The estimate is made before any data is read, so a wrong estimate means a wrong decision.
Where the estimate comes from
Spark starts at each scan with a size in bytes and carries it up the plan, changing it only where a rule says to. With cost-based optimization off, unless a row says otherwise:
| Plan node | Estimated size in bytes |
|---|---|
| Scan of Parquet or other files | Total size of the files on disk |
| Catalog table with computed statistics | The size stored by ANALYZE TABLE ... COMPUTE STATISTICS |
| Projection to fewer columns | The input's estimate, scaled down by the narrower row width |
| Filter | The input's estimate, unchanged |
| Filter, with cost-based optimization and column statistics | The input's estimate, scaled by the predicted fraction of rows kept |
The plain filter row is the one that slows real jobs. A 4GB drivers table filtered to one city's active drivers might keep 2MB of rows, but its estimate stays at 4GB, reduced at most for the columns dropped. Spark sees a side far over 10MB, plans a sort-merge join, and shuffles every trip to meet 2MB of drivers. The error runs the other way too: a heavily compressed file gives a small estimate for rows that are much larger once decoded, which the next section returns to.
A Parquet campaigns table is 6GB on disk. A job filters it to 3MB of active campaigns and inner-joins it to a 500GB clicks table. Cost-based optimization is off and the threshold is the default. What does Spark plan?
- A broadcast hash join, since the 3MB of filtered rows is under the 10MB threshold
- A broadcast hash join, since Spark first reads the filtered side once to measure it
- A shuffle hash join, since the filtered campaigns are far smaller than the clicks
- A sort-merge join, since the filter leaves the campaigns estimate at about 6GB
Overruling the estimate with broadcast()
from pyspark.sql import functions as F
trips = spark.read.parquet("/warehouse/trips")
drivers = spark.read.parquet("/warehouse/drivers")
city_drivers = drivers.filter(
(F.col("city_id") == 17) & (F.col("status") == "active")
)
trips_with_drivers = trips.join(F.broadcast(city_drivers), "driver_id")F.broadcast adds a hint to the plan. It does not read the data or check its size. A hint outranks the threshold, so it still applies when automatic broadcasting is set to -1, and city_drivers.hint("broadcast") has the same effect.
The animation sets the threshold to -1 to stand in for an estimate too large to qualify, so the hint is the only reason active gets broadcast. Watch the 3 active products reach both orders tasks while no order row moves. Product 5120 is the inactive fax machine, so its order finds no match and drops out.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
active = products.filter(F.col("active") == 1)
orders.join(F.broadcast(active), "product_id").collect()| order_id | product_id |
|---|---|
| 1 | 4017 |
| 2 | 2210 |
| 3 | 4017 |
| 4 | 3305 |
| 5 | 5120 |
| 6 | 2210 |
| product_id | product_name | active |
|---|---|---|
| 4017 | Desk lamp | 1 |
| 2210 | Notebook | 1 |
| 3305 | Headphones | 1 |
| 5120 | Fax machine | 0 |
Raise the threshold or hint the join
- Raising the threshold
- Applies to every join in every query the session runs
- Trusts each estimate, including the ones that are too small
- Lets a side grow past the size you tested with no change in code
- Hinting one join
- Applies only to the DataFrame you wrapped
- Overrides an estimate you have checked and found too large
- Keeps broadcasting after that side grows, until someone removes it
Spark can also refuse a hint. If the join type cannot build the side you marked, as with the left side of a left outer join, Spark drops the hint with a warning and plans the join without it. The code still reads F.broadcast, so the plan is where you confirm what happened.
Run the cell below. It broadcasts the in-stock products and totals units sold per category. Then change F.col("in_stock") to ~F.col("in_stock") and run it again: the join and the hint stay the same while the rows on the broadcast side change. On seed tables this small, Spark would broadcast products even without the hint.
The hint guarantees a broadcast. It says nothing about whether broadcasting is wise at the size you aimed it at, and that limit is the next section's subject.
About this lesson
This section is part of the Joins: 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.