Apache Spark · Experienced
How do you handle data skew in Spark?
By Anurag Pandey, Lead Data Engineer ·
The short answer
Skew is when a few keys hold most of the rows, so a few tasks run far longer than the rest. Find it in the Spark UI, then fix it with AQE's skew join handling, broadcasting the small side, salting hot keys or handling null keys separately.
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.
Say it like this
About 60 seconds out loud. Use it as a model, then put it in your own words.
Data skew means a few keys have far more rows than the rest, so after a shuffle a few partitions are huge. The symptom is a stage where most tasks finish in seconds and two or three run for many minutes. I confirm it in the Spark UI by comparing the maximum task time and shuffle read size with the median. Then I pick a fix. First, I check Adaptive Query Execution is on, because it can split skewed partitions in sort-merge joins automatically. If one side of the join is small, I broadcast it so nothing is shuffled. If a hot key is the problem, I salt it: add a random number to the key on the big side and replicate the matching rows on the other side, so the work spreads across tasks. And often the hot key is just null, which I filter out or handle separately.
What interviewers look for
- You describe how to detect skew, not only how to fix it.
- You know AQE handles skewed joins automatically.
- You explain salting clearly and mention the null key case.
Common mistakes
- Jumping to salting before checking AQE or a broadcast join.
- Adding more executors, which doesn't help one oversized task.
- Forgetting that salting needs the other side replicated too.
Follow-up questions to expect
- 1How does AQE decide a partition is skewed?
- 2How would you salt a join, step by step?
- 3How is skew in a groupBy different from skew in a join?
Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.