Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 74/ 160
04. Big Data Processing & Apache Spark

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.

AdvancedSkew · Salting · AQE3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 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.

Visual diagram

Loading interactive 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.

Solving Data Skew in one concrete design

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.

Key takeaway: Data skew occurs when one or a few hash partitions dominate runtime

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Skew

1. 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.

key counts, bytes, and task duration
Key numbers at this step
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 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.
  • 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

Skew
Best for: A power-law or concentrated partition load where workers and tasks spend unequal wall time or bytes.

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.

Salting
Best for: Adding a bounded randomized/derived component to spread a hot join key while preserving the required pairing.

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.

AQE
Best for: Spark runtime plan revision based on completed shuffle and scan metrics, including partition coalescing and skew split.

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 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.
  • 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()) % 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.

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 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.
  • When is Skew insufficient for AQE skew factor, and what does adding Salting cost in Median/p99 task?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Join Strategies in Spark (Broadcast Hash Join, Sort-Merge Join, Shuffle Hash Join)
Next topic
Caching, Persistence Levels & Memory Management (OOM Debugging)