IntermediateSpark · 14 min · 19 challenges

Joins: Intermediate

At Netflix, a nightly job joins a day of play events to the titles catalog and then to the profiles table. The catalog join finishes in minutes; the profiles join runs for an hour. An engineer raises the broadcast threshold so profiles broadcast too, and the driver dies before a single row is joined. All 3 outcomes come down to which join strategy Spark picked, and why.

The Four Join Strategies

Daily Life
Interviews

Every join you write with .join() is a logical join: it says which rows pair up. Catalyst turns it into 1 of 4 physical join strategies, and the strategy decides how many shuffles the join costs, how much memory it needs, and whether it runs at all. The beginner lesson showed 2 of them working, the broadcast join and the shuffled join. This section names all 4 and the conditions Spark checks, in order, to pick one.

The 4 strategies

StrategyPlan nodeWhen Spark picks itWhat it costs
Broadcast hash joinBroadcastHashJoinAn equality key, and one side's estimated size is under `spark.sql.autoBroadcastJoinThreshold`Ships the small side to every executor once; the big side never moves
Sort-merge joinSortMergeJoinAn equality key, neither side small enough to broadcastShuffles both sides on the key, sorts each partition, merges; 2 shuffles
Shuffle hash joinShuffledHashJoinAn equality key, one side far smaller but over the threshold, and a `shuffle_hash` hint or `spark.sql.join.preferSortMergeJoin` set to falseShuffles both sides, hashes the smaller side per partition, no sort
Broadcast nested loop joinBroadcastNestedLoopJoinNo equality key (a range or inequality condition), and one side is under the thresholdBroadcasts one side, then compares every row of the other against all of it

The planner asks 3 questions in a fixed order. First, does the condition contain at least one equality between a column on each side? orders.product_id == products.product_id does; events.ts.between(sessions.start, sessions.end) does not. Without an equality there is no key to hash or sort on, so only the nested loop family remains: broadcast nested loop if one side fits under the threshold. When neither side fits, an inner join falls to a full cartesian product, while an outer join with no equality key still runs as broadcast nested loop and broadcasts a side that is over the threshold. Second, with an equality key, is either side's estimated size under the threshold? If so, broadcast hash join, with no shuffle on either side. Third, if both sides are over the threshold, the default is sort-merge join; shuffle hash join runs only when you ask for it with a hint or turn the sort-merge preference off.

The cartesian product is the fifth outcome rather than a fifth strategy: it is what remains when no key exists and nothing fits in memory, and it pairs every row with every row.

What the choice costs

Put sizes on it. A fact table of 400GB joins a dimension of 8MB on a cluster of 200 executors. Broadcast hash join reads the dimension once, sends 8MB to each executor, 1.6GB of network in total, and every task joins its own partition of the fact table where it sits: 1 stage and no shuffle write. Sort-merge join on the same pair shuffles both sides: 400GB written to local disk and read back across the network, plus a sort of every partition, and 3 stages where there was 1. Spark's part of the work is seeing that the dimension is small, and the next section covers what happens when it cannot.

You can override the choice per join with a hint on either DataFrame. F.broadcast(products) and products.hint("broadcast") are the same request; merge asks for sort-merge join and shuffle_hash for shuffle hash join. On a join with no equality key, the broadcast hint is what produces a broadcast nested loop join, and shuffle_replicate_nl asks for the cartesian product. A hint that can't be honoured is dropped: a merge hint on a join with no equality key does nothing, because there is no key to merge on.

enriched = order_items.join(F.broadcast(products), "product_id")
merged = orders.join(order_items.hint("merge"), "order_id")
hashed = orders.join(order_items.hint("shuffle_hash"), "order_id")

Run the cell below. The hint keeps order_items in place and ships products to each task. The rows that come back are the same as without the hint; what changed is the plan underneath, and section 3 shows how to see that.

units = (order_items
    .join(F.broadcast(products), "product_id")
    .groupBy("product_id")
    .agg(F.max("product_name").alias("product_name"),
         F.sum("quantity").alias("units"))
    .orderBy(F.desc("units"), "product_id"))
