Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 73/ 160
04. Big Data Processing & Apache Spark

Join Strategies in Spark (Broadcast Hash Join, Sort-Merge Join, Shuffle Hash Join)

Pick broadcast, sort-merge, or shuffle hash joins based on table sizes and skew risk.

AdvancedBroadcast · Sort-Merge · Shuffle Hash3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Exact bytes, distinct keys, and null/hot share. Next, broadcast, sort-merge, or bounded shuffle hash. Finally, physical plan, shuffle read, memory, and task p99.

Think of it like…

Broadcast gives every worker a small lookup dictionary, sort-merge arranges both warehouses by aisle code, and shuffle hash moves one side into compact local lockers if the lockers fit.

Where you’ve seen it

A 20 MB country dimension is broadcast with broadcast(dim); two 500 GB fact tables use sort-merge on order_id; a hot NULL customer key is filtered/salted before either strategy is considered.

Words you’ll hear

Broadcast
A strategy that sends one small side fully to executors and probes it without shuffling the large side by join key.
Sort-Merge
A strategy that shuffles both inputs, sorts compatible keys, and merges ranges, well suited for large equi-joins.
Shuffle Hash
A shuffled build/probe join whose output partition for the build side must fit in execution memory.
Operational boundary
Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory. Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

Visual diagram

Loading interactive diagram

Theory & deep dive

Broadcast: the working mechanism

Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it.

Exact bytes, distinct keys, and null/hot share. Broadcast, sort-merge, or bounded shuffle hash, then pre-aggregate, split, or salt key; this exposes the state governed by Shuffle Hash.

Join Strategies in Spark in one concrete design

A 20 MB country dimension is broadcast with broadcast(dim); two 500 GB fact tables use sort-merge on order_id; a hot NULL customer key is filtered/salted before either strategy is considered.

Key takeaway: Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it

Join Strategies in Spark: failure mode and production response

Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.

Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan. Track Broadcast threshold as "default spark.sql.autoBroadcastJoinThreshold is 10 MiB", and make Skew hot-key share visible to the dataset owner.

Key takeaway: Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

How it works, step by step

Step 1 of 4
Broadcast

1. Estimate sides

Exact bytes, distinct keys, and null/hot share. Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it.

exact bytes, distinct keys, and null/hot share
Key numbers at this step
Broadcast threshold
default `spark.sql.autoBroadcastJoinThreshold` is 10 MiB
Sort-Merge
A strategy that shuffles both inputs, sorts compatible keys, and merges ranges, well suited for large equi-joins.

When to use it & trade-offs

What it is

Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it. A 20 MB country dimension is broadcast with broadcast(dim); two 500 GB fact tables use sort-merge on order_id; a hot NULL customer key is filtered/salted before either strategy is considered.

Why it matters: Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory. Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

When to reach for it

  • Pick broadcast, sort-merge, or shuffle hash joins based on table sizes and skew risk.
  • Choose Broadcast because it provides this capability: A strategy that sends one small side fully to executors and probes it without shuffling the large side by join key.
  • Keep evidence for Broadcast threshold at "default spark.sql.autoBroadcastJoinThreshold is 10 MiB" so the last production stage-Validate - physical plan, shuffle read, memory, and task p99-remains testable.

When to avoid or be careful

  • Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.
  • Do not conflate Sort-Merge with Broadcast. The second concept contributes different behavior: A strategy that shuffles both inputs, sorts compatible keys, and merges ranges, well suited for large equi-joins.
  • Release only after this guardrail is in place: Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

Broadcast / Sort-Merge / Shuffle Hash - roles, limits, and operating cost

Broadcast
Best for: A strategy that sends one small side fully to executors and probes it without shuffling the large side by join key.

Strengths: A strategy that sends one small side fully to executors and probes it without shuffling the large side by join key. Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it.

Watch out: Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.

Sort-Merge
Best for: A strategy that shuffles both inputs, sorts compatible keys, and merges ranges, well suited for large equi-joins.

Strengths: A strategy that shuffles both inputs, sorts compatible keys, and merges ranges, well suited for large equi-joins. In this workflow it supports: Handle skew - pre-aggregate, split, or salt key.

Watch out: The operating cost is real: key percentage and bytes, not only distinct-row count.

Shuffle Hash
Best for: A shuffled build/probe join whose output partition for the build side must fit in execution memory.

Strengths: A shuffled build/probe join whose output partition for the build side must fit in execution memory. Its output can be checked with Join output grain.

Watch out: Operating guardrail: Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Broadcast, and how would Broadcast threshold reveal it?”
  • “Why is Sort-Merge distinct from this behavior: a shuffled build/probe join whose output partition for the build side must fit in execution memory?”
  • Production clue: Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.
  • Design probe: Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

Benchmarks & rules of thumb

Broadcast threshold -
default `spark.sql.autoBroadcastJoinThreshold` is 10 MiB
Skew hot-key share -
key percentage and bytes, not only distinct-row count
Join output grain -
expected rows must be measured before/after the join

Common mistakes & follow-ups

Common interview mistakes

  • Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.
  • Reporting success from a job exit code without checking Broadcast threshold and Skew hot-key share.
  • Treating this example as complete without replay, rollback, or access boundaries: A 20 MB country dimension is broadcast with broadcast(dim); two 500 GB fact tables use sort-merge on order_id; a hot NULL customer key is filtered/salted before either strategy is considered.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A strategy that sends one small side fully to executors and probes it without shuffling the large side by join key.
  • What does the recovery plan do when this occurs? Forcing a large broadcast OOMs every executor, skew creates one massive sort-merge partition, and shuffle-hash is unsafe when the chosen build side cannot fit its per-key partition in memory.
  • When is Broadcast insufficient for Skew hot-key share, and what does adding Sort-Merge cost in Broadcast threshold?
Interview tip: Lead with the data path and failure boundary: Spark can broadcast a small relation into a hash map on executors, sort-merge large join partners by key into ranges, or build shuffle-hash structures when memory and configuration support it. Then make the answer operational: Check statistics and both sides' compressed bytes, rely on AQE or an explicit hint only with a memory bound, salt or pre-aggregate skewed hot keys, and verify the physical strategy and spill in the Spark plan.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Join Strategies in Spark (Broadcast Hash Join, Sort-Merge Join, Shuffle Hash Join) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Partitioning (repartition vs coalesce) & Bucketing Strategies
Next topic
Solving Data Skew (Salting Keys, Adaptive Query Execution / AQE)