Adaptive Salting
Concepts covered: Salting Strategy
Salting with 1 global N gives every key N salt values and replicates every row of the other side N times. Skewed data usually follows a steep curve, with a handful of keys far above the rest, so most of that replication copies tail keys that were never slow. Pick N for the biggest key and the replication cost balloons; pick it for a middling key and the biggest stays a straggler. Adaptive salting gives each key its own factor, from its measured row count: n_salt = max(1, ceil(rows / target)), where target is the row count of a well-sized partition. If a 128MB shuffle partition holds about 1M of these rows, the target is 1M. A key at or below the target keeps a factor of 1 and joins as it always did.
| merchant_id | fact rows | n_salt | rows per sub-key |
|---|---|---|---|
| m_001 | 48,000,000 | 48 | 1,000,000 |
| m_002 | 9,000,000 | 9 | 1,000,000 |
| m_003 | 2,600,000 | 3 | about 867,000 |
| 1,999,997 others | 400,000 or fewer each | 1 | unchanged |
The dimension side, a merchants table of 2,000,000 rows and far past the broadcast threshold, needs 1 copy of each merchant per salt value. Compare a single N large enough for m_001 with the per-key plan:
- 96,000,000
- 2,000,057
Building the salted join
TARGET = 1_000_000
salt_plan = (
payments.groupBy("merchant_id").count()
.withColumn("n_salt", F.ceil(F.col("count") / TARGET).cast("int"))
.filter(F.col("n_salt") > 1)
.select("merchant_id", "n_salt")
)
facts = (
payments
.join(F.broadcast(salt_plan), "merchant_id", "left")
.withColumn("salt", F.col("txn_id") % F.coalesce(F.col("n_salt"), F.lit(1)))
)
dims = (
merchants
.join(F.broadcast(salt_plan), "merchant_id", "left")
.withColumn(
"salt",
F.explode(F.sequence(F.lit(0), F.coalesce(F.col("n_salt"), F.lit(1)) - 1)),
)
)
joined = facts.join(dims.drop("n_salt"), ["merchant_id", "salt"])The 2 salt expressions must agree. A payment for m_001 with txn_id 1,000,001 gets salt 1,000,001 % 48 = 17 and meets the copy of m_001 whose exploded salt is 17; a tail merchant gets salt 0 on both sides. Each payment matches exactly 1 copy of its merchant, so the result holds the same rows the unsalted join would produce.
When the distribution moves
The factors are as current as the counts behind them. A count taken in the same run costs 1 extra pass over the facts and is always current. Yesterday's counts cost nothing today but miss any key that turned hot overnight: that key gets a factor of 1, and its full weight lands on 1 task. A sample, such as payments.sample(0.01) scaled by 100, still reads every row but shuffles roughly 1% of them; a key near a boundary may get a factor 1 lower than it needs, which a target set below the ideal partition size absorbs. Whatever the source, a stale plan changes how even the tasks are, never which rows the join returns.
- One global N
- Every key gets N salts and N dimension copies
- N fits 1 key well and misfits the rest
- No counting pass
- Sized by a guess
- Per-key factors
- Only keys above the target get extra copies
- Each hot key gets the split its size needs
- One extra counting pass, or a sample
- Drifts as the distribution shifts
A salt plan built from yesterday's counts uses a target of 500,000 rows. Merchant m_042 had 3.2M payments yesterday and has 7M today. What does today's salted join do with m_042?
- It uses factor 7, from ceil(3.2M / 500,000), so each of its sub-keys carries about 1M rows, twice the target
- It uses factor 14, from ceil(7M / 500,000), because the join recounts each merchant as it reads payments
- It uses factor 1 because m_042 grew since the plan was built, so all 7M of its rows land on a single task
- It drops some of m_042's rows, because the fact side and the dimension side now disagree on its salts
The cell computes factors on the practice payments table with a target of 2 rows per sub-key, small because the table is small. Run it, then raise target_rows to 4 and watch the factors drop toward 1.
About this lesson
This section is part of the Data Skew: 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.