The Broadcast Storm
A broadcast hash join drops the shuffle of the big side and sets up a different kind of traffic. The driver runs a separate job to collect the small side, builds a hash table from its rows, and ships that table to every executor. Torrent broadcast cuts it into 4MB blocks (spark.broadcast.blockSize) that executors fetch from the driver and from each other, which spreads the sending load. Each executor still receives the full table once, and all of its tasks share that copy.
Bytes on the wire
One broadcast therefore puts about table size times executor count on the network. A shuffle-based join moves each side once: big side plus small side. Add executors and the first number grows while the second stays put. Here are lookup tables from a trip pricing job on 400 executors, each joined to 60GB of trips.
| Lookup table | Size | Broadcast to 400 executors | Shuffle both sides |
|---|---|---|---|
| `cities` | 2MB | 0.8GB | 60GB |
| `promos` | 120MB | 48GB | 60.1GB |
| `drivers` | 600MB | 240GB | 60.6GB |
| `riders` | 900MB | 360GB | 60.9GB |
Setting the 2 costs equal gives the break-even size: 60GB divided by 399, about 150MB. Below it, broadcasting a table moves fewer bytes than shuffling the trips; above it, more. Network is one axis. The shuffle also writes the trips to disk and sorts them, so a broadcast a little past break-even can still finish first. At 6 times past it, riders sends about 300GB more than the shuffle would.
A threshold raised to rescue one join
A storm usually starts with a threshold raised to rescue one join. Someone sets autoBroadcastJoinThreshold to 1GB so riders broadcasts, and every lookup table under 1GB broadcasts along with it. The pricing job joins trips to 8 of them: riders 900MB, drivers 600MB, payment_methods 400MB, vehicles 250MB, promos and fare_rules 120MB each, zones 8MB and cities 2MB, 2.4GB in all. Each executor holds all 8 hash tables at once, and the driver collects and builds each one before sending it.
- 960GB
- 860GB
Those bytes repeat. Each action runs the plan again, broadcasts included, so a job that writes the result and then counts it pays twice. A backfill loop that runs the join once per hour pays once per hour. Because the cost is per executor, the broadcast phase also gets longer every time the cluster grows, which is the opposite of what more executors do for a shuffle.
Recognising a storm
- explain() shows a BroadcastExchange per table, and a short job per broadcast runs before the main job
- The query fails with Could not execute broadcast in 300 secs, the default spark.sql.broadcastTimeout
- The driver runs out of memory while collecting a small side, before any big-side task starts
- Executors fail with out-of-memory errors as the join stage starts, holding every broadcast table at once
- The broadcast phase slows down each time executors are added
- Do
- Price a broadcast as table size times executor count and compare it with the shuffle it replaces
- Filter and select columns on a lookup table before the join so fewer bytes are copied
- Hint broadcast on tables under the break-even size and keep the threshold at 10MB
- Recount broadcast bytes whenever the executor count changes
- Avoid
- Raise autoBroadcastJoinThreshold for the whole job to rescue one slow join
- Raise spark.sql.broadcastTimeout to hide a broadcast that is too large
- Run a broadcast join inside a loop of actions without counting the repeats
- Assume a table's size on disk equals the memory its hash table takes
A job joins a 250MB vehicles table to 40GB of ratings on 300 executors. Counting network bytes only, which strategy moves less?
- Broadcast: 250MB is sent once and shared, so about 250MB crosses the network.
- Sort-merge: about 40.25GB moves, against about 75GB for the broadcast.
- Broadcast: about 75GB, against 80.5GB, since sort-merge moves each side twice.
- Neither: torrent broadcast sends each 4MB block once, so both move about 40GB.
On the practice tables, stores is small enough to broadcast. Run the cell, then change the hint to "merge" and run it again; the rows match. The plan is what changes. With broadcast, explain() shows a BroadcastExchange over the stores scan and no Exchange between the store_sales_raw scan and the join, so the network carries stores once per executor. With merge, an Exchange hashpartitioning(store_id, 200) sits under each side, and every sales row crosses the network once. On 400 executors, the first plan sends 400 copies of stores and the second sends the sales table once.
About this lesson
This section is part of the Joins: 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.