Data skew in Spark and salting
diagnosing and fixing skewed distributed work.
data skew is uneven key distribution sending most rows to one partition and straggler task; salting appends a random suffix to hot keys to spread them across partitions, joining in two…
What's really being asked
This checks whether you understand that distributed performance is bounded by the slowest task and that uneven key distribution, not total cluster size, is often the culprit.
The full answer
Data skew occurs when the values of a grouping or join key are unevenly distributed, so a small number of keys account for a large fraction of the rows. When Spark shuffles by that key, all rows for a hot key go to a single partition handled by one task. That task processes far more data than the others, becomes a straggler, and the whole stage cannot finish until it does, while other executors sit idle. Adding more executors does not help because the bottleneck is one oversized partition. Salting mitigates skew by adding a random salt, an integer suffix, to the hot key so its rows are split across many partitions. For a join, the larger skewed table gets a random salt per row, and the smaller table is replicated across all salt values so matches are preserved; the join then runs on the composite salted key, spreading the load, after which you strip the salt and aggregate. Spark's adaptive query execution can also handle skew automatically by splitting large partitions.
The mistakes people make
Thinking more executors or memory alone fixes skew. Forgetting to replicate the other side of a salted join, which drops matches. Confusing skew with general data volume. Salting every key uniformly instead of targeting hot keys.
What usually comes next
How does adaptive query execution mitigate skew without manual salting. How do you identify the skewed keys. What is the downside of salting all keys.
A concrete example
Imagine joining transactions on customer_id where one wholesale account holds eighty percent of all transactions. After the shuffle, one partition holds eighty percent of the rows and its task runs for hours while others finish in minutes. By salting that customer_id with a random value from zero to nine, its rows scatter across ten partitions, and replicating the matching dimension row across those ten salts preserves correctness, turning one multi-hour straggler into ten balanced tasks.
Interview question
Why does simply adding more executors usually fail to fix Spark data skew?
- a.Executors cannot run shuffle operations
- b.The bottleneck is one oversized partition handled by a single taskCorrect
- c.More executors increase the skew on the hot key
- d.Skew only affects the driver, not executors
Why? this is the answer
All rows for a hot key land in one partition processed by one task, so the stage waits on that straggler regardless of how many idle executors exist. Salting spreads the hot key across partitions to balance the load.
Just read this? Test yourself on what you have been reading.
Read the original → sparkplayground.com
- #spark
- #data-skew
- #salting
- #shuffle
- #performance-tuning
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles