Shuffle
| Also known as | Exchange (Trino; the Exchange operator in Spark plans)
|
|---|---|
| Field | Distributed data processing |
| Key parts | Map side (shuffle write), shuffle files, reduce side (shuffle fetch), stage boundary |
| Open source implementations | Apache Spark, Trino, Apache Flink |
| Related | Executor, Partition, Small files problem |
A shuffle is the step in a distributed query engine that redistributes data across the cluster so that it is grouped differently across partitions, for example so that all rows with the same key end up in the same task.[1] The term is most used in Apache Spark, where a shuffle copies data between executors and machines and is one of the most costly operations in a job. Other engines do the same work under other names: Trino calls it an exchange, and Apache Flink distinguishes pipelined shuffles for streaming jobs from blocking shuffles for batch jobs.
Why a shuffle happens
A Spark task works on one partition at a time. Operations such as groupBy, reduceByKey or a join need all the values for a key in one place, but those values are usually spread over many partitions and machines. Spark must read from all partitions and bring the values for each key together; this all-to-all step is the shuffle.[1]
Operations whose output partition depends on many input partitions are often called wide transformations, and they cause a shuffle. Operations such as map and filter have narrow dependencies: each output partition depends on one input partition, so Spark can run them together in the same task.[2] The Spark documentation lists repartition operations, most ByKey operations and joins as operations that can cause a shuffle.[1] A broadcast join avoids a shuffle by sending the small side to every executor; Spark plans one automatically when a table is smaller than spark.sql.autoBroadcastJoinThreshold (default 10 MB).[3]
Stages and shuffle files
Spark builds stages by cutting the job's graph at shuffle boundaries. One stage writes a set of map output files; the next stage reads those files after all of them are written.[2] The writing side is called the map side and the reading side the reduce side, names taken from MapReduce rather than from Spark's own map and reduce functions.[1]
On the map side, each task keeps its output in memory until it no longer fits, then sorts it by target partition and writes it to a single file. Reduce tasks then read the sorted blocks that belong to them.[1] The files go to the directories in spark.local.dir (default /tmp) and are compressed by default (spark.shuffle.compress).[4] Spark keeps shuffle files until the data that uses them is garbage collected, so that lost work can be recomputed without writing them again; long-running applications can therefore use a lot of disk.[1]
In a DataFrame or SQL plan, a shuffle shows up as an Exchange operator. The plan below, from EXPLAIN on a GROUP BY with adaptive query execution turned off, aggregates each partition first (partial_sum), shuffles by the grouping key into 200 partitions, and then finishes the aggregation:
*(2) HashAggregate(keys=[code#14], functions=[sum(amount#13)])
+- Exchange hashpartitioning(code#14, 200), ENSURE_REQUIREMENTS, [plan_id=35]
+- *(1) HashAggregate(keys=[code#14], functions=[partial_sum(amount#13)])
+- *(1) Project [amount#13, code#14]
+- *(1) ColumnarToRow
+- FileScan parquet [amount#13,code#14,order_date#15] ...
The 200 comes from spark.sql.shuffle.partitions, whose default is 200.[3]
Spill
Shuffle operations build in-memory structures to organize records before or after the transfer. When these do not fit in the executor's execution memory, Spark spills them to disk, which adds disk I/O and garbage collection.[1] Spilled data is compressed by default (spark.shuffle.spill.compress).[4] Spill is a different thing from shuffle files: shuffle files are the normal output of the map side, while spill files are temporary overflow inside one task.
Adaptive query execution
Adaptive query execution (AQE) re-plans a query while it runs, using statistics from completed shuffle stages. It is on by default since Spark 3.2.0.[3]
- Coalescing partitions. After a shuffle, AQE merges small contiguous shuffle partitions so that a query does not run many tiny tasks. The target size is
spark.sql.adaptive.advisoryPartitionSizeInBytes(default 64 MB), but by default (spark.sql.adaptive.coalescePartitions.parallelismFirstis true) Spark favors parallelism and only enforces a minimum partition size of 1 MB.[3] - Skew join handling. A shuffle partition is treated as skewed when it is larger than 5 times the median partition size and larger than 256 MB (
spark.sql.adaptive.skewJoin.skewedPartitionFactorandspark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes). AQE splits such partitions in sort-merge joins into smaller tasks, replicating the other side where needed.[3] - Join conversion. When runtime statistics show that one side of a sort-merge join is small, AQE can switch to a broadcast hash join and read the shuffle files locally.[3]
A storage partition join avoids the shuffle altogether when both sides of a join come from a V2 data source, such as Apache Iceberg, that reports a compatible partitioning; the plan then has no Exchange before the join.[3]
External shuffle service and push-based shuffle
By default, an executor serves its own shuffle files to other executors. The external shuffle service is a long-running process on each node that serves shuffle files instead, so they stay available after the executor that wrote them is removed. It is enabled with spark.shuffle.service.enabled (default false) and matters most for dynamic allocation.[5][4] Spark on Kubernetes does not support it; there, shuffle tracking or decommissioning keeps shuffle data available instead.[5]
Push-based shuffle, added in Spark 3.2.0, has map tasks push their blocks to external shuffle services, which merge them per shuffle partition. Reduce tasks then read a mix of merged partitions and original blocks, which turns many small random disk reads into large sequential reads. It is off by default (spark.shuffle.push.enabled) and is supported only on YARN with the external shuffle service.[4]
Comparison: how engines exchange data between stages
| Apache Spark | Trino | Apache Flink | |
|---|---|---|---|
| Name | Shuffle (Exchange in plans)
|
Exchange[6] | Shuffle (pipelined or blocking network exchange)[7] |
| How data moves | Map tasks write sorted files to local disk; the next stage fetches them after the map stage is done[2] | Tasks write into output buffers; tasks of the next stage read them through an exchange client[6] | Streaming: pipelined exchange between running tasks. Batch: blocking shuffle (data is persisted and read after it is fully produced) or experimental hybrid shuffle[7] |
| Failure handling | Lost shuffle output makes Spark rerun the stages that produced it[2] | By default a node failure fails the query; with retry-policy=TASK, intermediate data is spooled through an exchange manager to storage such as S3 or HDFS[8]
|
Batch blocking shuffle keeps persisted results that consumers read later; hybrid shuffle can store data on remote storage[7] |
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 RDD Programming Guide: Shuffle operations (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 2.0 2.1 2.2 2.3 DAGScheduler.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 3.0 3.1 3.2 3.3 3.4 3.5 3.6 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 4.0 4.1 4.2 4.3 Spark Configuration (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 5.0 5.1 Job Scheduling (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 6.0 6.1 Trino concepts (Trino 484). Trino Software Foundation.Retrieved 2026-11-22.
- ↑ 7.0 7.1 7.2 Batch Shuffle (Apache Flink 2.3). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Fault-tolerant execution (Trino 484). Trino Software Foundation.Retrieved 2026-11-22.