units.show(5)
The threshold check is absolute. Spark compares each side's estimated bytes against a fixed number, 10MB unless you change it, and never against the other side. A 20MB table joined to a 2TB table fails the check exactly as a 20MB table joined to a 30MB table does, so a lopsided pair on its own never moves the planner off sort-merge.
Multiple Choice

orders is a 300GB fact table and stores is a 40MB table. orders.join(stores, "store_id") runs with default settings and no hints. Which strategy does Spark pick?

The 10MB Broadcast Threshold

Daily Life
Interviews

The threshold is spark.sql.autoBroadcastJoinThreshold, and its default is 10485760 bytes, 10MB. Spark compares it against a number it computes before reading a single row: the planner's size estimate for each side of the join, in bytes. Whether that estimate is right decides whether a 2MB dimension gets broadcast or shuffled.

Where the estimate comes from

For a table read from files, the estimate starts as the sum of the file sizes in the directory listing: the compressed bytes on disk, which Spark gets from the storage system without opening a file. Each operator above the scan adjusts that figure by a fixed rule, and the rules are blunt. The table below is the whole set the planner uses when table statistics are absent, which they are by default.
Operator above the scanEstimate it reportsWhy that is often wrong
`select` of fewer columnsChild size scaled by the width of the kept columnsColumn widths are type defaults, not measured
`filter`The child's size, unchangedA filter that keeps 1% of rows still reports 100%
`join`Left size times right sizeTreats every row as matching every row
`groupBy` with keysThe child's size, unchangedCollapsing 1B rows to 500 groups reports 1B rows' worth of bytes
`groupBy` with no keys1 rowThe only operator whose estimate shrinks

Read the filter row twice, because it is the one that bites. A products table of 300MB on disk, filtered to the 2MB of rows where active == 1, is still 300MB to the planner. That is 30 times over the threshold, so Spark picks sort-merge join and shuffles a 400GB fact table to meet a 2MB dimension. Nothing in the logs says the estimate was wrong; the job takes an hour instead of 4 minutes.

Multiple Choice

dim is 60MB on disk. fact.join(dim.filter(F.col("region") == "EU"), "id") runs with the default threshold, no statistics and no hint. The EU rows total 3MB. Which strategy runs?

Telling Spark what you know

When you know the side is small and the planner does not, wrap it in F.broadcast(). The hint is a promise, and Spark takes it over its own estimate, up to the hard caps in the next section. Apply the filter first and broadcast the filtered DataFrame. On an inner join the optimizer would push a later filter on products columns below the broadcast anyway, so this order does not change the plan; it makes the code show which rows travel to every executor.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
active = products.filter(F.col("active") == 1)
orders.join(F.broadcast(active), "product_id").collect()
orders6 rows
order_id
product_id
1
4017
2
2210
3
4017
4
3305
5
5120
6
2210
products4 rows
product_id
product_name
active
4017
Desk lamp
1
2210
Notebook
1
3305
Headphones
1
5120
Fax machine
0
Preparing the animation

The animation sets the threshold to -1 first. That value turns automatic broadcasting off entirely, which is how you make a plan take the sort-merge path in a test; with it off, only the hint can produce a broadcast. products has 4 rows and the filter drops the 1 inactive row, Fax machine, before the 3 survivors are copied to each of the 2 tasks reading orders. The 6 order rows stay in their 2 input partitions through the whole join; no row of orders moves.

TIP
A broadcast hint travels with the DataFrame. Filter after the hint and Spark broadcasts whatever the hinted side has become at join time, so hint early if you like, but the bytes shipped are the bytes left after the last filter above the join.

The other fix is to give the planner real numbers. Computing statistics for the table with an ANALYZE TABLE statement stores row counts and per-column histograms in the catalog, and with spark.sql.cbo.enabled on, the cost-based optimizer uses them to estimate how many rows a filter keeps. That makes the estimate right for every query on the table; the hint makes it right for 1 join. Statistics suit dimensions many jobs read; the hint suits a one-off.

Try it on the practice tables. products is filtered to in-stock rows before the hint, so only those rows travel; the aggregate afterwards groups on the join key.

