<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Speculative_execution</id>
	<title>Speculative execution - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Speculative_execution"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Speculative_execution&amp;action=history"/>
	<updated>2026-12-06T00:13:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Speculative_execution&amp;diff=203&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Speculative_execution&amp;diff=203&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:59Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Speculative execution&lt;br /&gt;
| also_known_as   = Speculation, speculative tasks, &amp;lt;code&amp;gt;spark.speculation&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Straggler detection, duplicate task attempts, first attempt wins, output commit coordination&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Data skew]], [[Accumulator]], [[Exactly-once semantics]], [[Executor]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/configuration.md, docs/rdd-programming-guide.md, docs/streaming/apis-on-dataframes-and-datasets.md, docs/sql-performance-tuning.md, core scheduler/TaskSetManager.scala, core scheduler/OutputCommitCoordinator.scala, sql adaptive/OptimizeSkewedJoin.scala) and the Delta Lake protocol at tag v4.4.1}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], &amp;#039;&amp;#039;&amp;#039;speculative execution&amp;#039;&amp;#039;&amp;#039; 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 (&amp;lt;code&amp;gt;spark.speculation=false&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;config&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/configuration.html |title=Spark Configuration (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Because the same task can run twice, any side effect that is not idempotent can happen twice.&lt;br /&gt;
&lt;br /&gt;
== How does speculation decide which tasks to copy? ==&lt;br /&gt;
Spark checks for slow tasks every &amp;lt;code&amp;gt;spark.speculation.interval&amp;lt;/code&amp;gt;. Speculation for a stage starts only after a fraction &amp;lt;code&amp;gt;spark.speculation.quantile&amp;lt;/code&amp;gt; of its tasks has finished; then a running task is a candidate if it is &amp;lt;code&amp;gt;spark.speculation.multiplier&amp;lt;/code&amp;gt; times slower than the median of the finished tasks and has run at least &amp;lt;code&amp;gt;spark.speculation.minTaskRuntime&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Setting !! Default !! Since !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation&amp;lt;/code&amp;gt; || false || 0.6.0 || Turn speculation on&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.interval&amp;lt;/code&amp;gt; || 100ms || 0.6.0 || How often to look for slow tasks&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.multiplier&amp;lt;/code&amp;gt; || 3 || 0.6.0 || How many times slower than the median a task must be&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.quantile&amp;lt;/code&amp;gt; || 0.9 || 0.6.0 || Fraction of tasks that must be done before speculating in a stage&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.minTaskRuntime&amp;lt;/code&amp;gt; || 100ms || 3.2.0 || Minimum run time before a task can be speculated&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.task.duration.threshold&amp;lt;/code&amp;gt; || none || 3.0.0 || Speculate stages with few tasks once a task runs longer than this&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.speculation.efficiency.enabled&amp;lt;/code&amp;gt; || true || 3.4.0 || Speculate only tasks that are inefficient by data processing rate or duration&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;small&amp;gt;Source: Spark 4.3.0 configuration reference.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt; With &amp;lt;code&amp;gt;spark.speculation.efficiency.enabled&amp;lt;/code&amp;gt;, 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 &amp;lt;code&amp;gt;spark.speculation.efficiency.longRunTaskFactor&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;config&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What happens when a speculative copy finishes? ==&lt;br /&gt;
When one attempt of a task succeeds, the scheduler kills the other running attempts of the same task, with the reason &amp;quot;another attempt succeeded&amp;quot;.&amp;lt;ref name=&amp;quot;tsm&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/scheduler/TaskSetManager.scala |title=TaskSetManager.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The killed attempt may have already done part of its work.&lt;br /&gt;
&lt;br /&gt;
For file output written through Spark&amp;#039;s commit protocol, the &amp;#039;&amp;#039;&amp;#039;output commit coordinator&amp;#039;&amp;#039;&amp;#039; on the driver decides which attempt may commit each partition, with a &amp;quot;first committer wins&amp;quot; policy. This keeps two attempts from both committing their output files.&amp;lt;ref name=&amp;quot;occ&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/scheduler/OutputCommitCoordinator.scala |title=OutputCommitCoordinator.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Table formats add their own commit on top. In [[Delta Lake]], a table snapshot consists of the &amp;lt;code&amp;gt;add&amp;lt;/code&amp;gt; actions in committed log entries, so a data file written by an attempt whose transaction never committed is not part of the table.&amp;lt;ref name=&amp;quot;delta&amp;quot;&amp;gt;{{Cite web |url=https://github.com/delta-io/delta/blob/v4.4.1/PROTOCOL.md#action-reconciliation |title=Delta Transaction Log Protocol: Action Reconciliation (v4.4.1) |publisher=Delta Lake (Linux Foundation) |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Side effects with non-idempotent writes ==&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;tsm&amp;quot; /&amp;gt; Examples are:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;foreachPartition&amp;lt;/code&amp;gt; code that inserts rows into a database, posts to an HTTP API or sends messages to a queue;&lt;br /&gt;
* UDFs with side effects, such as writing a log record per row;&lt;br /&gt;
* [[Accumulator]] updates inside transformations, which the Spark docs say may be applied more than once when tasks are re-executed.&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#accumulators |title=RDD Programming Guide: Accumulators (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The same duplication can happen without speculation, from task retries, so code that writes externally should be idempotent anyway: use upserts or &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; keyed on a stable ID, or write to a staging location and commit once from the driver. In [[Structured Streaming]], &amp;lt;code&amp;gt;foreachBatch&amp;lt;/code&amp;gt; gives only at-least-once write guarantees by default, and the docs suggest using the batch ID passed to the function to deduplicate output.&amp;lt;ref name=&amp;quot;ss&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html |title=Structured Streaming Programming Guide: APIs on DataFrames and Datasets (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; See [[Exactly-once semantics]].&lt;br /&gt;
&lt;br /&gt;
== When to use speculation ==&lt;br /&gt;
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&amp;#039;s [[Adaptive Query Execution]] can split large partitions instead.&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; 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 &amp;lt;code&amp;gt;spark.sql.adaptive.localShuffleReader.enabled&amp;lt;/code&amp;gt; is false. It does not split skewed aggregations, windows or full outer joins; for those, see [[salting]].&amp;lt;ref name=&amp;quot;skewrule&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedJoin.scala |title=OptimizeSkewedJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Each speculative copy also uses a task slot that another task could have used.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
spark.speculation                          true&lt;br /&gt;
spark.speculation.multiplier               3&lt;br /&gt;
spark.speculation.quantile                 0.9&lt;br /&gt;
spark.speculation.minTaskRuntime           30s&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Data skew]]&lt;br /&gt;
* [[Accumulator]]&lt;br /&gt;
* [[Exactly-once semantics]]&lt;br /&gt;
* [[Executor]]&lt;br /&gt;
* [[Dynamic allocation]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>