Speculative execution
| Also known as | Speculation, speculative tasks, spark.speculation
|
|---|---|
| Field | Distributed data processing |
| Key parts | Straggler detection, duplicate task attempts, first attempt wins, output commit coordination |
| Open source implementations | Apache Spark |
| Related | Data skew, Accumulator, Exactly-once semantics, Executor |
In Apache Spark, speculative execution is a scheduler feature that launches a second copy of a task that is running much slower than the other tasks in its stage, on another executor, and uses whichever copy finishes first. It targets stragglers caused by a slow machine or disk, and is off by default (spark.speculation=false).[1] Because the same task can run twice, any side effect that is not idempotent can happen twice.
How does speculation decide which tasks to copy?
Spark checks for slow tasks every spark.speculation.interval. Speculation for a stage starts only after a fraction spark.speculation.quantile of its tasks has finished; then a running task is a candidate if it is spark.speculation.multiplier times slower than the median of the finished tasks and has run at least spark.speculation.minTaskRuntime.[1]
| Setting | Default | Since | Meaning |
|---|---|---|---|
spark.speculation |
false | 0.6.0 | Turn speculation on |
spark.speculation.interval |
100ms | 0.6.0 | How often to look for slow tasks |
spark.speculation.multiplier |
3 | 0.6.0 | How many times slower than the median a task must be |
spark.speculation.quantile |
0.9 | 0.6.0 | Fraction of tasks that must be done before speculating in a stage |
spark.speculation.minTaskRuntime |
100ms | 3.2.0 | Minimum run time before a task can be speculated |
spark.speculation.task.duration.threshold |
none | 3.0.0 | Speculate stages with few tasks once a task runs longer than this |
spark.speculation.efficiency.enabled |
true | 3.4.0 | Speculate only tasks that are inefficient by data processing rate or duration |
Source: Spark 4.3.0 configuration reference.[1]
The duration threshold helps stages that have no more tasks than one executor has slots, where the median rule has too few finished tasks to work with.[1] With spark.speculation.efficiency.enabled, a task is speculated only if its data processing rate is below the stage average times a multiplier, or if its duration exceeds the threshold times spark.speculation.efficiency.longRunTaskFactor.[1]
What happens when a speculative copy finishes?
When one attempt of a task succeeds, the scheduler kills the other running attempts of the same task, with the reason "another attempt succeeded".[2] The killed attempt may have already done part of its work.
For file output written through Spark's commit protocol, the output commit coordinator on the driver decides which attempt may commit each partition, with a "first committer wins" policy. This keeps two attempts from both committing their output files.[3] Table formats add their own commit on top. In Delta Lake, a table snapshot consists of the add actions in committed log entries, so a data file written by an attempt whose transaction never committed is not part of the table.[4]
Side effects with non-idempotent writes
Code that writes to an external system directly from tasks is outside the commit coordinator. With speculation on, two attempts of the same task can run that code at the same time, and the killed attempt may have written some or all of its data before it was stopped.[2] Examples are:
foreachorforeachPartitioncode that inserts rows into a database, posts to an HTTP API or sends messages to a queue;- UDFs with side effects, such as writing a log record per row;
- Accumulator updates inside transformations, which the Spark docs say may be applied more than once when tasks are re-executed.[5]
The same duplication can happen without speculation, from task retries, so code that writes externally should be idempotent anyway: use upserts or MERGE keyed on a stable ID, or write to a staging location and commit once from the driver. In Structured Streaming, foreachBatch gives only at-least-once write guarantees by default, and the docs suggest using the batch ID passed to the function to deduplicate output.[6] See Exactly-once semantics.
When to use speculation
Speculation helps when tasks are slow because of the machine they run on, for example a node with a failing disk or a noisy neighbour. It does not help with data skew, because a copy of a skewed task has the same amount of data and is just as slow. For skewed joins, Spark's Adaptive Query Execution can split large partitions instead.[7] In Spark 4.3.0 this applies to sort-merge and shuffled hash joins, and only to a side the join type allows to split: the left side of inner, cross, left semi, left anti and left outer joins, and the right side of inner, cross and right outer joins. It also applies to the streamed side of a broadcast hash join when spark.sql.adaptive.localShuffleReader.enabled is false. It does not split skewed aggregations, windows or full outer joins; for those, see salting.[8] Each speculative copy also uses a task slot that another task could have used.
spark.speculation true
spark.speculation.multiplier 3
spark.speculation.quantile 0.9
spark.speculation.minTaskRuntime 30s
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 Spark Configuration (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 TaskSetManager.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ OutputCommitCoordinator.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Delta Transaction Log Protocol: Action Reconciliation (v4.4.1). Delta Lake (Linux Foundation).Retrieved 2026-11-23.
- ↑ RDD Programming Guide: Accumulators (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Structured Streaming Programming Guide: APIs on DataFrames and Datasets (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ OptimizeSkewedJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.