in_stock = products.filter(F.col("in_stock") == 1)
stocked = (order_items
    .join(F.broadcast(in_stock), "product_id")
    .groupBy("product_id")
    .agg(F.sum("quantity").alias("units"))
    .orderBy("product_id"))
stocked.show(5)

The Real Broadcast Ceiling

Daily Life
Interviews
The config takes any number of bytes, so nothing stops you setting it to 5GB and letting every join broadcast. The cost of a broadcast doesn't scale the way the threshold suggests, and 3 fixed limits sit in the way before the config matters.
8GB
hard cap on one broadcast table
512M rows
hard cap on build-side rows
300s
default spark.sql.broadcastTimeout

The first 2 are refusals: Spark throws an error naming the cap and the job dies at the moment the broadcast is built. The third is a clock: spark.sql.broadcastTimeout is how long the driver waits for the build side to be collected and shipped, and a broadcast that overruns it fails the job. None of those is the practical ceiling. The practical ceiling comes from what a broadcast makes the cluster do, and it lands at 1 to 2GB on disk.

What a broadcast makes the cluster do

The driver collects every row of the build side into its own heap and builds a hash table from them.
The driver collects every row of the build side into its own heap and builds a hash table from them.
Each executor pulls a full copy of that hash table over the network and keeps it in memory for the whole job.
Each executor pulls a full copy of that hash table over the network and keeps it in memory for the whole job.
Every task on an executor probes the same copy, which is never split, so more executors never shrink it.
Every task on an executor probes the same copy, which is never split, so more executors never shrink it.
Build side on diskHash table in memory (about 4x)Driver heap it needsNetwork with 100 executors
10MB40MB40MB4GB
500MB2GB2GB200GB
2GB8GB8GB800GB
The second column is the one the threshold never mentions. Parquet stores columns compressed; the hash table holds every row decompressed, each with its own header, plus an index over the keys, and that comes to roughly 4 times the disk size. A 500MB dimension becomes a 2GB structure that the driver must hold while building it, and that each executor must hold next to the memory its tasks need for their own work. With 100 executors it also crosses the network 100 times. At 2GB on disk the driver needs 8GB free, every executor needs 8GB free, and 800GB moves before the first row is joined, where a sort-merge join shuffles the 2GB dimension plus the fact table once.
✓Broadcast a 1GB dimension
  • Driver builds a 4GB hash table before anything ships
  • Every executor holds 4GB for the life of the job
  • 100 executors pull 400GB over the network
  • The fact table never shuffles: 1 stage
✗Sort-merge the same dimension
  • Driver holds nothing; executors do all the work
  • A task holds 1 partition of each side, not the table
  • 1GB plus the fact table cross the network once
  • Both sides shuffle and sort: 3 stages
The crossover follows from the comparison. A broadcast pays off while executor count times hash-table size stays well under the bytes a shuffle would move, and while each executor has the headroom to hold the table beside its tasks. Past 1 to 2GB on disk, the hash table alone is a quarter of a 16GB executor, and the driver becomes the single machine that must hold the whole thing first. That is why production configs keep the threshold in the tens or low hundreds of megabytes and reserve hints for sides under around 1GB, whatever the config would accept.
The driver is the part people forget. In client mode it is the laptop or edge node that submitted the job, with a default heap of 1GB; a broadcast the executors could hold still dies there with an out-of-memory error, because the driver gathers the build side before anything is sent. The error names the driver, not the join, which is why a failed broadcast is often filed as a driver sizing problem.
Multiple Choice

An engineer sets spark.sql.autoBroadcastJoinThreshold to 4GB so that a 3GB customers table broadcasts in a join to a 1TB fact table. The driver runs with its default memory. What fails first?

Verifying with explain()

Daily Life
Interviews

You've added a hint, or raised the threshold, and the job still takes an hour. The next step is to read the plan rather than guess. df.explain() prints the physical plan Catalyst settled on, and the join node in it names the strategy and the side that was built. The rule from the Catalyst lessons, counting Exchange nodes to count shuffles, applies to joins the same way.

joined = orders.join(products, "product_id")
joined.explain()

The same 2 lines print different plans depending on what the planner believes about products. Below are the 2 plans side by side, with the null filters Spark adds under an inner join left out to keep the lines short.

