Spark Optimization
The four levers: data, shuffle, memory, plans
Optimizing Spark is about reducing what travels and what recomputes: minimize shuffle, avoid spills, use broadcast joins, cache deliberately, and let auto-tuning (AQE) do the mechanical parts.
What you will learn
- Diagnose a slow Spark job from stage metrics
- Apply broadcast joins, partition tuning and caching deliberately
- Use Adaptive Query Execution to fix skew and shuffle automatically
What is this?
Spark optimization is the practice of finding and removing the job's bottleneck: too much shuffle, memory pressure that spills to disk, a skew that strands most of the cluster, or a plan that reads more than needed. Standard fixes are projection/filter pushdown, broadcast small tables, right-sized partitions, deliberate caching and spill-aware memory, perfected by AQE for skew and shuffle auto-tuning.
Why do you need it?
- Optimization decisions routinely have 10-100x impact on job cost and latency.
- Understanding which knob moves each bottleneck prevents cargo-cult tuning (more RAM ≠ faster).
- Cluster cost tracks cluster utilization, so optimized jobs lower the bill.
- Directing optimization at the plan lets you keep data small instead of adding raw compute.
Mental model
Reducing what you ship internationally
Spark optimization
Free shipping (broadcast join) if the package is small; choose lighter packaging (projection); consolidate to fewer trips (fewer, bigger partitions); and track customs delays (spill, skew) to fix the specific bottleneck.
How it works
- Projection and filter pushdown shrink what is read (PartitionFilters, PushedFilters).
- Broadcast joins copy a small table to every executor, avoiding a full shuffle join.
- Partition tuning matches shuffle partition count to data size and cores.
- Memory/spill management keeps the working set in memory; storage levels control cache behaviors.
- AQE (spark.sql.adaptive.enabled) optimizes at runtime: coalesces post-shuffle partitions, switches join strategies from stats, and splits skewed partitions.
Optimization levers in a query plan.
In production
- A batch pipeline broadcasting a 1MB country table turns a shuffle join into a no-shuffle broadcast, cutting runtime 4x.
- AQE coalesced 200 idle shuffle partitions down to 8 for a small job, slashing overhead.
- Salting a hot 'NULL' product key split a skewed reducer into many, removing the 10x straggler.
Best practices
- Start with the plan, not the hardware: profile stages, find the shuffle/spill, then tune.
- Prefer the two CLI-level high-value knobs first: broadcast join threshold and shuffle partitions.
- Cache only what's reused; persist disk for very large intermediates.
- Let AQE handle the mechanical: enable spark.sql.adaptive.enabled=true.
Common mistakes
- Adding executors/memory before diagnosing (skew isn't fixed by RAM).
- Caching everything, bloating memory to force spills.
- Treating 200 shuffle partitions as a universal default.
- Forgetting the write-side: unruly file sizes/partitions dominate output latency.
Common issues
One stage dominates the runThe bottleneck stage is shuffle-heavy or adding data rather than reducing
Push filters earlier, broadcast the small side, and reduce the row width.
Spill to disk appearsData too wide/heavy for the executor memory pool
Project columns earlier, lower partition row-count, or raise executor memory.
AQE not helping despite enablingThe wrong mode: AQE needs broadcastJoinSize and coalescing set; skew needs stage separation
Enable spark.sql.adaptive.coalescePartitions + focused skew tuning (spark.sql.adaptive.skewJoin).
Debugging tips
- Time the slow stage first; read input/size vs shuffle-write.
- A spill in the UI means memory pressure — reduce chunk or raise memory.
- Max vs median task duration exposes skew at a glance.
- Diff the physical plan between a slow and fast run: strategies shift silently.
Interview checkpoint
Hands-on lab
- Create a dataset with a hot key to reproduce skew
- Measure the unequal stage duration
- Apply salting and observe the straggler disappear
Make a Skewed Spark Job Fast
35 min · advanced
Quick quiz
The strongest single sign of data skew in a stage is...
More executors always make a Spark job faster.