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 partitionseven share per taskhot task, at leasthot task vs even share
2001.5M rows30M rows20x
2,000150K rows30M rows200x
20,00015K rows30M rows2,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()
orders
order_idcustomer_id
1c7
2c7
3c7
4c2
5c7
6c7
7c7
8c3
9c1
customers
customer_idname
c1Ana
c2Ben
c3Chen
c7Dee

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?

Pick the fix from the measurement

What the measurement showsThe fix
The small side fits under the broadcast ceilingBroadcast it, so the big side never shuffles
A handful of hot keys with few partner rowsIsolate them on a broadcast path and union back
Hot keys whose partners are too many to broadcastSalt the join, with N = hot rows / even share
NULL is the hot value in a left joinRoute the null rows around the join
A hot key in a sum or count aggregationOften nothing: partial results shrink it before the shuffle
No hot key, but every task is too bigRaise 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?

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.