Apache Spark · Intermediate
What is a broadcast join in Spark, and when should you use it?
By Anurag Pandey, Lead Data Engineer ·
The short answer
A broadcast join sends a full copy of the small table to every executor, so the large table is joined where it sits without being shuffled. Spark does it automatically below spark.sql.autoBroadcastJoinThreshold, 10 MB by default.
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.
Say it like this
About 55 seconds out loud. Use it as a model, then put it in your own words.
When I join a big table with a small one, like events with a country lookup, Spark doesn't have to shuffle both sides. It can broadcast the small table: send a full copy to every executor. Then each task joins its partition of the big table locally, with no shuffle of the big table at all. Spark does this automatically when it estimates one side is below spark.sql.autoBroadcastJoinThreshold, which is 10 megabytes by default, and I can ask for it with the broadcast hint. With Adaptive Query Execution, Spark can also switch a planned sort-merge join to a broadcast join at runtime, once it sees the real size. The risk is broadcasting something too big: it has to fit in memory on the driver and every executor, or the job fails with out-of-memory errors.
What interviewers look for
- You explain that the large table is not shuffled.
- You know the threshold setting and the broadcast hint.
- You name the memory risk of broadcasting a table that is too large.
Common mistakes
- Saying both tables are broadcast. Only the small side is.
- Raising the threshold very high without thinking about memory.
- Not knowing AQE can convert joins to broadcast at runtime.
Follow-up questions to expect
- 1How do you request or disable a broadcast join?
- 2What happens if you broadcast a table that is too large?
- 3What join does Spark use when neither side is small?
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.