Salting a Hot Key
Concepts covered: Salting Strategy
A hot key has a single hash value, so it has a single destination. Salting gives it several. Add a salt column holding a number from 0 to N-1 to every row of the big side, then join on the key and the salt together. The 30M rows of acct_1001 now form N separate join keys, and each of those hashes to a partition independently of the others.
The small side has to keep up. A follows row salted 7 can only match an account row that also carries salt 7, so every small-side row is copied N times, once per salt value. The join's output stays the same: each big-side row still meets exactly 1 copy of its partner.
The tables above show the c7 rows of each side and the copies of c1. The animation runs the whole join on 9 orders, 6 of them for customer c7, with 3 shuffle partitions and broadcasting switched off so both sides shuffle. The salt order_id % 3 splits c7 into 3 keys of 2 rows each: orders 3 and 6 carry salt 0, orders 1 and 7 salt 1, orders 2 and 5 salt 2. The 4 customers become 12 rows. Watch where the 3 c7 keys land.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
salted_orders = orders.withColumn("salt", F.col("order_id") % 3)
salted_customers = customers.withColumn("salt", F.explode(F.array(F.lit(0), F.lit(1), F.lit(2))))
salted_orders.join(salted_customers, ["customer_id", "salt"]).collect()| order_id | customer_id |
|---|---|
| 1 | c7 |
| 2 | c7 |
| 3 | c7 |
| 4 | c2 |
| 5 | c7 |
| 6 | c7 |
| 7 | c7 |
| 8 | c3 |
| 9 | c1 |
| customer_id | name |
|---|---|
| c1 | Ana |
| c2 | Ben |
| c3 | Chen |
| c7 | Dee |
Each (customer_id, salt) pair hashes on its own, so the 3 c7 keys are free to reach different tasks, and 2 of them can still share one: salting spreads a key across up to N tasks, never a guaranteed N. Salting adds no shuffle either. The plan still has 1 exchange per side, hashing on 2 columns instead of 1. The output holds the same 9 rows the unsalted join returns; drop salt once the join is done.
Choosing N
Pick N so each salted piece is about the size of an ordinary task. With section 0's numbers, acct_1001 holds 30M rows against a 1.5M-row even share, so N = 30M / 1.5M = 20, and each piece carries roughly what every other task carries. The bill arrives on the small side: an accounts table of 50M rows, copied 20 times, enters the shuffle 20 times larger.
- 1B
That copy cost makes salting a second resort. If the small side fits under the broadcast threshold, broadcast it instead: the big side never shuffles, so no task ever collects a key. Salt when the small side is too big to broadcast and the hot key is too big for a single task. Section 2 copies only the hot keys' partners instead of the whole table.
Every salt needs a partner
A salted join returns the right rows only when the small side carries every salt the big side can produce. If the big side computes order_id % 20 while the small side is copied over 0 to 18, every row salted 19 finds no partner, and an inner join drops it without an error. Build both sides from the same N. Derive the salt from a column with many distinct values, such as a unique id, so a row gets the same salt on every run, and never from the join key, which would hand every row of the hot key the same salt.
This salted join runs without an error. What happens to the order rows whose order_id % 8 equals 7?
salted_orders = orders.withColumn("salt", F.col("order_id") % 8)
salted_customers = customers.withColumn(
"salt", F.explode(F.array(*[F.lit(i) for i in range(7)]))
)
joined = salted_orders.join(salted_customers, ["customer_id", "salt"])- They vanish from the result, because no customer copy carries salt 7 for them to match
- They match the copy salted 6, because Spark rounds a missing salt down to the nearest one
- They all land on a single task, because rows without a partner gather on one reducer
- The join fails at the action, because the 2 salt columns cover different ranges of values
The cell salts a join of order_items to products with N = 4. The products table is tiny, so the cell turns broadcasting off to get the sort-merge join a 50M-row table would get. The output counts joined rows per salt value.
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.