Measuring Skew

Concepts covered: Data Skew

Skew lives in the data before Spark touches it: a few values of the join or grouping key carry far more rows than the rest. The shuffle turns that imbalance into task time, because hash partitioning sends every row of a key to the same reducer. The most useful measurement is therefore a row count per key, taken on the side that will be shuffled and ranked from heaviest down.

Rank the keys by row count

The cell below ranks order_items by product_id. A groupBy already gathers every missing key into a group of its own. The cast and coalesce give that group the string label NULL and give every key 1 type, so the missing value ranks, sorts and displays like any other key. Each key's share divides its rows by the table's total, which the first count() fetches as a job of its own. Run it and read the top row: that value sets the minimum load of whichever task receives it.

This measurement stays cheap on a huge table. count combines before the shuffle, so each map task sends a single partial count per key it saw, never the rows themselves. Because limit(10) follows the orderBy, Spark also keeps a running top 10 in each partition instead of range-shuffling every key, and the ranking job runs with only the shuffle that groupBy needs.

From a count to a verdict

A row count becomes a verdict when you compare it with what a task normally receives. Take a follows table of 300M rows, shuffled into 200 partitions on followee_id. Spread evenly, each task would get 1.5M rows: the even share. Any key holding more than the even share is hot, because its task can't finish before it has processed that key's rows, however the other keys fall.

followee_idrowsshareverdict
acct_100130,000,00010.0%hot: 20 times the even share
NULL12,000,0004.0%hot: 8 times the even share
acct_20022,400,0000.8%hot: 1.6 times the even share
acct_30031,100,0000.37%below the 1.5M even share

Run the count on both sides of a join. A key that is hot on both sides compounds: if the 30M follows rows of acct_1001 met 5 matching rows on the other side, the join would emit 150M rows, every one built on that key's task. That is the duplicate-key blowup and skew at once, and only per-key counts on each side reveal it before the job runs.

The ranking names the exact values to fix, which the running job cannot. Once the stage runs, the straggler shows up as a single task reading far more shuffle data than its peers, but that figure is a partition's total: the hot key mixed with every other key that hashed alongside it. The per-key count tells you which value it is and how many rows it brings.

Measure what each task will get

from pyspark.sql import functions as F

per_task = (
    follows
    .repartition(200, "followee_id")
    .groupBy(F.spark_partition_id().alias("task"))
    .count()
    .orderBy(F.desc("count"))
)

This version answers the task-level question directly. It hashes followee_id into the same 200 partitions the join would use, then counts the rows that landed in each. Its top row is the straggler's load, neighbours included, before the real job ever runs. It pays for that with a full shuffle of the table, so run the per-key ranking first and reach for this one when you need partition totals.

A join shuffles 400M rows into 400 partitions. The per-key count of its join key returns these top values: k1 with 9M rows, k2 with 3M, k3 with 800K and k4 with 600K. Which values are hot?

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.