Apache Spark Big Data (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Data Skew Resolution Techniques: Salting partition keys and two-stage aggregations.
senior Levelapache-sparkAdvanced Internals
In Simple Words:
When 90% of rows contain user_id = 'GUEST', all those rows route to a single partition, leaving 99 executors idle while 1 executor runs for 45 minutes:
- Key Salting:
- Append a random integer (0 to 9) to the skewed key:
concat(key, "_", floor(rand() * 10)). - The skewed data evenly disperses across 10 separate parallel partitions.
- Perform the join or aggregation over salted keys.
- Strip the salt in a secondary aggregation to merge final totals.
- Append a random integer (0 to 9) to the skewed key: