<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Shuffle</id>
	<title>Shuffle - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Shuffle"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Shuffle&amp;action=history"/>
	<updated>2026-12-06T00:08:03Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Shuffle&amp;diff=74&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@9783fe6</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Shuffle&amp;diff=74&amp;oldid=prev"/>
		<updated>2026-11-22T21:29:56Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@9783fe6&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Shuffle&lt;br /&gt;
| also_known_as   = Exchange (Trino; the &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; operator in Spark plans)&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Map side (shuffle write), shuffle files, reduce side (shuffle fetch), stage boundary&lt;br /&gt;
| implementations = [[Apache Spark]], [[Trino]], [[Apache Flink]]&lt;br /&gt;
| related         = [[Executor]], [[Partition]], [[Small files problem]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-22|the v4.3.0 tag of apache/spark (docs/rdd-programming-guide.md, docs/sql-performance-tuning.md, docs/configuration.md, docs/job-scheduling.md, core DAGScheduler.scala), an EXPLAIN run on a Spark 4.3.1-SNAPSHOT build, the Trino 484 docs and the Apache Flink 2.3 docs}}&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;shuffle&amp;#039;&amp;#039;&amp;#039; is the step in a distributed query engine that redistributes data across the cluster so that it is grouped differently across [[partition]]s, for example so that all rows with the same key end up in the same task.&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#shuffle-operations |title=RDD Programming Guide: Shuffle operations (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; The term is most used in [[Apache Spark]], where a shuffle copies data between [[executor]]s 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.&lt;br /&gt;
&lt;br /&gt;
== Why a shuffle happens ==&lt;br /&gt;
A Spark task works on one partition at a time. Operations such as &amp;lt;code&amp;gt;groupBy&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;reduceByKey&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Operations whose output partition depends on many input partitions are often called &amp;#039;&amp;#039;&amp;#039;wide&amp;#039;&amp;#039;&amp;#039; transformations, and they cause a shuffle. Operations such as &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt; have &amp;#039;&amp;#039;&amp;#039;narrow&amp;#039;&amp;#039;&amp;#039; dependencies: each output partition depends on one input partition, so Spark can run them together in the same task.&amp;lt;ref name=&amp;quot;dag&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala |title=DAGScheduler.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; The Spark documentation lists repartition operations, most &amp;lt;code&amp;gt;ByKey&amp;lt;/code&amp;gt; operations and joins as operations that can cause a shuffle.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; A broadcast join avoids a shuffle by sending the small side to every executor; Spark plans one automatically when a table is smaller than &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default 10 MB).&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stages and shuffle files ==&lt;br /&gt;
Spark builds stages by cutting the job&amp;#039;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.&amp;lt;ref name=&amp;quot;dag&amp;quot; /&amp;gt; The writing side is called the map side and the reading side the reduce side, names taken from MapReduce rather than from Spark&amp;#039;s own &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;reduce&amp;lt;/code&amp;gt; functions.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; The files go to the directories in &amp;lt;code&amp;gt;spark.local.dir&amp;lt;/code&amp;gt; (default &amp;lt;code&amp;gt;/tmp&amp;lt;/code&amp;gt;) and are compressed by default (&amp;lt;code&amp;gt;spark.shuffle.compress&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; 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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a DataFrame or SQL plan, a shuffle shows up as an &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; operator. The plan below, from &amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt; with adaptive query execution turned off, aggregates each partition first (&amp;lt;code&amp;gt;partial_sum&amp;lt;/code&amp;gt;), shuffles by the grouping key into 200 partitions, and then finishes the aggregation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
*(2) HashAggregate(keys=[code#14], functions=[sum(amount#13)])&lt;br /&gt;
+- Exchange hashpartitioning(code#14, 200), ENSURE_REQUIREMENTS, [plan_id=35]&lt;br /&gt;
   +- *(1) HashAggregate(keys=[code#14], functions=[partial_sum(amount#13)])&lt;br /&gt;
      +- *(1) Project [amount#13, code#14]&lt;br /&gt;
         +- *(1) ColumnarToRow&lt;br /&gt;
            +- FileScan parquet [amount#13,code#14,order_date#15] ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The 200 comes from &amp;lt;code&amp;gt;spark.sql.shuffle.partitions&amp;lt;/code&amp;gt;, whose default is 200.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Spill ==&lt;br /&gt;
Shuffle operations build in-memory structures to organize records before or after the transfer. When these do not fit in the [[executor]]&amp;#039;s execution memory, Spark spills them to disk, which adds disk I/O and garbage collection.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; Spilled data is compressed by default (&amp;lt;code&amp;gt;spark.shuffle.spill.compress&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
== Adaptive query execution ==&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Coalescing partitions.&amp;#039;&amp;#039;&amp;#039; After a shuffle, AQE merges small contiguous shuffle partitions so that a query does not run many tiny tasks. The target size is &amp;lt;code&amp;gt;spark.sql.adaptive.advisoryPartitionSizeInBytes&amp;lt;/code&amp;gt; (default 64 MB), but by default (&amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.parallelismFirst&amp;lt;/code&amp;gt; is true) Spark favors parallelism and only enforces a minimum partition size of 1 MB.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Skew join handling.&amp;#039;&amp;#039;&amp;#039; A shuffle partition is treated as skewed when it is larger than 5 times the median partition size and larger than 256 MB (&amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.skewedPartitionFactor&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes&amp;lt;/code&amp;gt;). AQE splits such partitions in sort-merge joins into smaller tasks, replicating the other side where needed.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Join conversion.&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; before the join.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== External shuffle service and push-based shuffle ==&lt;br /&gt;
By default, an executor serves its own shuffle files to other executors. The &amp;#039;&amp;#039;&amp;#039;external shuffle service&amp;#039;&amp;#039;&amp;#039; 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 &amp;lt;code&amp;gt;spark.shuffle.service.enabled&amp;lt;/code&amp;gt; (default false) and matters most for dynamic allocation.&amp;lt;ref name=&amp;quot;jobsched&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/job-scheduling.html |title=Job Scheduling (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt; Spark on Kubernetes does not support it; there, shuffle tracking or decommissioning keeps shuffle data available instead.&amp;lt;ref name=&amp;quot;jobsched&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Push-based shuffle&amp;#039;&amp;#039;&amp;#039;, 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 (&amp;lt;code&amp;gt;spark.shuffle.push.enabled&amp;lt;/code&amp;gt;) and is supported only on YARN with the external shuffle service.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison: how engines exchange data between stages ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Apache Spark !! Trino !! Apache Flink&lt;br /&gt;
|-&lt;br /&gt;
! Name&lt;br /&gt;
| Shuffle (&amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; in plans)&lt;br /&gt;
| Exchange&amp;lt;ref name=&amp;quot;trino&amp;quot;&amp;gt;{{Cite web |url=https://trino.io/docs/current/overview/concepts.html |title=Trino concepts (Trino 484) |publisher=Trino Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Shuffle (pipelined or blocking network exchange)&amp;lt;ref name=&amp;quot;flinkshuffle&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-stable/docs/ops/batch/batch_shuffle/ |title=Batch Shuffle (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! How data moves&lt;br /&gt;
| Map tasks write sorted files to local disk; the next stage fetches them after the map stage is done&amp;lt;ref name=&amp;quot;dag&amp;quot; /&amp;gt;&lt;br /&gt;
| Tasks write into output buffers; tasks of the next stage read them through an exchange client&amp;lt;ref name=&amp;quot;trino&amp;quot; /&amp;gt;&lt;br /&gt;
| Streaming: pipelined exchange between running tasks. Batch: blocking shuffle (data is persisted and read after it is fully produced) or experimental hybrid shuffle&amp;lt;ref name=&amp;quot;flinkshuffle&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Failure handling&lt;br /&gt;
| Lost shuffle output makes Spark rerun the stages that produced it&amp;lt;ref name=&amp;quot;dag&amp;quot; /&amp;gt;&lt;br /&gt;
| By default a node failure fails the query; with &amp;lt;code&amp;gt;retry-policy=TASK&amp;lt;/code&amp;gt;, intermediate data is spooled through an exchange manager to storage such as S3 or HDFS&amp;lt;ref name=&amp;quot;trinofte&amp;quot;&amp;gt;{{Cite web |url=https://trino.io/docs/current/admin/fault-tolerant-execution.html |title=Fault-tolerant execution (Trino 484) |publisher=Trino Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Batch blocking shuffle keeps persisted results that consumers read later; hybrid shuffle can store data on remote storage&amp;lt;ref name=&amp;quot;flinkshuffle&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Executor]]&lt;br /&gt;
* [[Partition]]&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
* [[Small files problem]]&lt;br /&gt;
* [[Trino]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>