products estimated at 8MB
Physical plan line
BroadcastHashJoin [product_id#3], [product_id#12], Inner, BuildRight
:- FileScan parquet orders[order_id#2,product_id#3]
+- BroadcastExchange HashedRelationBroadcastMode(...)
+- FileScan parquet products[product_id#12,product_name#13]
products estimated at 50MB
Physical plan line
SortMergeJoin [product_id#3], [product_id#12], Inner
:- Sort [product_id#3 ASC NULLS FIRST], false, 0
: +- Exchange hashpartitioning(product_id#3, 200)
: +- FileScan parquet orders[order_id#2,product_id#3]
+- Sort [product_id#12 ASC NULLS FIRST], false, 0
+- Exchange hashpartitioning(product_id#12, 200)
+- FileScan parquet products[product_id#12,product_name#13]

Read the join line first. BroadcastHashJoin ends with BuildRight: the right side of the join, the second DataFrame in orders.join(products), is the one turned into a hash table and shipped. Under it, the BroadcastExchange node sits on the branch that was broadcast, and the other branch runs from FileScan straight into the join with no Exchange at all: orders never moved. The right-hand plan has no build side to name. Both branches carry an Exchange hashpartitioning(..., 200), 1 shuffle each into 200 reducer partitions, and a Sort above each exchange, which is the merge's precondition.

What each join node tells you
  • ▸BroadcastHashJoin ... BuildLeft or BuildRight: strategy 1; the named side is the one broadcast, and a BroadcastExchange sits under it
  • ▸SortMergeJoin: strategy 2; expect an Exchange and a Sort on both branches, 2 shuffles
  • ▸ShuffledHashJoin ... BuildLeft or BuildRight: strategy 3; an Exchange on both branches, no Sort, the named side is hashed per partition
  • ▸BroadcastNestedLoopJoin BuildRight, Inner: strategy 4; a BroadcastExchange on the named side and a condition with no equality in it
  • ▸CartesianProduct: the fallback; no key and nothing small enough to broadcast

The plan also predicts the stage count you will see once the job runs. Each Exchange ends a stage, so the left plan is 1 stage for the join itself plus a small separate job that collects products for the broadcast, and the right plan is 3 stages: 1 per scan feeding an exchange, and 1 that reads both exchanges and merges. If a job that should broadcast shows 3 stages for the join, the plan is where the mismatch shows first, before any stage has run.

Which side can be built

For an inner join Spark may build either side and picks the smaller estimate, so BuildLeft shows up whenever the first DataFrame is the small one. An outer join removes the choice. The side a join preserves must be the streamed side, because the hash table only holds rows that get probed, so a left join can only build the right side and a right join only the left; a full outer join cannot be a broadcast hash join at all. small.join(big, "id", "left") therefore runs as sort-merge even with a hint on small, and the plan tells you so with a SortMergeJoin line ending in LeftOuter.

Multiple Choice

customers is 4MB and orders is 200GB. The plan for the join below shows SortMergeJoin [customer_id#5], [customer_id#20], LeftOuter, even though customers carries a broadcast hint. Why did Spark not broadcast customers?

result = F.broadcast(customers).join(orders, "customer_id", "left")
TIP
explain("formatted") prints the same tree with each node numbered and a details block under it, including the estimated size each side was planned with. When a dimension you expected to broadcast did not, that size is the first number to read.

Ordering a Multi-Table Join

Daily Life
Interviews

A join with 3 or more tables runs as a chain of 2-table joins, in the order you wrote them. Catalyst can reorder joins only when the cost-based optimizer is on and spark.sql.cbo.joinReorder.enabled is set, and both are off by default, so for most jobs the order in the code is the order Spark runs. Each link in the chain is 1 of the 4 strategies from section 0, and each one's cost depends on the size of its inputs, which is where ordering matters: the output of one join is the input to the next.

A worked chain

Take 3 tables. orders is 2 billion rows and 400GB. customers is 50 million rows and 5GB, past the broadcast ceiling. products is 200,000 rows and 20MB, and the job wants only the Toys category: 5% of products, which is 1MB, and 3% of orders. The same 3 tables can be joined in 2 orders.

StepOrder A: customers firstOrder B: toys first
Join 1`orders` to `customers`: sort-merge, shuffles 400GB + 5GB, output 2B rows`orders` to broadcast(`toys`): no shuffle, output 60M rows, 12GB
Join 22B rows to broadcast(`toys`): no shuffle, output 60M rows12GB to `customers`: sort-merge, shuffles 12GB + 5GB
Shuffle total405GB17GB
Rows that are joined and then discarded1.94B0
Both orders produce the same 60 million rows. Order B moves 24 times fewer bytes because the only filter in the job, Toys, is applied by the join that can be broadcast, and that join runs before the join that cannot. The rule generalises: run first the join that removes the most rows per byte it moves, and that is nearly always a broadcast join against a filtered dimension, because it shrinks the fact table with no shuffle at all.

The ordering rules

Order a chain by these, top first
  • Filter each table before it joins. Spark pushes a filter on one table's columns below an inner join anyway, but the fact table shrinks only at the join to that table.
  • Join the filtered, broadcastable dimensions first; each one shrinks the fact table without a shuffle.
  • Put the sort-merge joins last, when the fact side is as small as it is going to get.
  • Keep joins on the same key adjacent: the second reuses the first's hash partitioning and skips a shuffle.
  • Join 2 fact tables to each other only after both have been filtered and reduced by their dimensions.

Rule 4 comes from the shuffle lessons. orders.join(order_items, "order_id").join(shipments, "order_id") shuffles orders and order_items on order_id for the first join, and the result is already partitioned on order_id, so the second join shuffles only shipments. Put a sort-merge join on product_id between them and the order_id partitioning is gone; the third join shuffles the whole intermediate result again. A broadcast join on product_id in the same place keeps it, because the fact side never moves, so the third join still shuffles only shipments. The same reuse applies to a groupBy on the join key straight after the join.

(orders
.join(F.broadcast(customers), "customer_id")
.join(F.broadcast(products), "product_id")
.collect())
orders6 rows
order_id
customer_id
product_id
1
c1
11
2
c2
12
3
c1
13
4
c3
11
5
c2
13
6
c3
12
customers3 rows
customer_id
segment
c1
Retail
c2
Wholesale
c3
Retail
products3 rows
product_id
category
11
Kitchen
12
Garden
13
Toys
Preparing the animation

The animation chains 2 broadcast joins. orders, 6 rows in 2 partitions, picks up segment from customers and then category from products; both dimensions have 3 rows and both arrive at every task whole. Watch the orders partitions: through 2 joins they never leave the task that read them, which is the shape to aim for in a dimension-heavy chain, every dimension broadcast and the fact table staying put or shrinking.

The cell below applies rules 1 and 2 to the practice tables. The filter on status sits on orders before anything joins it, and products arrives by broadcast. The practice tables are all far under 10MB, so Spark broadcasts the order_items join as well, and neither join shuffles. The groupBy on order_id then adds its own Exchange, because no earlier shuffle left the rows partitioned on that key, and the orderBy adds a range shuffle after it. With a production-sized order_items, the first join would be a sort-merge on order_id, and the groupBy would reuse that partitioning with no shuffle of its own.

completed = orders.filter(F.col("status") == "Completed")
revenue = (completed
    .join(order_items, "order_id")
    .join(F.broadcast(products), "product_id")
    .groupBy("order_id")
    .agg(F.sum(F.col("quantity") * F.col("unit_price")).alias("revenue"))
    .orderBy(F.desc("revenue"), "order_id"))
revenue.show(5)
✓Do
  • Write the filter on each table above its join, so the join sees fewer rows
  • Join filtered dimensions by broadcast before any sort-merge join
  • Group consecutive joins on one key so the partitioning carries over
  • Confirm the order with explain() and count the Exchange nodes
✗Don't
  • Join the 2 largest tables before a dimension join has cut either one down
  • Rely on the optimizer to reorder joins; join reordering is off by default
  • Interleave a sort-merge join on another key between 2 joins on the same key
  • Expect a filter comparing columns of both sides to shrink the join beneath it
Multiple Choice

events is 1TB with user_id and device_id. users is 30GB with a country_code. devices is 3MB, and the job keeps 1 device type, 2% of devices. countries is 50KB and joins only through users.country_code. Which order moves the least data?

❯❯❯PUTTING IT ALL TOGETHER

> An Uber team builds a daily table of trips enriched with city, driver and promotion details. Trips is 3 billion rows, drivers is 6GB, cities is 2MB, and promotions is 400MB on disk but the job wants only the 50 promotions active that day. The first version joins trips to drivers first and runs for 2 hours; the plan shows 2 SortMergeJoin nodes and 4 Exchange nodes, one of the joins being promotions.

The promotions join went sort-merge because the filter to active promotions left the planner's estimate at 400MB, 40 times over the threshold; F.broadcast() on the filtered DataFrame fixes that one join.
Drivers at 6GB stays sort-merge on purpose: decompressed into roughly 24GB per executor and collected on the driver first, it is past the ceiling no config can move.
Reordering the chain to trips, broadcast(cities), broadcast(active promotions), then drivers lets the 2 broadcast joins shrink trips before the only shuffle, and the plan afterwards shows 1 SortMergeJoin with 2 Exchange nodes.
The BuildRight on each BroadcastHashJoin line confirms that cities and promotions, written as the right side of each .join(), were the sides shipped.
KEY TAKEAWAYS
Spark picks 1 of 4 physical strategies by 3 ordered checks: an equality key, a side under the threshold, then the sort-merge default.
The 10MB threshold is compared against a planner estimate that a filter never shrinks; F.broadcast() overrides the estimate.
A broadcast is built on the driver and copied whole to every executor at around 4 times its disk size, so 1 to 2GB is the real ceiling.
explain() names the strategy in the join node, and BuildLeft or BuildRight names the side that was broadcast.
Joins run in the order written; put filtered, broadcastable dimensions first and sort-merge joins last.

Four physical join strategies, one 10MB line, and a plan you can read to prove which one ran.

Category
Spark
Difficulty
intermediate
Duration
14 minutes
Challenges
19 hands-on challenges

Topics covered: The Four Join Strategies, The 10MB Broadcast Threshold, The Real Broadcast Ceiling, Verifying with explain(), Ordering a Multi-Table Join

Lesson Sections

  1. The Four Join Strategies

    The 4 strategies The cartesian product is the fifth outcome rather than a fifth strategy: it is what remains when no key exists and nothing fits in memory, and it pairs every row with every row. What the choice costs Put sizes on it. A fact table of 400GB joins a dimension of 8MB on a cluster of 200 executors. Broadcast hash join reads the dimension once, sends 8MB to each executor, 1.6GB of network in total, and every task joins its own partition of the fact table where it sits: 1 stage and no

  2. The 10MB Broadcast Threshold (concepts: Broadcast Join)

    Where the estimate comes from For a table read from files, the estimate starts as the sum of the file sizes in the directory listing: the compressed bytes on disk, which Spark gets from the storage system without opening a file. Each operator above the scan adjusts that figure by a fixed rule, and the rules are blunt. The table below is the whole set the planner uses when table statistics are absent, which they are by default. Telling Spark what you know

  3. The Real Broadcast Ceiling (concepts: Broadcast Join)

    The config takes any number of bytes, so nothing stops you setting it to 5GB and letting every join broadcast. The cost of a broadcast doesn't scale the way the threshold suggests, and 3 fixed limits sit in the way before the config matters. What a broadcast makes the cluster do The second column is the one the threshold never mentions. Parquet stores columns compressed; the hash table holds every row decompressed, each with its own header, plus an index over the keys, and that comes to roughly

  4. Verifying with explain() (concepts: Broadcast Join)

    Which side can be built

  5. Ordering a Multi-Table Join

    A worked chain Both orders produce the same 60 million rows. Order B moves 24 times fewer bytes because the only filter in the job, Toys, is applied by the join that can be broadcast, and that join runs before the join that cannot. The rule generalises: run first the join that removes the most rows per byte it moves, and that is nearly always a broadcast join against a filtered dimension, because it shrinks the fact table with no shuffle at all. The ordering rules