Solving Data Skew (Salting Keys, Adaptive Query Execution / AQE)
Fix skewed joins and aggs with salting, AQE, and skew hints so one task doesn’t dominate runtime.
In plain words
Key counts, bytes, and task duration. Next, null/default/hot-key treatment. Finally, deduplicate and compare totals by original key.
Think of it like…
Hashing millions of parcels to the same postal code overloads one depot; salting assigns suffix routes, while AQE watches queue lengths and splits an unexpectedly huge route at runtime.
Where you’ve seen it
salt = hash(id, rand()) % 64 on 64 rows per duplicate hot key on the large side pairs with explode(array(0..63)) on the replicated small side; AQE may instead split a skewed partition without code changes.
Words you’ll hear
- Skew
- A power-law or concentrated partition load where workers and tasks spend unequal wall time or bytes.
- Salting
- Adding a bounded randomized/derived component to spread a hot join key while preserving the required pairing.
- AQE
- Spark runtime plan revision based on completed shuffle and scan metrics, including partition coalescing and skew split.
- Operational boundary
- A 99% key such as
NULLproduces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators. Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged.
Visual diagram
Theory & deep dive
Skew: the working mechanism
Data skew occurs when one or a few hash partitions dominate runtime; Adaptive Query Execution can coalesce partitions, switch join strategy, and split skewed shuffle/join partitions from runtime statistics.
Key counts, bytes, and task duration. Null/default/hot-key treatment, then split, replicate, or repartition; this exposes the state governed by AQE.
salt = hash(id, rand()) % 64 on 64 rows per duplicate hot key on the large side pairs with explode(array(0..63)) on the replicated small side; AQE may instead split a skewed partition without code changes.
Solving Data Skew: failure mode and production response
A 99% key such as NULL produces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators.
Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged. Track Median/p99 task as "large ratio indicates straggler/skew", and make AQE skew factor visible to the dataset owner.
How it works, step by step
Step 1 of 41. Profile skew
Key counts, bytes, and task duration. Data skew occurs when one or a few hash partitions dominate runtime; Adaptive Query Execution can coalesce partitions, switch join strategy, and split skewed shuffle/join partitions from runtime statistics.
- Median/p99 task
- large ratio indicates straggler/skew
- Salting
- Adding a bounded randomized/derived component to spread a hot join key while preserving the required pairing.
When to use it & trade-offs
What it is
Data skew occurs when one or a few hash partitions dominate runtime; Adaptive Query Execution can coalesce partitions, switch join strategy, and split skewed shuffle/join partitions from runtime statistics. salt = hash(id, rand()) % 64 on 64 rows per duplicate hot key on the large side pairs with explode(array(0..63)) on the replicated small side; AQE may instead split a skewed partition without code changes.
Why it matters: A 99% key such as NULL produces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators. Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged.
When to reach for it
- Fix skewed joins and aggs with salting, AQE, and skew hints so one task doesn’t dominate runtime.
- Choose Skew because it provides this capability: A power-law or concentrated partition load where workers and tasks spend unequal wall time or bytes.
- Keep evidence for Median/p99 task at "large ratio indicates straggler/skew" so the last production stage-Reconcile - deduplicate and compare totals by original key-remains testable.
When to avoid or be careful
- A 99% key such as
NULLproduces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators. - Do not conflate Salting with Skew. The second concept contributes different behavior: Adding a bounded randomized/derived component to spread a hot join key while preserving the required pairing.
- Release only after this guardrail is in place: Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged.
Skew / Salting / AQE - roles, limits, and operating cost
Strengths: A power-law or concentrated partition load where workers and tasks spend unequal wall time or bytes. Data skew occurs when one or a few hash partitions dominate runtime.
Watch out: A 99% key such as NULL produces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators.
Strengths: Adding a bounded randomized/derived component to spread a hot join key while preserving the required pairing. In this workflow it supports: Apply salting/AQE - split, replicate, or repartition.
Watch out: The operating cost is real: configured relative to median partition bytes for detection.
Strengths: Spark runtime plan revision based on completed shuffle and scan metrics, including partition coalescing and skew split. Its output can be checked with Salt fanout.
Watch out: Operating guardrail: Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Skew, and how would Median/p99 task reveal it?”
- “Why is Salting distinct from this behavior: Spark runtime plan revision based on completed shuffle and scan metrics, including partition coalescing and skew split?”
- Production clue: A 99% key such as NULL produces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators.
- Design probe: Quantify key distribution, filter or special-case invalid hot keys, salt a key on one side and replicate the smaller side when joining, and enable/measure AQE split size while keeping the logical result unchanged.
Benchmarks & rules of thumb
- Median/p99 task
- large ratio indicates straggler/skew
- AQE skew factor
- configured relative to median partition bytes for detection
- Salt fanout
- replication factor bounded by small-side size and memory
Common mistakes & follow-ups
Common interview mistakes
- A 99% key such as
NULLproduces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators. - Reporting success from a job exit code without checking Median/p99 task and AQE skew factor.
- Treating this example as complete without replay, rollback, or access boundaries:
salt = hash(id, rand()) % 64on 64 rows per duplicate hot key on the large side pairs withexplode(array(0..63))on the replicated small side; AQE may instead split a skewed partition without code changes.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A power-law or concentrated partition load where workers and tasks spend unequal wall time or bytes.
- What does the recovery plan do when this occurs? A 99% key such as
NULLproduces an enormous task that cannot be solved by increasing the default partition count alone, and AQE's skew mitigation depends on runtime thresholds and supported operators. - When is Skew insufficient for AQE skew factor, and what does adding Salting cost in Median/p99 task?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Solving Data Skew (Salting Keys, Adaptive Query Execution / AQE) with data engineer interview Q&A and hands-on practice.