What Skew Is
Concepts covered: Data Skew
Data skew means the partitions coming out of a shuffle differ wildly in size: most hold a normal share of rows, and a few hold many times more. The cause is the rule a shuffle uses to place rows. Spark hashes the key column, divides the hash by the number of shuffle partitions, and sends the row to the partition named by the remainder. Two rows with the same key always produce the same remainder, so they always land together. When every key owns a similar number of rows, the partitions come out similar. When a single key owns a tenth of the table, whichever partition it hashes to receives that tenth on top of its ordinary share.
Watching a join pile rows onto a single task
The program below joins 6 orders to 3 customers on customer_id. Customer c7 placed 4 of the 6 orders. The first line sets spark.sql.autoBroadcastJoinThreshold to -1, which turns broadcasting off, so both tables are shuffled by key the way 2 large tables would be. Each table is read as 2 input partitions, and the shuffle writes 2 output partitions.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(customers, "customer_id").collect()| order_id | customer_id |
|---|---|
| 1 | c7 |
| 2 | c7 |
| 3 | c2 |
| 4 | c7 |
| 5 | c7 |
| 6 | c3 |
| customer_id | name |
|---|---|
| c2 | Ana |
| c3 | Ben |
| c7 | Dee |
Before the shuffle, the orders sit in 2 input partitions of 3 rows each, an even split. After it, every c7 row has moved to whichever task c7 hashed to: 4 orders plus Dee's customer row, which is 5 of the 9 rows in the job. The c2 and c3 rows go wherever their own hashes send them, which may be the other task or the same crowded one. The input split was balanced and the shuffle undid it, because the shuffle places rows by key and c7 owns most of the orders.
The same shape at 120 million rows
Now scale it up. A plays table holds 120 million rows, a row for every song played, and a nightly job joins it to a tracks table on track_id. Both tables are far too large to broadcast, so Spark shuffles both into 200 partitions, the default value of spark.sql.shuffle.partitions. If plays were spread evenly, each partition would hold 600,000 of them. On this day, a hit track drew 12 million plays.
The right-hand table shows 4 of the 200 partitions after the real shuffle. The 108 million plays of all the other tracks still spread evenly, 540,000 to a partition. The hit track hashed to partition 87, so its 12 million plays went there too, leaving partition 87 with 12,540,000 rows: 23 times the size of each neighbour. The other 199 partitions came out a little smaller than the even split, and partition 87 came out enormous.
A job shuffles a 100-million-row events table by user_id into 200 partitions. Which situation is data skew?
- All 200 partitions hold about 500,000 rows, and each task takes 6 minutes because 500,000 rows is a heavy load.
- 199 partitions hold about 460,000 rows and the last holds 8.5 million, because a single user_id owns 8 million rows.
- The input files were read as 200 equal partitions, and the shuffle by user_id then wrote 200 partitions of about 500,000 rows.
- All 200 partitions hold about 500,000 rows, but task 37 runs slowly because its executor's disk is failing.
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.