EXECUTION & PERFORMANCEPySpark

Broadcast Joins, Shared Variables & Bucketing

Distributed data processing with Spark — pick a topic on the left and its full breakdown loads here: the execution model, worked jobs and diagrams, performance and shuffle behavior, and the habits that keep Spark jobs fast, correct, and affordable.

18 min readTopics chapter readerLevel · Performance
01 · Orientation

What You'll Master Here

If a table is small, broadcast it (copy to all). If you reuse the same data per task, broadcast it as a variable. If you join the same key repeatedly, bucket it once on disk. Each avoids moving big data.

4 min · Topic 1 of 5

This chapter is a toolkit of shuffle-avoiding and data-sharing techniques. The headline act is the broadcast join: when one table is small, ship it to every executor so the big table can be joined locally, no shuffle at all. It is the single most effective join optimisation in Spark.

Alongside it, two shared-variable mechanisms, broadcast variables (read-only data sent once to every executor) and accumulators (write-only counters aggregated on the driver), and bucketing, which pre-shuffles data on write so repeated joins on the same key skip the shuffle entirely.

With diagrams of broadcast-vs-shuffle joins and the bucketing layout, plus runnable examples. These are the techniques that separate a job that shuffles needlessly from one that does not.

Core mental model

If a table is small, broadcast it (copy to all). If you reuse the same data per task, broadcast it as a variable. If you join the same key repeatedly, bucket it once on disk. Each avoids moving big data.

Why it matters

Broadcast joins and bucketing remove shuffles, the dominant cost (Chapter 17). Broadcast variables and accumulators are the correct way to share data and collect metrics across executors. All four come up constantly in real tuning and interviews.

broadcast join
Joining by copying the small table to every executor, avoiding a shuffle.
broadcast variable
A read-only value shipped once to each executor and reused by its tasks.
accumulator
A write-only counter tasks add to, aggregated on the driver.
bucketing
Pre-partitioning data by a key on write so later joins avoid shuffling.
Common mistake

Broadcasting a table that is too large. Every executor must hold a full copy; an oversized broadcast causes driver/executor OOM, broadcast only small tables.

Better habit

Broadcast the small side of a large–small join.

Use broadcast variables for lookup data reused across tasks.

Bucket tables joined repeatedly on the same key.

The big idea

Moving big data is expensive; moving small data (or moving it once) is cheap. Broadcast joins, broadcast variables, and bucketing all replace a big shuffle with a small copy or a one-time layout.

Remember this

Broadcast joins copy a small table to all executors to avoid a shuffle; broadcast variables share read-only data; accumulators collect metrics; bucketing pre-shuffles on disk. All four trade a big shuffle for something cheap.

Practice2 prompts
  1. When does a broadcast join avoid a shuffle, and what is the risk?
  2. What is the difference between a broadcast variable and an accumulator?