Spotting a Straggler

Concepts covered: Spark UI Diagnosis

Click stage 2's Description to open its page. Under the DAG and the event timeline sits Summary Metrics for 200 Completed Tasks: a row per metric, with columns for the fastest task, the 3 quartiles and the slowest task.

MetricMin25th percentileMedian75th percentileMax
Duration29 s36 s40 s45 s50 min
Shuffle Read Size / Records81.5 MiB / 171240692.4 MiB / 193418798.0 MiB / 2043118103.7 MiB / 21619023.2 GiB / 66417305

Read the Duration row first

Min, 25th percentile, Median and 75th percentile run from 29 to 45 seconds, so at least 150 of the 200 tasks finished within 45 seconds. Max is 50 minutes. All of the spread is in the last column, which is the signature of a straggler: a task, or a few, still running long after the rest have gone idle.

Was the slow task handed more data?

The Shuffle Read row answers it. The median task fetched 98.0 MiB and about 2.0 million records, and the max fetched 3.2 GiB and 66.4 million, roughly 33 times as much. The straggler was handed far more records than its siblings, and a join stage sends every row with the same join value to the same task, so the next question is which value.

Below the summary, the Tasks table lists every task. Sorted by Duration, descending:

IndexExecutor IDDurationShuffle Read Size / Records
137550 min3.2 GiB / 66417305
42258 s112.9 MiB / 2349811
171755 s108.6 MiB / 2260437

Task 137 handled shuffle partition 137 of the join, which collects every profile_id that hashes to it. A row count per profile_id on views finds the culprit: profile_id 0 holds 64.2 million rows, the placeholder the player logs while a device previews titles before anyone signs in. Those rows plus roughly 2.2 million from ordinary profiles sharing the partition make the 66.4 million.

The same shape, small enough to watch

The animation runs a sort-merge join of 6 events to 3 users, with spark.sql.autoBroadcastJoinThreshold set to -1 so both sides shuffle. User u1 owns 4 of the 6 events. In the join stage, whichever of the 2 tasks receives u1 handles at least 5 of the 9 shuffled rows, and in a full-size job that task would sit alone in the Max column.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
events.join(users, "user_id").collect()
events
event_iduser_id
1u1
2u1
3u2
4u1
5u1
6u3
users
user_idplan
u1free
u2pro
u3free

Now on the practice data. The cell counts rows per user_id in transactions, largest first. In a join on user_id, the top count is a floor on the records its task would read, and setting it against a typical count predicts how far that task's Records would sit above the median.

The fix, measured

Preview rows never belong in a Continue Watching row, so the fix is another filter in stage 0: .filter(F.col("profile_id") != 0). Measured on the same input with the same 8 executors:

NumberBaselineWith the filter
Total runtime59 min12 min
Stage 252 min4.9 min
Median task40 s39 s
Max task50 min52 s
Shuffle Read22.7 GiB19.5 GiB

The max task fell from 50 minutes to 52 seconds, within 2x of the median, and the run returned to 12 minutes with no executor added.

Four stages, each summarised by its Duration and Shuffle Read rows. Which one points to a straggler?

About this lesson

This section is part of the The Spark UI: Beginner 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.