Narrow vs. wide transformations
What causes a shuffle, and why it is the single most expensive thing Spark does.
The rule
means one input partition maps to exactly one output partition. No row ever needs to leave the partition it started in, so there is no network movement. select, filter, withColumn, union, and mapPartitions are all narrow.
means the output needs data from many input partitions at once. Spark cannot compute a result like that in place, so it has to redistribute rows across the cluster first. That redistribution is the .
Stage boundaries are shuffle boundaries
Spark splits a job into stages at every point where data must move between partitions. A plan with N stages has exactly N-1 shuffles between them.
In the physical plan, every Exchange node is a shuffle. BroadcastExchange is the cheap variant: it copies a small side to every executor instead of redistributing the large side. Run df.explain(True) and grep the output for Exchange to see exactly where shuffles happen in a query.
Wide operations to memorize
groupBy, any join that is not a broadcast join, distinct and dropDuplicates, window functions, orderBy, and all require a shuffle because each of them needs rows with a shared key or ordering to land together.
Why a shuffle costs what it costs
A shuffle is the only point in an otherwise in-memory job where Spark touches disk: each task writes its shuffle output to local disk, and the next stage's tasks read it back over the network.
A shuffle is also a barrier. The next stage cannot start until the slowest map task in the current stage finishes, so one straggler task delays the entire job, not just its own partition.
Finally, a shuffle amplifies skew. Partition assignment is hash(key) % numPartitions, so a single hot key lands entirely in one task no matter how many partitions you configure. More partitions spread cold keys thinner; they do nothing for a key that is already alone in its bucket.
These three costs compound: disk and network I/O, a synchronization barrier, and skew concentration all happen on the same operation.
repartition vs. coalesce
repartition shuffles the data and produces evenly sized partitions. avoids the shuffle by merging adjacent partitions, which means it can leave existing skew in place and it propagates reduced parallelism backwards through the plan.
coalesce(1) on a 1 TB dataset forces the entire upstream chain onto a single task, since every stage feeding that final coalesce now has nowhere to parallelize into.
repartition vs. coalesce
| repartition | coalesce | |
|---|---|---|
| Shuffles data | Yes | No |
| Partition sizes | Even | Uneven, inherits upstream skew |
| Can increase partition count | Yes | No, only decreases |
| Affects upstream parallelism | No | Yes, can collapse it |
Spark UI evidence
Stages tab: count the stages and look for the Exchange nodes in the SQL / DAG view, one per shuffle. Shuffle Read Size and Shuffle Write Size columns show how much data actually crossed the network. Task duration distribution within a stage (the Summary Metrics table) is the fastest way to spot a shuffle that is also skewed.
Video
Worked example
Watch records cross the shuffle boundary
Start with four numbers split across two input partitions, then retain even numbers and sum them by value modulo 2.
- Input partitions
P0 contains [1, 2]; P1 contains [3, 4].
- Narrow filter
P0 keeps [2]; P1 keeps [4]. Neither needs records from the other.
- Wide grouping
Shuffle by value % 2. Both surviving records belong to key 0 and must meet.
- Result
Key 0 has sum 2 + 4 = 6. The grouping introduces a shuffle boundary.
What to notice: Filtering can stay partition-local. Grouping by a key changes which records must be processed together, which is why the plan needs an exchange.
Practice / self-test
1. You raise spark.sql.shuffle.partitions from 200 to 2000 and a stage's runtime doesn't change. What does that suggest?
Reveal answer
The stage is likely skewed rather than under-partitioned. More partitions spread cold keys thinner, but a hot key still lands entirely in one task regardless of the partition count, so the slowest task's duration is unchanged.
2. Why does coalesce(1) hurt performance even before any action that follows it?
Reveal answer
coalesce avoids a shuffle by merging partitions in place, which propagates backwards through the plan: every upstream stage that feeds the coalesce loses parallelism because its output now has to collapse onto a single task.
3. Is orderBy narrow or wide? Why?
Reveal answer
Wide. A global order requires rows to be compared against rows that may live in any other partition, so Spark must range-partition the data via a shuffle before each partition can be sorted locally.
4. A stage has one Exchange node feeding it. How many shuffles occurred to produce its input?
Reveal answer
One. Each Exchange node in the plan corresponds to exactly one shuffle boundary.
5. Name two operations that look wide but are actually narrow under the right conditions.
Reveal answer
A join is narrow when the small side is broadcast instead of shuffled. coalesce, which looks like repartition, is narrow because it only merges adjacent partitions without moving data across the network.
6. Why is BroadcastExchange not considered a "real" shuffle in the cost sense?
Reveal answer
It copies one small side to every executor once; the large side is never redistributed, so there is no per-key network shuffle, no barrier on the large side's partitions, and no skew amplification from hashing a key.