Skip to the page

Spark · mid

Joins, broadcast, skew and adaptive execution

Join strategy and key distribution decide most Spark run time. Broadcast the small side, find the hot key when one task is slow, and know what adaptive query execution can and cannot fix.

The notes

What you must know

  • Broadcast join: the small table is collected to the driver and sent to every executor, so the big table is not shuffled. Spark broadcasts automatically when a side is estimated under spark.sql.autoBroadcastJoinThreshold (10 MB by default).
  • Other strategies: shuffle hash join, sort-merge join (default for two big tables with an equality condition), broadcast nested loop and cartesian (non-equality conditions).
  • Broadcast backfires when the table is big: driver out of memory, a copy on every executor, network time, spark.sql.broadcastTimeout (300 s), and a refusal over 8 GB. A hint on a table that grows becomes a future outage.
  • In an outer join only the side whose unmatched rows are not kept can be broadcast: the right side of a left outer join. Prune columns and filter first, then measure.
  • Skew: 199 tasks finish in 2 minutes and one takes 40. Confirm in the stage summary (max versus median time, shuffle read, spill). Find the hot key with groupBy(key).count().orderBy(desc).
  • Typical hot keys: NULL or placeholder values (0, empty, UNKNOWN) and a few giant sellers. NULL keys never match in a join, so split them off, handle them directly and union back.
  • Salting: add a random salt 0 to N-1 on the big side, copy the small side N times, join on key and salt. Salt only known hot keys. Raising shuffle partitions does not help, because one key still lands in one partition.
  • Adaptive query execution (default since Spark 3.2) re-plans after shuffle stages with real sizes: it merges small partitions (64 MB advisory), turns sort-merge into broadcast when a side is small, and splits skewed join partitions (bigger than 5 times the median and 256 MB).
  • AQE cannot fix skew in aggregations, the parallelism of the first scan, small files, missing partition pruning, slow Python UDFs or wrong logic. Dynamic partition pruning is a separate feature.

The red pen

Common traps: mistake → fix

  • Mistake: Broadcast the bigger table to go faster

    Fix: it must fit in memory on the driver and every executor.

  • Mistake: AQE fixes all skew

    Fix: only join skew, not aggregation skew.

  • Mistake: More executors fix one slow task

    Fix: the hot key is still in one task.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is a broadcast join and how does Spark choose it?
  2. One task runs 40 minutes and the rest run 2. What do you do?
  3. What does adaptive query execution change at runtime?
  4. What is salting and when would you use it?
  5. When can forcing a broadcast hurt?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview