Narrate the Hunt for the Slow Stage

Concepts covered: Spark UI Diagnosis

The Spark UI has 1 screen per level of the hierarchy. The Jobs tab lists every action the program ran, with its duration and the stages it needed. The Stages tab lists every stage across all jobs, with its task count, its duration, its input size, and its shuffle read and shuffle write in bytes. Opening a stage shows the summary metrics for its tasks: min, 25th percentile, median, 75th percentile and max, for task duration, GC time, shuffle read size, and spill to memory and disk. Those 5 columns are the ones you read aloud, because the gap between median and max is the single number that separates the most common causes from each other.

A stage boundary is a shuffle. Spark runs everything it can without moving rows between machines inside one stage, then stops, writes each partition's rows to local disk grouped by destination, and starts the next stage by reading them back over the network. The program below has 1 shuffle, so it runs as 2 stages, and the animation shows which rows move and where they land.

orders.groupBy("region").sum("amount").collect()
orders
order_idregionamount
1West40
2East25
3West15
4North30
5East10
6North20
7West35
8East45
9North5

Stage 1 reads the 3 input partitions and sums the amounts per region inside each one, so each partition writes at most 3 partial sums, grouped by the task that owns each region. The shuffle routes every partial sum for a region to the same task. Stage 2 adds the partial sums for each region and the driver collects 3 rows. That partial step is why an aggregate's shuffle write can be far smaller than its input. In the Stages tab the job appears as 2 lines, and the second line's shuffle read equals the first line's shuffle write, which is how you confirm that the two lines belong to the same boundary when the list is long.

The hunt, narrated

Here is the Stages tab for the job from the intro, as you would describe it from memory. The job scans a day of viewing events, scans the title catalog, joins them on title_id, aggregates hours watched per title, and writes the result. None of this is visible in the interview room, which is why the narration has to carry every number.

StageWhat it doesTasksDurationShuffle writeShuffle readSpill (disk)
0Scan viewing_events, prepare join key4004 min38 GB00
1Scan title_catalog820 s1.2 GB00
2Join on title_id, aggregate2002 h 51 min0.9 GB39.2 GB24 GB
3Write output2003 min00.9 GB0

Stage 2 owns the clock: 2 h 51 min of a 2 h 58 min job. The other 3 stages together take under 8 minutes, so a change to the scan, the catalog read, or the write cannot return more than 8 minutes however well it goes. Say that sentence in the interview. It is the moment the interviewer learns you can tell the 1 stage that matters from the 3 that do not. Then open stage 2 and read its task summary.

MetricMin25thMedian75thMax
Duration22 s34 s41 s56 s2 h 48 min
Shuffle read60 MB110 MB135 MB160 MB12.4 GB
Spill (disk)000024 GB
GC time0.4 s0.9 s1.2 s1.6 s4 min

Read the shape. Half the tasks finish in 41 seconds and three quarters in under a minute, yet the stage takes 2 h 51 min because 1 task runs for 2 h 48 min. That task read 12.4 GB where its neighbours read 135 MB, about 90 times more, and it is the only one that spilled. One task holding the stage, with a shuffle read far above the median, is the signature of a hot join key, and the hunt has found both its stage and its task without a screen in the room.

If the stage list is long

A real job may have 30 stages across several actions. Sort the Stages tab by duration and add up the top 2 lines. If they cover 80% of the job, the hunt narrows to those 2. If the time is spread across 20 stages of a few minutes each, say so, because that shape points at a different cause, a slow scan or slow per-row code repeated in every stage, and a different fix than a single hot stage would.

The spoken version, in 6 sentences

When the interviewer says there is no UI, narrate what each screen would show anyway. The missing screen is the test, and the narration is the answer.

About this lesson

This section is part of the Make It Faster lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.