The Local Shuffle Read
After a runtime switch, the large side of the join sits in shuffle files, split by hash of the join key across its map tasks' output. A broadcast hash join has no use for that split: every task holds the whole small side, so any slice of the large side can be joined on any executor. The local shuffle read uses that freedom to keep the large side off the network.
- Regular shuffle read
- Task i fetches block i from every map output in the cluster.
- Most of those blocks were written on other executors, so most bytes cross the network.
- The sort-merge join then sorts each task's input on the join key.
- Local shuffle read
- Each task reads blocks written by a single map task.
- Spark prefers to run the task on the executor that wrote them, so the bytes come off local disk.
- The task streams its rows past the broadcast hash table, with no sort.
Which blocks each task reads
Take 4 map tasks and 4 reducers on 2 executors: map tasks M0 and M1 ran on executor A, M2 and M3 on executor B. Each map task wrote 4 blocks, a block per reducer partition.
| Task | Regular read fetches | Local read fetches |
|---|---|---|
| 0 | block 0 from M0, M1 (A) and M2, M3 (B) | blocks 0 to 3 from M0, on A |
| 1 | block 1 from M0, M1 (A) and M2, M3 (B) | blocks 0 to 3 from M1, on A |
| 2 | block 2 from M0, M1 (A) and M2, M3 (B) | blocks 0 to 3 from M2, on B |
| 3 | block 3 from M0, M1 (A) and M2, M3 (B) | blocks 0 to 3 from M3, on B |
In the regular read, whichever executor a task runs on, half its blocks live on the other one: 8 of the 16 fetches cross the network. In the local read, each task takes all 4 blocks of a map output from the disk of the executor that wrote them, so 0 of the 16 cross the network, as long as each task runs there. On a cluster of 50 executors, a regular read pulls about 49 of every 50 bytes from another machine.
Real shuffles have more partitions than this. Spark keeps roughly the parallelism the join would have had: if 20 map tasks wrote a 200-partition shuffle, each map output is cut into 10 contiguous ranges of 20 partitions, giving 200 local-read tasks of 20 blocks each.
After a runtime switch, 25 map tasks wrote the join's large side into a 200-partition shuffle. How does the local read lay out its tasks?
- 25 tasks, each reading all 200 blocks of a single map output.
- 200 tasks, each reading block i from all 25 map outputs.
- 200 tasks, each reading a run of 25 neighbouring blocks from a single map output.
- 8 tasks, each reading 25 neighbouring blocks from every map output.
When Spark keeps the regular read
A local read gives up the hash partitioning of the large side, and sometimes a later step depends on it. Take a join on customer_id followed by groupBy("customer_id"). Under the sort-merge plan, the join's output is already partitioned by customer_id, so the aggregation reuses it with no exchange, as Reusing Shuffle Output showed. A local read would scatter those keys and force a new shuffle. AQE checks for this: when a rewrite would add an exchange the plan did not have, Spark keeps the broadcast join but reads the large side the regular way.
In the SQL tab, a probe side read the local way shows an AQEShuffleRead node labelled local. On the join stage's page, the shuffle read metrics separate remote bytes from the total, and after a local read the remote figure should sit close to 0. The behaviour is controlled by spark.sql.adaptive.localShuffleReader.enabled, which is on by default.
The final plan shows a BroadcastHashJoin whose probe side reads straight from a ShuffleQueryStage, with no AQEShuffleRead local node between them, and the join stage's remote shuffle bytes are close to its total shuffle read. What best explains this?
- A later step depends on the probe side's hash partitioning, so a local read would have added a shuffle.
- The broadcast table travels as shuffle blocks, so the remote bytes are the small side.
- A runtime switch never uses a local read while the static threshold is still 10MB.
- Local reads are reported as remote bytes in the UI whenever the shuffle files are compressed.
About this lesson
This section is part of the Adaptive Query Execution: Intermediate 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.