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
- Writes keyed on the partition column. repartition("city") before partitionBy("city") gives each directory 1 file, and hands all 300GB of New York to a single task while Boise's task finishes in seconds.
- Bucketed reads. A bucket is read whole by 1 task, so with 2048 buckets a key holding 5% of the rows makes its bucket about 100 times the size of the rest.
- Null keys. Every null hashes to the same value, so all trips without a driver share 1 bucket, and any read that keeps them, such as a left join or a groupBy, hands that bucket to 1 task.
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()| trip_id | driver_id | city | fare |
|---|---|---|---|
| 201 | 7 | New York | 18 |
| 202 | NULL | New York | 24 |
| 203 | 3 | Austin | 12.5 |
| 204 | NULL | Austin | 9.5 |
| 205 | 5 | Denver | 27 |
| 206 | NULL | New York | 16 |
| 207 | 9 | New York | 11 |
| 208 | NULL | Denver | 13.5 |
| 209 | 7 | New York | 19.5 |
| 210 | 3 | Austin | 14 |
| 211 | NULL | New York | 21 |
| 212 | 5 | Denver | 8 |
| driver_id | driver_name | rating |
|---|---|---|
| 3 | Ana | 4.9 |
| 5 | Ben | 4.7 |
| 7 | Chen | 4.8 |
| 9 | Dara | 4.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?
- About 1GB, like every other task, because hashing spreads rows evenly
- About 80MB, since the null rows spread evenly across all 2048 buckets
- About 165GB: all 164GB of nulls plus its share of the rest, against under 1GB
- About 2GB, because a bucket that large is split across 2 tasks
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.