Isolating Hot Keys

Concepts covered: Data Skew

Salting copies the whole small side N times to rescue a few keys. When the measurement names only a handful of hot values, split the big side by those values instead and send each part down its own path. The hot rows join through a broadcast of the few small-side rows that match them, and the rest join as before, with no key left that is too big for its task.

The measuring job's hot list decides each row's path. Hot follows meet 2 broadcast accounts where they were read; every other follow takes the sort-merge join, and the union stacks both results.
  • follows (source, 300M rows)
  • accounts (source, 50M rows)
  • measure hot keys (transform, collect() to the driver)
  • hot follows (transform, isin(hot_keys))
  • other follows (transform, ~isin(hot_keys))
  • hot accounts (transform, 2 rows, broadcast)
  • broadcast join (transform, no shuffle)
  • sort-merge join (transform, both sides shuffled); parallelism: 200 partitions
  • unionByName (transform, no shuffle)
  • joined follows (storage, result)
  • follows -> measure hot keys
  • follows -> hot follows
  • follows -> other follows
  • measure hot keys -> hot follows
  • measure hot keys -> other follows
  • measure hot keys -> hot accounts
  • accounts -> hot accounts
  • accounts -> sort-merge join
  • hot follows -> broadcast join
  • hot accounts -> broadcast join
  • other follows -> sort-merge join
  • broadcast join -> unionByName
  • sort-merge join -> unionByName
  • unionByName -> joined follows
from pyspark.sql import functions as F

hot_rows = (
    follows
    .filter(F.col("followee_id").isNotNull())
    .groupBy("followee_id")
    .count()
    .filter(F.col("count") > 1_500_000)
    .collect()
)
hot_keys = [row["followee_id"] for row in hot_rows]

hot_part = (
    follows.filter(F.col("followee_id").isin(hot_keys))
    .join(
        F.broadcast(accounts.filter(F.col("account_id").isin(hot_keys))),
        F.col("followee_id") == F.col("account_id"),
    )
)
rest_part = (
    follows.filter(~F.col("followee_id").isin(hot_keys))
    .join(accounts, F.col("followee_id") == F.col("account_id"))
)

result = hot_part.unionByName(rest_part)

The collect() runs the measuring job first, on its own, and returns 2 ids, acct_1001 and acct_2002; the isNotNull filter keeps NULL out of the list, and section 3 handles it. The hot path broadcasts the 2 matching accounts to every executor, so the 32.4M hot follows join on the tasks that read them, spread however the input was split, and no task gathers a key. The rest path is the original sort-merge join over every other follow, and its largest key is now acct_3003 at 1.1M rows, under the 1.5M even share.

The hot path broadcasts only because of the F.broadcast hint. Without column statistics, Spark estimates a filtered table at its unfiltered size, so it sees all 50M accounts instead of 2 rows and plans a sort-merge join. The hint states what the measuring job already proved.

Count the plan's shuffles: 2, a shuffle for each side of the rest join. The hot path adds a broadcast and no exchange, and unionByName stacks the 2 results without moving a row. The new costs are the measuring job and a second scan of follows, because each filter reads the table on its own. Both are even passes with no straggler.

In the isolation code above, accounts holds 50M rows, far over the broadcast threshold, and hot_keys holds 2 ids. How many shuffle exchanges does the plan for result contain?

Salting the join
  • Copies every small-side row N times
  • A single plan for every key, with no list to maintain
  • Spreads a hot key over up to N tasks
  • Works when a hot key's partners are too many to broadcast
Isolating hot keys
  • Copies nothing and broadcasts only the hot keys' partners
  • Needs a measuring job to build the hot list
  • Hot rows join where they were read, with no shuffle
  • Scans the big side twice, once per path

Keep the hot list fresh

Measure the hot list on every run instead of pasting ids into the code. Hot keys move: a celebrity joins, an account goes viral, a promotion makes a single product the week's top seller. A hardcoded list goes stale in silence, and the next new hot key lands back on a single task. The measuring job stays cheap because its counts combine before the shuffle.

When a hot key's partners are too many to broadcast, as when both sides of a many-to-many join are hot, keep the split and salt only the hot path. The copies then cover the hot keys' partner rows alone, a small set of rows copied N times instead of the whole small side.

An isolation job runs with a hot list pasted into the code last month: ['acct_1001', 'acct_2002']. This week acct_5005 goes viral and gains 25M follows. What happens to its rows on tonight's run?

About this lesson

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