Narrow and wide transformations
| Also known as | Narrow and shuffle dependencies |
|---|---|
| Field | Distributed data processing |
| Key parts | Narrow dependency, shuffle (wide) dependency, Exchange operator, stage boundary
|
| Open source implementations | Apache Spark |
| Related | Shuffle, Job, stage and task, Directed acyclic graph (Spark), Partition, Broadcast join |
In Apache Spark, a narrow transformation is one where each output partition depends on a single input partition, so it runs inside the same task as the step before it; a wide transformation needs data from many input partitions and therefore causes a shuffle and a new stage.[1][2] The distinction decides how Spark splits a job into stages. Narrow steps are cheap and are pipelined together; wide steps write shuffle files to disk and send them over the network, which makes them the main cost in most jobs.
How do narrow and wide transformations differ?
Spark's scheduler states the rule directly. RDD operations with narrow dependencies, like map() and filter(), are pipelined into one set of tasks in each stage, while operations with shuffle dependencies need two stages: one to write a set of map output files and another to read them after a barrier.[1]
A wide transformation exists because a task works on one partition at a time. For reduceByKey, the values of one key are usually spread over many partitions and machines, so Spark must read from all partitions and bring each key's values together. This all-to-all step is the shuffle; it involves disk I/O, data serialization and network I/O.[2]
| Narrow transformation | Wide transformation | |
|---|---|---|
| Input partitions per output partition | One | Many |
| Data movement | None; the task reads its own partition | A shuffle across executors |
| Effect on stages | Pipelined into the current stage | Ends a stage and starts a new one[1] |
| In a DataFrame physical plan | No Exchange node
|
An Exchange node (or a BroadcastExchange for a broadcast join)
|
| Number of output partitions | Same as the input (or fewer, for coalesce)
|
For DataFrame and SQL, spark.sql.shuffle.partitions (default 200) unless set explicitly or changed by Adaptive Query Execution[3]
|
Which operators shuffle?
The Spark documentation lists repartition operations (such as repartition and coalesce), most ByKey operations except counting (such as groupByKey and reduceByKey) and join operations (such as cogroup and join) as operations that can cause a shuffle.[2] "Can" matters: in the DataFrame plans below, coalesce to fewer partitions merges partitions without an Exchange, and a join does not shuffle again an input that is already partitioned by its keys.
The following table shows what Spark 4.3.0 actually planned for common DataFrame operations. Each row is the physical plan of the operation on a 100-row DataFrame in PySpark 4.3.0, with Adaptive Query Execution and automatic broadcast joins turned off so that the base plan is visible.
| DataFrame operation | Narrow or wide | Exchange in the physical plan
|
|---|---|---|
filter, where |
Narrow | None |
select, withColumn |
Narrow | None |
explode |
Narrow | None |
union |
Narrow | None |
sample |
Narrow | None |
coalesce(n) |
Narrow | None |
sortWithinPartitions |
Narrow | None |
limit(n) alone |
Narrow | None |
limit(n) followed by groupBy |
Wide | Exchange SinglePartition
|
groupBy(...).agg(...), count per key |
Wide | Exchange hashpartitioning(key, 200)
|
distinct, dropDuplicates |
Wide | Exchange hashpartitioning(...)
|
orderBy, sort |
Wide | Exchange rangepartitioning(... ASC NULLS FIRST, 200)
|
repartition(4) |
Wide | Exchange RoundRobinPartitioning(4)
|
repartition("col") |
Wide | Exchange hashpartitioning(col, 200), REPARTITION_BY_COL
|
join (sort-merge) |
Wide | One Exchange hashpartitioning per side
|
Window with partitionBy |
Wide | Exchange hashpartitioning(partition key, 200)
|
intersect |
Wide | Exchange hashpartitioning on all columns
|
A broadcast join replaces the two shuffles of a sort-merge join with a BroadcastExchange of the small side. Spark plans one automatically when a side's estimated size is below spark.sql.autoBroadcastJoinThreshold (default 10 MB).[3]
Spark avoids shuffles it does not need
Spark adds an Exchange only when an operator's input is not already distributed the way the operator requires. The planner rule that does this is EnsureRequirements, which is why shuffles it adds are labeled ENSURE_REQUIREMENTS in the plan.[4] In the PySpark 4.3.0 plan below, a groupBy("g") is followed by a join on the same key. The aggregate's output is already hash-partitioned by g, so the join reuses it and only the other side is shuffled:
*(5) SortMergeJoin [g#5L], [g#7L], Inner
:- *(2) Sort [g#5L ASC NULLS FIRST], false, 0
: +- *(2) HashAggregate(keys=[g#5L], functions=[count(1)])
: +- Exchange hashpartitioning(g#5L, 200), ENSURE_REQUIREMENTS
: +- *(1) HashAggregate(keys=[g#5L], functions=[partial_count(1)])
: ...
+- *(4) Sort [g#7L ASC NULLS FIRST], false, 0
+- Exchange hashpartitioning(g#7L, 200), ENSURE_REQUIREMENTS
...
Tables stored with a matching layout can avoid the shuffle entirely. Storage-partitioned join uses the partitioning reported by a compatible V2 data source to skip the shuffle phase of a join (spark.sql.sources.v2.bucketing.enabled, default true, since 3.3.0).[3] Apache Iceberg supports it through spark.sql.iceberg.planning.preserve-data-grouping (default false), which groups scan tasks for the same partition into the same read split.[5]
How to reduce the cost of wide transformations
- Filter and select columns before a wide step, so that less data is shuffled. The Catalyst optimizer pushes many filters down automatically (see Predicate pushdown).
- Use a broadcast join when one side is small.
- Prefer aggregations that combine on the map side.
reduceByKeyandaggregateByKeybuild in-memory structures on the map side; in the DataFrame API, thepartial_countandpartial_sumsteps in the plan show the same map-side combine.[2] - Let Adaptive Query Execution coalesce small shuffle partitions and split skewed ones.[3]
See also
References
- ↑ 1.0 1.1 1.2 DAGScheduler.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 2.3 RDD Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 3.2 3.3 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ EnsureRequirements.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Configuration (Apache Iceberg 1.12.0 documentation). Apache Software Foundation.Retrieved 2026-11-23.