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.
| Metric | Min | 25th percentile | Median | 75th percentile | Max |
|---|---|---|---|---|---|
| Duration | 29 s | 36 s | 40 s | 45 s | 50 min |
| Shuffle Read Size / Records | 81.5 MiB / 1712406 | 92.4 MiB / 1934187 | 98.0 MiB / 2043118 | 103.7 MiB / 2161902 | 3.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.
- 75x
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:
| Index | Executor ID | Duration | Shuffle Read Size / Records |
|---|---|---|---|
| 137 | 5 | 50 min | 3.2 GiB / 66417305 |
| 42 | 2 | 58 s | 112.9 MiB / 2349811 |
| 171 | 7 | 55 s | 108.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()| event_id | user_id |
|---|---|
| 1 | u1 |
| 2 | u1 |
| 3 | u2 |
| 4 | u1 |
| 5 | u1 |
| 6 | u3 |
| user_id | plan |
|---|---|
| u1 | free |
| u2 | pro |
| u3 | free |
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:
| Number | Baseline | With the filter |
|---|---|---|
| Total runtime | 59 min | 12 min |
| Stage 2 | 52 min | 4.9 min |
| Median task | 40 s | 39 s |
| Max task | 50 min | 52 s |
| Shuffle Read | 22.7 GiB | 19.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?
- Duration median 12 s, max 19 s; Shuffle Read median 60 MiB, max 74 MiB.
- Duration median 12 s, max 14 min; Shuffle Read median 60 MiB, max 4.1 GiB.
- Duration median 3 min, max 4 min; Shuffle Read median 900 MiB, max 1.1 GiB.
- Duration median 12 s, max 21 s; Shuffle Read median 2 MiB, max 3 MiB.
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.