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()
orders
order_idcustomer_id
1c7
2c7
3c2
4c7
5c7
6c3
customers
customer_idname
c2Ana
c3Ben
c7Dee

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?

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.