Layout-Induced Skew

Concepts covered: Data Skew

Skew is a single task holding far more data than its siblings. A stage ends when its last task does, so that task's runtime becomes the stage's runtime. Most skew arrives with the data. Layout skew is written down: a partition column or bucket key with lopsided values bakes the imbalance into the files, and every job that reads them inherits it.

Large directories still scan evenly

Take 1 day of trips partitioned by city: 300GB under city=New York and 40MB under city=Boise. A scan of both still runs evenly sized tasks, because Spark cuts large files into splits of up to spark.sql.files.maxPartitionBytes, 128MB by default, and packs small files together. New York becomes about 2,400 tasks and Boise 1. Directory size alone does not skew a scan.

Every null lands in 1 bucket

The program below left-joins 12 trips to drivers; 5 of those trips have no driver yet.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)

joined = trips.join(drivers, "driver_id", "left")
joined.collect()
trips
trip_iddriver_idcityfare
2017New York18
202NULLNew York24
2033Austin12.5
204NULLAustin9.5
2055Denver27
206NULLNew York16
2079New York11
208NULLDenver13.5
2097New York19.5
2103Austin14
211NULLNew York21
2125Denver8
drivers
driver_iddriver_namerating
3Ana4.9
5Ben4.7
7Chen4.8
9Dara4.6

All 5 null-key trips meet on the same reducer task. A bucketed write sends rows through the same hash, so on disk those trips would share 1 bucket, and that bucket stays whole in every read.

trips is bucketed into 2048 buckets on driver_id, and 8% of a day's 2TB has a null driver_id. In a bucketed left join to drivers, about how much does the task holding the null bucket read?

Detecting skew before it ships

A bucket can never hold less than its hottest key. With n buckets an even bucket holds 1/n of the rows, so any key above that share produces the largest task however high n goes. Before choosing a partition column or bucket key, count rows per value and compare the top share with 1/n. Run that count for country on user_events, with nulls kept as a value of their own, since they share 1 bucket like any other key.

On a table already written, the Spark UI shows the same thing per task. A stage's summary metrics list the minimum, median and maximum of task duration and input size; a maximum many times the median, on a stage reading bucketed or freshly partitioned data, points at the layout.

Designing around skew

For writes, range partitioning on the partition column plus a high-cardinality column splits the hot value. Spark samples the keys and cuts them into ranges of similar row counts, so New York's trips spread over many ranges and many tasks each write a slice of its directory, while several small cities share 1 range.

(
    trips.repartitionByRange(400, "city", "trip_id")
    .write.partitionBy("city")
    .mode("overwrite")
    .saveAsTable("curated.trips_by_city")
)
Do
  • Partition by a column with bounded, even values, such as ds
  • Compare the top key's row share with 1/n before choosing a bucket key
  • Write null or placeholder keys to their own table before bucketing
  • Spread a hot partition value on write with repartitionByRange on a second column
Avoid
  • Repartition by the partition column alone when 1 value dominates
  • Bucket on a key whose nulls or test accounts hold a large share of rows
  • Raise the bucket count to tame 1 hot key; its bucket stays whole

About this lesson

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