Tech challenge · PySpark

A 50 MB lookup table causes a huge shuffle

EasyPySpark

You join a 2 TB sales DataFrame with a 50 MB stores dimension. The plan shows a sort-merge join that shuffles all 2 TB. How do you avoid the shuffle?

Think it through or sketch an answer first, then open the hint or the solution.

Show a hint

Spark can send a small table to every executor instead of moving the big one.

Show the solution

Broadcast the small side: each executor gets a full copy of stores and joins locally, so the 2 TB side is never shuffled. Spark broadcasts automatically below spark.sql.autoBroadcastJoinThreshold (10 MB by default), so at 50 MB you either raise the threshold or add a broadcast hint. Make sure the table really fits comfortably in executor and driver memory.

Example (Python)

from pyspark.sql.functions import broadcast

result = sales.join(broadcast(stores), 'store_id')

Common mistakes

  • Broadcasting a table of several GB: it is collected to the driver first and can crash it.
  • Trusting the file size on disk: compressed Parquet can be several times larger once in memory.

Learn more

More PySpark challenges

Today's challenge and the full archive