Skew Before the First Shuffle

Concepts covered: Data Skew

Every skewed stage you have fixed so far sat after a shuffle, where a hot key hashed to 1 reducer. The first stage of a job can skew with no key involved, because its task sizes come from the way the input splits. Spark cuts splittable files into pieces of at most spark.sql.files.maxPartitionBytes (128MB by default) and runs 1 task per piece. When the pieces come out unequal, or the work each piece triggers is unequal, the stage has a straggler before a single row has been hashed.

Four ways a first stage goes uneven

SourceWhat the tasks seeRebalance at
Unsplittable fileA 6GB gzip CSV is read as 1 partition by 1 task while its neighbours read 128MB eachRight after the read, or rewrite the source in a splittable form
Uneven layoutOne date directory holds 40 times the rows of a typical dateThe write that made the layout; after the read until then
Uneven fan-outexplode() turns most rows into 3 rows and a few into 50,000After the explode
Uneven filterA filter keeps 90% of some files and 1% of othersBefore the next expensive step

Gzip is unsplittable because a decompressor must start at the first byte, so a 6GB file holds 1 task for as long as 1 core needs to inflate and parse all of it. The fan-out case is the subtle one. Pipelining puts the read, any filter and the explode into the same task, so the task that reads a partition also produces everything the explode emits. Take 128MB partitions of about 1M rows each, where a typical row carries 3 tags. One partition also holds 300 rows whose tags array has 50,000 entries. Its neighbours emit 3M rows; it emits 18M, 6 times as many, from an input the same size as theirs.

Diagnosing it

The tell is a straggler in a stage that reads input and no shuffle. In that stage's task list, compare the slow task's input bytes and output records with the median task's. Large input points at the file or the layout; normal input with huge output points at fan-out. To see row counts per partition from code, tag each row with the partition that holds it:

per_partition = (
    events
    .withColumn("pid", F.spark_partition_id())
    .groupBy("pid")
    .count()
)
per_partition.orderBy(F.desc("count")).show(5)

Rebalancing on purpose

repartition(n) with no column deals each map task's rows round-robin across n partitions, so every output partition ends up with nearly the same row count whatever the keys look like. It is a full shuffle: a 400GB stage writes 400GB and reads it back, so place it once, at the line where the imbalance first appears. Two similar-looking calls do not balance. coalesce(n) merges neighbouring partitions without moving rows across the network, so a 6GB partition merged with 2 small ones becomes a larger straggler. repartition(n, "page_id") hashes a column, which copies whatever skew that column carries into the new layout.

A first stage reads 300 files: 299 Parquet files of around 128MB, and 1 gzip CSV of 6GB that lands in a single partition. Store 1 holds a third of all rows. Which line, placed right after the read, gives the next stage partitions of nearly equal row count?

For fan-out, the right line depends on how concentrated it is. A round-robin repartition(200) before the explode spreads the 300 fat rows to about 1 or 2 per task, which is enough here. It cannot help when the fan-out sits in a handful of rows: 1 row with 20M tags explodes inside whichever task receives it. A repartition after the explode always balances what follows, because it moves output rows; the exploding task still emits all 20M, so only the work downstream of it gets spread.

A job reads 2,000 even Parquet files of messages and calls explode("recipients"). One announcement row lists 30M recipients; every other row lists under 10. The step after the explode builds a delivery record per recipient. Where does a repartition(400) do the most good?

About this lesson

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