<?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=Exactly-once_semantics</id>
	<title>Exactly-once semantics - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Exactly-once_semantics"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Exactly-once_semantics&amp;action=history"/>
	<updated>2026-12-06T00:08:09Z</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=Exactly-once_semantics&amp;diff=65&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@9783fe6</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Exactly-once_semantics&amp;diff=65&amp;oldid=prev"/>
		<updated>2026-11-22T21:29:53Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@9783fe6&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            = Exactly-once semantics&lt;br /&gt;
| also_known_as   = Exactly-once processing, exactly-once delivery, effectively-once&lt;br /&gt;
| field           = [[Stream processing]], distributed systems&lt;br /&gt;
| key_parts       = Replayable source, checkpointed progress and state, idempotent or transactional sink&lt;br /&gt;
| implementations = [[Structured Streaming]], [[Apache Flink]], Kafka transactions and Kafka Streams ([[Apache Kafka]]), [[Delta Lake]], [[Apache Iceberg]], [[Apache Hudi]]&lt;br /&gt;
| related         = [[Stream processing]], [[Watermark]], [[Streaming lakehouse]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-22|the Spark 4.3.0 Structured Streaming guides, the Apache Flink 2.3 documentation, the Apache Kafka 4.4 design and Streams documentation, the Delta Lake streaming documentation and the Apache Iceberg 1.12.0 Flink documentation}}&lt;br /&gt;
&lt;br /&gt;
In [[stream processing]], &amp;#039;&amp;#039;&amp;#039;exactly-once semantics&amp;#039;&amp;#039;&amp;#039; is the guarantee that each input record affects the result exactly once, with nothing lost and nothing duplicated, even when parts of the system fail and work is retried. Engines usually achieve it by replaying input after a failure and making sure the replay does not change the result twice, not by literally processing each record once. For this reason the guarantee is sometimes called &amp;#039;&amp;#039;&amp;#039;effectively-once&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Delivery guarantees ==&lt;br /&gt;
The Apache Kafka documentation defines three levels of guarantee:&amp;lt;ref name=&amp;quot;kafka-design&amp;quot;&amp;gt;{{Cite web |url=https://kafka.apache.org/44/design/design/ |title=Design: Message Delivery Semantics |publisher=Apache Software Foundation (Apache Kafka 4.4 documentation) |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Guarantee !! Definition !! Typical cause&lt;br /&gt;
|-&lt;br /&gt;
| At-most-once || Messages may be lost but are never redelivered || No retry after a failure&lt;br /&gt;
|-&lt;br /&gt;
| At-least-once || Messages are never lost but may be redelivered || Retry or replay without deduplication&lt;br /&gt;
|-&lt;br /&gt;
| Exactly-once || Each message is processed once and only once || Replay combined with idempotent or transactional output&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Apache Flink describes the same three outcomes for recovery: no effort to recover (at most once), nothing lost but possible duplicates (at least once), and nothing lost or duplicated (exactly once).&amp;lt;ref name=&amp;quot;flink-ft&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-stable/docs/learn-flink/fault_tolerance/ |title=Fault Tolerance via State Snapshots |publisher=Apache Software Foundation (Apache Flink 2.3 documentation) |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What it means and what it does not ==&lt;br /&gt;
Two guarantees are often confused, and the Spark Real-time Mode documentation separates them explicitly:&amp;lt;ref name=&amp;quot;ss-rtm&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/real-time-mode.html |title=Real-time Mode (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Exactly-once processing&amp;#039;&amp;#039;&amp;#039; (or exactly-once state): each input record&amp;#039;s effect on the state the engine manages, such as an aggregation count, is applied effectively once across failures and restarts. Flink states its guarantee in the same terms: every event affects the state managed by Flink exactly once.&amp;lt;ref name=&amp;quot;flink-ft&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;End-to-end exactly-once&amp;#039;&amp;#039;&amp;#039; (or exactly-once delivery): each record affects the external system, such as a table or topic, exactly once. This is a property of the sink as well as the engine.&amp;lt;ref name=&amp;quot;ss-rtm&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Exactly-once does not mean that user code runs once per record. After a failure, an engine re-runs the work since the last checkpoint, so functions, API calls and log lines in that work can run again. Only effects that pass through a sink with the right guarantees are deduplicated.&lt;br /&gt;
&lt;br /&gt;
== The end-to-end requirement ==&lt;br /&gt;
Engine documentation converges on three conditions:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;A replayable source.&amp;#039;&amp;#039;&amp;#039; After a failure the engine must be able to read the same data again from a recorded position, such as a Kafka offset. Spark assumes every source has offsets for this purpose; Flink requires that the source can rewind to the position stored in the checkpoint.&amp;lt;ref name=&amp;quot;ss-start&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/getting-started.html |title=Structured Streaming: Getting Started (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;flink-conn&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-stable/docs/connectors/datastream/overview/ |title=DataStream Connectors: Fault Tolerance Guarantees |publisher=Apache Software Foundation (Apache Flink 2.3 documentation) |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Recorded progress and deterministic processing.&amp;#039;&amp;#039;&amp;#039; The engine records what it has read, and its state, in a checkpoint, so a replay starts from a consistent point. If processing is not deterministic, a replay can produce different output from the first attempt.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;An idempotent or transactional sink.&amp;#039;&amp;#039;&amp;#039; Writing the same output twice must have the same effect as writing it once (idempotent), or output must become visible atomically together with the recorded progress (transactional). Flink states that the sink must commit transactionally or write idempotently.&amp;lt;ref name=&amp;quot;flink-conn&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;flink-ft&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementations ==&lt;br /&gt;
=== Spark Structured Streaming ===&lt;br /&gt;
{{Main|Structured Streaming}}&lt;br /&gt;
Structured Streaming uses checkpointing and write-ahead logs to record the offset range processed in each trigger, and its built-in sinks are designed to be idempotent so that reprocessing is safe. The Spark documentation states that replayable sources and idempotent sinks together give end-to-end exactly-once semantics under any failure.&amp;lt;ref name=&amp;quot;ss-start&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The guarantee depends on the sink. In the Spark 4.3 documentation the file sink is exactly-once, while the Kafka sink and &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; sink are at-least-once, and for &amp;lt;code&amp;gt;foreachBatch&amp;lt;/code&amp;gt; it &amp;quot;depends on the implementation&amp;quot;.&amp;lt;ref name=&amp;quot;ss-apis&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: APIs on DataFrames and Datasets (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; Real-time Mode keeps exactly-once processing, but because it commits offsets at the end of each batch, after records have reached the sink, a failure can re-write records; it does not yet ship an exactly-once sink.&amp;lt;ref name=&amp;quot;ss-rtm&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;foreachBatch&amp;lt;/code&amp;gt; gives only at-least-once writes by default. The Spark guide recommends using the &amp;lt;code&amp;gt;batchId&amp;lt;/code&amp;gt; passed to the function to deduplicate output and get exactly-once.&amp;lt;ref name=&amp;quot;ss-apis&amp;quot; /&amp;gt; [[Delta Lake]] supports this directly: with the &amp;lt;code&amp;gt;txnAppId&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;txnVersion&amp;lt;/code&amp;gt; writer options, Delta identifies a repeated write of the same batch and ignores it.&amp;lt;ref name=&amp;quot;delta-stream&amp;quot;&amp;gt;{{Cite web |url=https://docs.delta.io/delta-streaming/ |title=Table streaming reads and writes |publisher=Delta Lake |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
APP_ID = &amp;quot;orders-to-silver&amp;quot;   # unique per streaming query&lt;br /&gt;
&lt;br /&gt;
def write_batch(batch_df, batch_id):&lt;br /&gt;
    (batch_df.write.format(&amp;quot;delta&amp;quot;)&lt;br /&gt;
        .option(&amp;quot;txnAppId&amp;quot;, APP_ID)&lt;br /&gt;
        .option(&amp;quot;txnVersion&amp;quot;, batch_id)   # a replayed batch is skipped&lt;br /&gt;
        .mode(&amp;quot;append&amp;quot;)&lt;br /&gt;
        .save(&amp;quot;/data/silver/orders&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
(orders.writeStream&lt;br /&gt;
    .foreachBatch(write_batch)&lt;br /&gt;
    .option(&amp;quot;checkpointLocation&amp;quot;, &amp;quot;/data/_checkpoints/orders-to-silver&amp;quot;)&lt;br /&gt;
    .start())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the checkpoint is deleted and the query restarts with a new one, batch IDs start again from 0, so the application ID must change; otherwise Delta ignores the new writes as duplicates.&amp;lt;ref name=&amp;quot;delta-stream&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Kafka transactions and Kafka Streams ===&lt;br /&gt;
Since Kafka 0.11.0.0, the producer supports an idempotent mode that prevents duplicate log entries when a send is retried, and transactions that write to several topic partitions atomically. Consumers with the &amp;lt;code&amp;gt;read_committed&amp;lt;/code&amp;gt; isolation level only see messages from committed transactions.&amp;lt;ref name=&amp;quot;kafka-design&amp;quot; /&amp;gt; Kafka Streams uses these features: with &amp;lt;code&amp;gt;processing.guarantee&amp;lt;/code&amp;gt; set to &amp;lt;code&amp;gt;exactly_once_v2&amp;lt;/code&amp;gt; (the default is &amp;lt;code&amp;gt;at_least_once&amp;lt;/code&amp;gt;), it commits input offsets, state store updates and output records atomically. Exactly-once v2 requires brokers at version 2.5.0 or newer.&amp;lt;ref name=&amp;quot;kstreams&amp;quot;&amp;gt;{{Cite web |url=https://kafka.apache.org/44/streams/core-concepts/ |title=Kafka Streams Core Concepts |publisher=Apache Software Foundation (Apache Kafka 4.4 documentation) |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Kafka documentation notes that exactly-once delivery to other destination systems generally requires the cooperation of those systems; one approach is to store the consumer&amp;#039;s offset in the same place as its output.&amp;lt;ref name=&amp;quot;kafka-design&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Flink two-phase commit sinks ===&lt;br /&gt;
Flink takes consistent snapshots of operator state with checkpoint barriers, a variant of the Chandy-Lamport algorithm.&amp;lt;ref name=&amp;quot;flink-ft&amp;quot; /&amp;gt; For external sinks, Flink 1.4.0 introduced &amp;lt;code&amp;gt;TwoPhaseCommitSinkFunction&amp;lt;/code&amp;gt;: a sink pre-commits an external transaction when a checkpoint starts and commits it only after the checkpoint completes.&amp;lt;ref name=&amp;quot;flink-blog&amp;quot;&amp;gt;{{Cite web |url=https://flink.apache.org/2018/02/28/an-overview-of-end-to-end-exactly-once-processing-in-apache-flink-with-apache-kafka-too/ |title=An Overview of End-to-End Exactly-Once Processing in Apache Flink (with Apache Kafka, too!) |author=Piotr Nowojski, Mike Winters |publisher=Apache Software Foundation |date=2018-02-28 |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; Current sinks expose a &amp;lt;code&amp;gt;DeliveryGuarantee&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;NONE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;AT_LEAST_ONCE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;EXACTLY_ONCE&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;flink-conn&amp;quot; /&amp;gt; With &amp;lt;code&amp;gt;EXACTLY_ONCE&amp;lt;/code&amp;gt;, the Kafka sink writes records in a Kafka transaction committed on each checkpoint, so output becomes visible only after a checkpoint, and the Kafka transaction timeout must exceed the checkpoint and restart time to avoid data loss.&amp;lt;ref name=&amp;quot;flink-kafka&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-stable/docs/connectors/datastream/kafka/ |title=Apache Kafka Connector |publisher=Apache Software Foundation (Apache Flink 2.3 documentation) |access-date=2026-11-22}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Why table formats make good sinks ==&lt;br /&gt;
[[Open table format]]s commit each write as a single atomic change to table metadata, which is the transactional sink the end-to-end requirement asks for. A streaming engine can tie each commit to its own checkpoint, so a replayed batch either is detected as already committed or replaces an uncommitted attempt. Delta Lake states that its transaction log guarantees exactly-once processing even when other streams or batch jobs write to the same table concurrently.&amp;lt;ref name=&amp;quot;delta-stream&amp;quot; /&amp;gt; The [[Apache Iceberg]] Flink sink guarantees exactly-once semantics, committing to the Iceberg table after each successful Flink checkpoint.&amp;lt;ref name=&amp;quot;iceberg-flink&amp;quot;&amp;gt;{{Cite web |url=https://iceberg.apache.org/docs/latest/flink-writes/ |title=Flink Writes (Apache Iceberg 1.12.0) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; [[Apache Hudi]] lists exactly-once ingestion of new events from Kafka as a capability of its Hudi Streamer tool.&amp;lt;ref name=&amp;quot;hudi-streamer&amp;quot;&amp;gt;{{Cite web |url=https://hudi.apache.org/docs/hoodie_streaming_ingestion |title=Streaming Ingestion (Apache Hudi 1.2.1) |publisher=Apache Software Foundation |access-date=2026-11-22}}&amp;lt;/ref&amp;gt; This is one reason the [[streaming lakehouse]] pattern writes streams directly into tables.&lt;br /&gt;
&lt;br /&gt;
== Where it breaks ==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;External side effects.&amp;#039;&amp;#039;&amp;#039; Calls to APIs, emails or writes to systems outside the sink run again on replay. Only the sink&amp;#039;s effects are protected.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Non-idempotent &amp;lt;code&amp;gt;foreachBatch&amp;lt;/code&amp;gt; code.&amp;#039;&amp;#039;&amp;#039; A function that appends to a table or calls a service without using &amp;lt;code&amp;gt;batchId&amp;lt;/code&amp;gt; produces duplicates when a batch is retried.&amp;lt;ref name=&amp;quot;ss-apis&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;At-least-once sinks.&amp;#039;&amp;#039;&amp;#039; The Spark Kafka sink and &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; sink are at-least-once, so downstream readers must tolerate or remove duplicates.&amp;lt;ref name=&amp;quot;ss-apis&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Non-replayable sources.&amp;#039;&amp;#039;&amp;#039; The Spark socket source, for example, does not provide end-to-end fault-tolerance guarantees and is meant only for testing.&amp;lt;ref name=&amp;quot;ss-apis&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Lost or reset checkpoints.&amp;#039;&amp;#039;&amp;#039; The checkpoint holds the recorded progress. Deleting it or reusing identifiers incorrectly breaks deduplication, as the Delta &amp;lt;code&amp;gt;txnAppId&amp;lt;/code&amp;gt; rule shows.&amp;lt;ref name=&amp;quot;delta-stream&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Consumers outside the transaction.&amp;#039;&amp;#039;&amp;#039; Exactly-once through Kafka transactions holds for consumers that read only committed messages (the &amp;lt;code&amp;gt;read_committed&amp;lt;/code&amp;gt; isolation level); the Flink Kafka sink documentation states the same condition.&amp;lt;ref name=&amp;quot;kafka-design&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;flink-kafka&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Stream processing]]&lt;br /&gt;
* [[Structured Streaming]]&lt;br /&gt;
* [[Streaming lakehouse]]&lt;br /&gt;
* [[Watermark]]&lt;br /&gt;
* [[Delta Lake]]&lt;br /&gt;
* [[Apache Iceberg]]&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:Pipelines and orchestration]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>