The Straggler Task
Concepts covered: Data Skew
A straggler is a task still running long after the rest of its stage has finished. A stage is the group of tasks that run between 2 shuffles, each doing the same work on a different partition, and skew is the most common way a stage ends up with a straggler. In the plays join, task 87 received the 12,540,000-row partition. The other 199 tasks each read 540,000 rows and finish in about 2 minutes, while task 87 works through 23 times as many rows at the same pace and needs about 46 minutes. These timings assume time grows in step with rows, which holds closely enough for a join like this.
What the straggler looks like while it runs
In an interactive shell, Spark prints a progress line for each running stage. For the join stage it reads like [Stage 4:===================>(199 + 1) / 200]: 199 tasks done, 1 running, 200 in total. At minute 2 the line reaches (199 + 1) / 200 and then stops moving. The bar looks 99.5% full while 44 of the stage's 46 minutes are still ahead.
| task | rows read | duration |
|---|---|---|
| 0 | 540,000 | 2 min |
| 1 | 540,000 | 2 min |
| 87 | 12,540,000 | 46 min |
| 199 | 540,000 | 2 min |
| 196 others | 540,000 each | 2 min each |
Spark records how many rows and bytes each task read alongside how long it ran, and those 2 numbers together identify a skew straggler. Task 87 read 23 times the rows of a typical task and took 23 times as long. A long duration paired with an oversized input points at the data. A long duration paired with a normal input points somewhere else.
Since Spark 3.2, Spark splits an oversized join partition by default when certain size conditions hold, a feature that dates from Spark 3.0. That feature and where it stops come in a later lesson; the timings here describe the join running without it.
Why the whole job waits on it
A stage is finished only when its last task is. The next stage, which totals royalties per rights holder, reads the join's output through another shuffle, so it cannot start until task 87 hands over its rows, and every stage after that waits in turn. Say the join stage starts at 05:15 and a dashboard reads the daily royalties table at 06:00. With balanced keys the join is done by about 05:17, and the table lands well before the dashboard looks. With task 87, the totals stage cannot start until 06:01, so the dashboard shows yesterday's royalties.
- plays (source, 120M rows)
- tracks (source, track catalog)
- join on track_id (transform, Spark stage); parallelism: 200 partitions
- total per rights holder (transform, Spark stage); parallelism: 200 partitions
- daily_royalties (storage, table); freshness: ready by 06:00
- plays -> join on track_id
- tracks -> join on track_id
- join on track_id -> total per rights holder
- total per rights holder -> daily_royalties
- Skew straggler
- Read many times the rows of its neighbours
- Slow again on every run, on any machine
- The same partition lags each time the data repeats its key mix
- Its rows share a single hot key
- Slow-machine straggler
- Read about the same rows as its neighbours
- Normal speed when the job runs on other machines
- Slowness stays with a host across tasks and runs
- Its rows look like any other task's
When the straggler fails
A task holds the rows it is working on in the memory of a single executor. A partition 23 times the usual size can outgrow the memory available to it, and the task fails with an out-of-memory error. Spark retries a failed task a few times before failing the job, and every retry receives the same 12,540,000 rows, because the hit track still hashes to partition 87. This is a failure a retry cannot fix: the cause sits in the data, and the data has not changed.
A stage has 100 tasks with a median duration of 90 seconds and a median input of 60 MB. Which task fits the signature of a skew straggler?
- Task 12: ran 31 minutes and read 4.1 GB, against the 60 MB median read.
- Task 40: ran 30 minutes and read 62 MB, close to the 60 MB median.
- Task 77: ran 95 seconds and read 64 MB, a little above the median read.
- Task 3: finished first, in 20 seconds, after reading 9 MB.
About this lesson
This section is part of the Data Skew: 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.