Shuffle Operations

Most slow-job questions end at a shuffle, because a shuffle holds rows in memory while it writes them to disk and sends them across the network, and no other step does all of that at once. A strong answer explains the mechanics, sizes the cost from the numbers in the job at hand, and points at the shuffle that can be removed.

What a shuffle does, step by step

StepSideWhat happens
1MapEach task hashes the grouping key of every row and assigns the row to 1 of 200 target partitions.
2MapIt sorts rows by target partition and writes 1 shuffle file to the executor's local disk, plus an index of where each partition starts.
3MapIt reports the file's location to the driver. The next stage starts only after every map task has reported.
4ReduceEach of the 200 reduce tasks fetches its slice from every map output over the network.
5ReduceIt deserializes and merges the slices, then aggregates or joins, spilling to disk if the rows exceed its memory.

Every shuffled byte is serialized and written to local disk, then sent over the network, where the reading task loads it back and deserializes it. The cost scales with bytes, and the bytes can be estimated. The nightly job filters to plays and keeps 4 columns, which shrinks 1.28 TB of input to 640 GB that must be regrouped by profile and title within each country. Spark also sums minutes inside each map task before writing, which shrinks the data when a task holds many rows for the same key; a profile rarely repeats the same title many times inside a single 128 MB file, so here that step saves little.

Step 3 creates a barrier. The 200 reduce tasks wait for the slowest of the 10,000 map tasks, so a single straggling scan task delays everything after it. Shuffle files also outlive the tasks that wrote them on purpose: they stay on local disk so a failed reduce task can fetch them again without rerunning the map side.

Broadcast joins remove the big shuffle

The genre report from the previous section joins the filtered events to titles, and a shuffle join moves both tables so that equal title_id values meet in the same task. When a side is small, a cheaper plan exists. The driver collects the small table and ships a full copy to every executor, and each scan task joins its slice of the big table against that local copy as it reads. The big table never leaves the task that read it, so the join becomes a narrow step inside the scan stage.

Spark picks a broadcast join on its own when it estimates a side to be below spark.sql.autoBroadcastJoinThreshold, 10 MB by default. The titles table is 40 MB, so the planner falls back to a sort-merge join, the default shuffle join, unless you raise the threshold or mark the table with a broadcast hint. The trade is lopsided here: 20 copies of 40 MB put 800 MB on the network, against 640 GB for the shuffle.

from pyspark.sql.functions import broadcast

enriched = watched.join(broadcast(titles), "title_id")

Other shuffles worth removing

Adaptive query execution, on by default since version 3.2, measures shuffle outputs at runtime. It can switch a sort-merge join to a broadcast join once a side proves small, and it merges small shuffle partitions. Both happen after shuffle files were written, so a broadcast planned up front still saves the most work.

About this lesson

This section is part of the Distributed Compute: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.