Module · 5 lessons

Partitions, Shuffles & Caching

Where Spark performance actually lives. Learn what a partition is and how many the data wants, why the shuffle is the most expensive thing Spark does and how to make it cheaper, when caching a DataFrame pays and when it just wastes memory, and how a single hot key like JFK turns one task into the whole job — then tune the zone-hour job end to end.

At a glance

Level
Intermediate
Lessons
5 lessons
Time to complete
1 week
Cost
Free forever · no sign-up

Welcome to Partitions, Shuffles & Caching, the fifth module — where Spark tuning stops being about the query and starts being about the machine underneath it. Module 4 ended on the one thing it could not optimize away: the group-by’s Exchange, the shuffle that aggregating by key genuinely requires. It left you with a sharper question than “how do I avoid this shuffle?” — namely, “how big is it, and can I make it cheaper?” That question is where real Spark performance lives, and answering it means understanding the three levers this module is named for.

The first is the partition — the unit of parallelism, one task per partition. How many partitions your data is split into decides how many cores can work at once, how much scheduling overhead you pay, and how big each shuffle piece is; you’ll learn to see the count, control it with repartition and coalesce, and recognize when there are too few or too many. The second is the shuffle itself — the moment Spark regroups rows across partitions for a wide operation like a group-by or a join. It writes files, moves them across the network, and reads them back, which is why it is the single most expensive thing Spark does, and why every Exchange in a plan is worth its own attention. The third is caching — the answer to Spark’s lazy recomputation, where a DataFrame used by three actions is computed three times unless you tell Spark to hold it. You’ll measure exactly when cache() turns that into a one-time cost and when it just fills memory for a workload that never reuses anything.

Then the module confronts the failure mode that quietly ruins otherwise-tuned jobs: skew. When one key is far bigger than the rest — and CityFlow has a perfect example in JFK Airport’s 429,745 trips — one shuffle partition swells, one task runs long after all the others have finished, and the whole stage waits for that single straggler. You’ll diagnose it on the real data and weigh the honest mitigations, including the ones Spark’s adaptive execution already applies for you. The guided project brings it together, tuning the zone-hour job’s physical execution knob by measured knob while proving the answer never changes: still 240,917 buckets, 9,554,757 trips, $256,692,373.14. Start with Lesson 1, where a partition turns out to be the most important number in a Spark job that nobody looks at.

Lessons in this module

1 Partitions Learn that a partition is Spark's unit of parallelism — one task per partition — why CityFlow's January month reads as exactly 10, and how repartition and coalesce reshape that count, measured on the real NYC taxi data down to a 103x slowdown from over-partitioning. 2 Narrow vs Wide: the Shuffle Classify every transformation as narrow or wide, watch the one that costs — the shuffle — appear as an Exchange in the plan, and measure it on CityFlow's zone-hour report: map-side pre-aggregation shrinks 9,554,757 rows to 332,510 partial rows across the wire, a groupBy runs about 3.6x a narrow scan, and AQE coalesces 200 post-shuffle partitions to 6. 3 Caching & Persistence Measure both sides of cache() on CityFlow's zone-hour base: three actions recompute the window-shuffle-join pipeline 2.92 s worth of work, caching cuts that to 1.88 s (1.56x) by turning two recomputes into memory reads — and a single-action or too-big frame where cache buys nothing or throws OutOfMemoryError. 4 Skew & Stragglers Hash-partition CityFlow's quarter by pickup zone and watch one task swell to 2,278,324 rows against another's 170,641 — a 13.4x straggler — then salt the hot key to cut the spread 4.2x while JFK's count stays 429,745. 5 Guided Project: Tune the Zone-Hour Job Take CityFlow's already-correct zone-hour dashboard job — three outputs from one expensive report — and tune its physical execution: measure the 3.33 s recompute trap, discover that a naive cache() is actually slower at 5.57 s because it freezes 200 tiny partitions, then couple caching with a right-sized spark.sql.shuffle.partitions=8 to land the same verified answer in 0.46 s.
Achievement

Complete all 5 lessons to finish the Partitions, Shuffles & Caching module.

Start module
Sponsor

Keep DATATWEETS free. Help fund practical data, AI, and engineering lessons for learners worldwide.

Buy Me a Coffee at ko-fi.com