Where Hot Keys Come From

Concepts covered: Data Skew

A key runs hot when it owns far more rows than the table's row count divided by the number of shuffle partitions. For plays, that even share is 120 million divided by 200, or 600,000, and the hit track's 12 million is 20 times it. You can find suspects before any job runs by asking what each value of the key column stands for and how the world produces it. Hot keys come from 4 sources again and again.

Popularity: a few values take most of the activity

Activity piles onto a few winners: a hit song, a celebrity's account, a viral video, the largest seller on a marketplace, a company that runs 50,000 employees through a single account_id. Any key that names who or what produced a row inherits that shape. Columns like track_id, creator_id, seller_id and account_id deserve a hot-key check before any join or window on them.

Placeholders: every missing value becomes a single value

Upstream systems often fill a missing value with a default, such as 'unknown', '', 0, -1 or '1970-01-01'. A website that logs every logged-out visitor as user_id = 0 has made 0 its busiest user, often by a wide margin. Placeholders are the sneakiest source because they stand for no real entity, so a team reasoning about real customers skips past them. A missing key stored as NULL gathers into a single group in the same way, and it gets its own lesson later.

Few distinct values: the key cannot fill the partitions

A device column with 4 values (ios, android, web, tv) can fill at most 4 of the 200 shuffle partitions, and fewer when 2 values hash to the same partition. At least 196 tasks receive no rows at all, and the busy ones are as unequal as the device mix: if 70% of rows come from ios, a single task does 70% of the work. Columns such as country, status, plan and event_type carry the same risk.

Bursts and machines: spikes in time and in traffic

Time keys run hot on the days that matter: a launch date, the hour a campaign goes out, the evening of a championship final. Machine traffic does the same to identity keys, since a load-testing rig or a scraping bot can send millions of events under a single device_id or ip_address. These keys look balanced on an ordinary day and spike on the day the job most needs to finish.

columnlikely hot valuesource
track_idthe day's biggest hitpopularity
user_id0 for logged-out visitorsplaceholder
countrythe largest marketfew distinct values
event_datelaunch dayburst in time
device_ida load-testing rigmachine traffic
play_idnone: each value appears onceunique key

Your next job shuffles a 50-million-row orders table into 200 partitions. Which key is the strongest hot-key suspect?

Measuring before you run

The check is a count of rows per key, sorted with the biggest first, compared with the even share. A grouped count stays cheap even when a key is hot, for a reason the next section explains.

key_counts = (
    plays.groupBy("track_id")
    .count()
    .orderBy("count", ascending=False)
)
key_counts.show(10)

Read the top rows against the even share of 600,000. A top count in the millions, like the hit track's, means a single partition will dwarf the rest and a straggler is coming. A top count of 3,000 means track_id is safe to shuffle on.

Run the same check on the practice order_items table. The cell lists the 5 products with the most order lines. Then change both product_id references to user_id and see whether customers concentrate order lines more than products do.

Each line of the count does one job. groupBy names the key column whose values you are testing. count() turns each group into a single row holding the key and its number of rows, in a column named count. orderBy with ascending=False puts the biggest counts first, and that matters because show(5) prints only the first rows: sorted the default ascending way, those first rows are the rarest keys, and the hot one hides at the bottom.

A teammate checks user_id on a 40-million-row events table with the count below. The output lists 5 users with 1 row each. What does that tell them?

counts = events.groupBy("user_id").count().orderBy("count")
counts.show(5)

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.