Joins, skew & salting

Broadcast vs sort-merge joins, detecting skew from straggler tasks, salting hot keys, AQE skew-join handling. The single most asked Spark debugging scenario.

About 27 min read3-question knowledge check

The mental model

A distributed join is **a meeting where everyone with the same badge number must gather at the same table**. To join two datasets on user_id, every row with user_id = 5 — from *both* sides — has to end up on the same machine, because that's the only way to match them. Spark achieves this by hashing the join key and sending equal keys to the same partition. The whole cost and pain of joins comes from that gathering: it's a shuffle, and if one badge number is worn by 20% of the crowd, one table is mobbed while the others sit empty. That mob is skew, and it is the single most-asked Spark debugging scenario.

The analogy that sticks: a wedding seating chart. Sort-merge join is the default plan for two big crowds — sort everyone by table number, walk both lines in lockstep, seat matches. It works at any size but requires shuffling *both* crowds. Broadcast join is what you do when one side is tiny: photocopy the small guest list, hand a copy to every table, and let each table match locally — no shuffling the big crowd at all. The art of Spark joins is (1) picking broadcast whenever one side fits in memory, and (2) when both sides are big and one key is a mob, breaking that mob into smaller groups so no single table is overwhelmed.

Two facts drive every decision:

  • Broadcast eliminates a shuffle; sort-merge pays for two. If either side of the join fits in executor memory (default threshold 10 MB, tunable), Spark can broadcast it and skip shuffling the large side entirely. This is the first thing to check on any slow join.
  • Skew turns a parallel job serial. A stage finishes only when its slowest task finishes. If one key holds 20% of the rows, its partition is one giant task while 199 others idle — so a 200-way-parallel job runs at the speed of one machine processing a fifth of the data. No amount of adding executors fixes it, because the work isn't spread.

The one idea interviewers probe for: you diagnose skew from the Spark UI's task-duration distribution before you touch any config. A straggler task with far more shuffle-input than the median is the evidence; the fix (broadcast, salt, or AQE) follows from what the numbers show, not from a guess.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision