Debugging Severe PySpark Partition Skew & Out-Of-Memory (OOM) Crashes on a 15TB Event Stream
1. Situation & Stakes
A nightly PySpark batch job aggregating 15TB of clickstream and checkout events across 120 worker nodes repeatedly failed with java.lang.OutOfMemoryError: Java heap space and executor heartbeat lost.
2. Task & Ownership Mandate
I took lead responsibility for diving into the Spark UI DAG visualization, identifying the root cause of the task straggler skew, and restructuring the join architecture to guarantee stable sub-hour completion.
Success Criteria: Job completion in under 60 minutes with zero executor OOM drops and a 50%+ reduction in AWS EMR cluster compute hours.
3. Technical Action & Trade-Offs
- Inspected the Spark UI Stage breakdown: discovered that while 1,999 tasks completed in under 45 seconds, a single straggler task processed 142GB of shuffle data due to 38% of records having a null or guest customer_id.
- Implemented two-phase salted key joins: appended a random salt (0 to 19) to skewed keys in the probe table, replicated the dimension lookup table with 20 salted rows, and aggregated in two parallel stages.
- Tuned Adaptive Query Execution (AQE): enabled spark.sql.adaptive.skewJoin.enabled and tuned spark.sql.adaptive.skewJoin.skewedPartitionFactor to 3.
- Eliminated memory spills to disk by increasing spark.sql.shuffle.partitions from default 200 to 2,400, aligning partition sizes to an optimal 128MB.
- Switched from SortMergeJoin to BroadcastHashJoin for dimensions under 100MB by bumping spark.sql.autoBroadcastJoinThreshold to 64MB.
- Configured KryoSerializer instead of default Java serialization, reducing shuffle payload serialization overhead by 32%.
4. Quantified Results
- Slashed end-to-end execution runtime from 8.2 hours (with frequent failures) down to 24 minutes (95% performance improvement).
- Reduced EMR cluster footprint from 120 nodes down to 48 nodes, saving $28,500/month ($342,000/year).
- Achieved 100% SLA reliability over the subsequent 6 months without a single OOM failure.
Business Impact: Restored daily executive marketing attribution by 05:00 AM, unblocking the performance marketing team to optimize high-yield ad spend.
5. Executive Reflection (Learning Agility)
- Executive Hindsight: In hindsight, throwing larger AWS EC2 hardware at distributed computing problems is an anti-pattern. Analyzing Spark UI shuffle read bytes always isolates the algorithmic flaw.
- Learning Agility: Deepened my intuition for distributed hash partitioning mechanics, data salting math, and the power of Spark 3.x Adaptive Query Execution.
- Systemic Impact: Built the standard cluster sizing and AQE baseline configuration template now packaged into our company-wide Terraform EMR module.
Interviewer Follow-Up Probes (Prepare for These Next)
- Q1.How did you choose the number of salt buckets (0-19) vs a larger number like 100?
- Q2.Why did BroadcastHashJoin fail when the dimension grew beyond 100MB?
- Q3.How does Spark AQE dynamically coalesce and split skewed shuffle partitions at runtime?