Why More Partitions Won't Help
Concepts covered: Data Skew
Raising spark.sql.shuffle.partitions is the right fix when every task is too big because the data is big. It is the wrong fix for a hot key, and the reason is a single line of arithmetic. Hash partitioning places a row in partition pmod(hash(key), P). A key has a single hash, so for every P it maps to a single partition, and every row carrying it goes there. Raising P divides the other keys more finely and leaves the hot key whole.
| shuffle partitions | even share per task | hot task, at least | hot task vs even share |
|---|---|---|---|
| 200 | 1.5M rows | 30M rows | 20x |
| 2,000 | 150K rows | 30M rows | 200x |
| 20,000 | 15K rows | 30M rows | 2,000x |
A stage ends when its slowest task ends. At 200 partitions the hot task holds the 30M rows of acct_1001 plus its share of the other 270M, around 1.35M more. At 2,000 partitions that share falls to about 135K, so the straggler loses roughly 4% of its rows while the stage now schedules 10 times as many tasks. The stage barely speeds up.
The animation replays section 1's 9 orders without the salt, at 6 shuffle partitions, twice the 3 used there. The 6 orders for c7 still meet on a single task. The other 3 orders land wherever their keys hash, and at least 2 of the 6 tasks receive nothing at all.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
spark.conf.set("spark.sql.shuffle.partitions", 6)
orders.join(customers, "customer_id").collect()| order_id | customer_id |
|---|---|
| 1 | c7 |
| 2 | c7 |
| 3 | c7 |
| 4 | c2 |
| 5 | c7 |
| 6 | c7 |
| 7 | c7 |
| 8 | c3 |
| 9 | c1 |
| customer_id | name |
|---|---|
| c1 | Ana |
| c2 | Ben |
| c3 | Chen |
| c7 | Dee |
A sort-merge join's stage runs 41 minutes, 40 of them spent waiting on a single task. A teammate raises spark.sql.shuffle.partitions from 200 to 1,000, and the stage now runs 40 minutes. What explains the result?
- The slow task holds a hot key, which hashes to a single partition at any count, so only its neighbours moved
- The extra partitions added scheduling overhead that cancelled out the split of the hot key's rows
- The setting changes only groupBy stages, so the join kept hashing into its old 200 partitions
- The hot key split into 5 pieces, and each piece still runs longer than the remaining tasks
Pick the fix from the measurement
| What the measurement shows | The fix |
|---|---|
| The small side fits under the broadcast ceiling | Broadcast it, so the big side never shuffles |
| A handful of hot keys with few partner rows | Isolate them on a broadcast path and union back |
| Hot keys whose partners are too many to broadcast | Salt the join, with N = hot rows / even share |
| NULL is the hot value in a left join | Route the null rows around the join |
| A hot key in a sum or count aggregation | Often nothing: partial results shrink it before the shuffle |
| No hot key, but every task is too big | Raise spark.sql.shuffle.partitions |
A groupBy with sum or count combines before the shuffle, so the hot key's reducer receives at most a partial result from each map task: 30M rows read by 200 map tasks arrive as no more than 200 partials. Skew bites the aggregations whose partial result doesn't shrink, such as collect_list, which ships every value it gathers.
A left join of events (2B rows, 1,000 shuffle partitions) to a 20GB devices table is stuck on a straggler. The per-key count of device_id returns NULL at 300M rows and dev_0, a placeholder id, at 40M rows; every other value is under 2M. Which plan fixes it without changing the output?
- Salt the whole join with N = 150, because NULL holds 150 times the even share
- Raise the partition count to 10,000, so the even share falls to 200K rows a task
- Send the null rows around the join, isolate dev_0 on a broadcast path, and union the 3 parts
- Switch to an inner join to drop the nulls, then broadcast the 20GB devices table
The capstone puts sections 0 to 2 together on the practice data. It measures the 2 heaviest product_id values in transactions, sends their rows down a broadcast path, joins the rest with a sort-merge join, and counts the rows each path produced. Broadcasting is switched off so the rest path really shuffles, and the F.broadcast hint overrides that setting on the hot path. On a table this small, hot only means heaviest, but the plan is the one you would ship.
About this lesson
This section is part of the Data Skew: 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.