Which Operations Skew Hurts

Concepts covered: Data Skew

A hot key does damage only at a step that sends rows to partitions by that key. For any line of code, ask whether the step shuffles by the key, and whether every row of the key travels to the receiving task or only a summary of those rows. The table sorts the common operations by those answers.

operationshuffles by the key?effect of a hot key
`filter`, `select`, `withColumn`nonone: each task keeps its own input partition
join to a table under 10MB (broadcast)no, the large side stays putnone
`count()` on the whole DataFrameno key involvednone
`groupBy(k)` with `sum`, `count`, `avg`yessoftened: a summary per input partition travels
`groupBy(k)` with `collect_list`yesfull: every value reaches the hot task
join of 2 large tables (sort-merge)yes, both sidesfull, plus extra output rows when keys repeat
window with `partitionBy(k)`yesfull: every row reaches the hot task

Untouched: steps with no key shuffle

Narrow steps like filter and withColumn run on each input partition where it sits, so key frequencies never come into play. A broadcast join is narrow on its large side: every executor receives a full copy of the small table, and each task joins the large-side rows it already holds. Here is the orders and customers join from the first section without the conf line. Deleting that line does not undo a setting that already ran: the threshold is back at its 10MB default only in a fresh session or after spark.conf.unset("spark.sql.autoBroadcastJoinThreshold"). At the default, the customers table is far under the threshold, so Spark broadcasts it.

orders.join(customers, "customer_id").collect()
orders
order_idcustomer_id
1c7
2c7
3c2
4c7
5c7
6c3
customers
customer_idname
c2Ana
c3Ben
c7Dee

No order rows move. Each of the 2 input tasks holds 2 of the c7 orders and matches them against its own copy of customers, so c7's popularity stays spread exactly as the input split spread it. Writing the join as orders.join(F.broadcast(customers), "customer_id") asks for the broadcast by name, whatever the threshold is set to. A hot key in a broadcast join costs nothing extra, which is one more reason to broadcast whenever the small side fits.

orders has 80 million rows, and a single customer_id owns 10 million of them. customers is 3MB. What does the hot customer do to this join?

joined = orders.join(F.broadcast(customers), "customer_id")

Softened: sums, counts and averages

A groupBy("track_id").count() on plays does shuffle by the hot key, but Spark first counts each key inside every input partition and ships only those partial counts. If plays is read as 960 input partitions, the hit track reaches its task as at most 960 small rows, which that task adds up in moments. The same holds for sum, avg, min and max. That pre-totaling is why the per-key count from the previous section stays cheap; how it works is a later lesson's subject.

Hit in full: joins, windows and row lists

When both sides of a join are too large to broadcast, Spark shuffles both by the join key and joins them partition by partition, a strategy called sort-merge join. Every one of the hit track's 12 million plays travels to partition 87. The damage multiplies when the key repeats on both sides: joined to a track_credits table with 40 rows for the hit track (writers, producers, performers), those 12 million plays become 480 million output rows, all produced by task 87. That is the duplicate-key blowup from the joins lesson, concentrated on a single task.

A window partitioned by a key gathers every row of that key into a single task too, because the window function needs all of them in order. The program below numbers each play of a track by time:

w = Window.partitionBy("track_id").orderBy("played_at")
ranked = plays.withColumn("play_rank", F.row_number().over(w))

collect_list behaves the same way: its result keeps every value, so every value reaches the hot key's task, and no partial total can shrink a list.

plays has 120 million rows and a hit track_id. tracks and track_credits are both far above the broadcast threshold. Which line does the hit track slow the most?

br = plays.where(F.col("country") == "BR")
per_track = br.groupBy("track_id").agg(F.sum("ms_played"))
titled = per_track.join(tracks, "track_id")
credited = plays.join(track_credits, "track_id")

About this lesson

This section is part of the Data Skew: Beginner 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.