Exactly-once semantics
| Also known as | Exactly-once processing, exactly-once delivery, effectively-once |
|---|---|
| Field | Stream processing, distributed systems |
| Key parts | Replayable source, checkpointed progress and state, idempotent or transactional sink |
| Open source implementations | Structured Streaming, Apache Flink, Kafka transactions and Kafka Streams (Apache Kafka), Delta Lake, Apache Iceberg, Apache Hudi |
| Related | Stream processing, Watermark, Streaming lakehouse |
In stream processing, exactly-once semantics 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 effectively-once.
Delivery guarantees
The Apache Kafka documentation defines three levels of guarantee:[1]
| Guarantee | Definition | Typical cause |
|---|---|---|
| At-most-once | Messages may be lost but are never redelivered | No retry after a failure |
| At-least-once | Messages are never lost but may be redelivered | Retry or replay without deduplication |
| Exactly-once | Each message is processed once and only once | Replay combined with idempotent or transactional output |
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).[2]
What it means and what it does not
Two guarantees are often confused, and the Spark Real-time Mode documentation separates them explicitly:[3]
- Exactly-once processing (or exactly-once state): each input record'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.[2]
- End-to-end exactly-once (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.[3]
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.
The end-to-end requirement
Engine documentation converges on three conditions:
- A replayable source. 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.[4][5]
- Recorded progress and deterministic processing. 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.
- An idempotent or transactional sink. 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.[5][2]
Implementations
Spark Structured Streaming
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.[4]
The guarantee depends on the sink. In the Spark 4.3 documentation the file sink is exactly-once, while the Kafka sink and foreach sink are at-least-once, and for foreachBatch it "depends on the implementation".[6] 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.[3]
foreachBatch gives only at-least-once writes by default. The Spark guide recommends using the batchId passed to the function to deduplicate output and get exactly-once.[6] Delta Lake supports this directly: with the txnAppId and txnVersion writer options, Delta identifies a repeated write of the same batch and ignores it.[7]
APP_ID = "orders-to-silver" # unique per streaming query
def write_batch(batch_df, batch_id):
(batch_df.write.format("delta")
.option("txnAppId", APP_ID)
.option("txnVersion", batch_id) # a replayed batch is skipped
.mode("append")
.save("/data/silver/orders"))
(orders.writeStream
.foreachBatch(write_batch)
.option("checkpointLocation", "/data/_checkpoints/orders-to-silver")
.start())
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.[7]
Kafka transactions and Kafka Streams
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 read_committed isolation level only see messages from committed transactions.[1] Kafka Streams uses these features: with processing.guarantee set to exactly_once_v2 (the default is at_least_once), it commits input offsets, state store updates and output records atomically. Exactly-once v2 requires brokers at version 2.5.0 or newer.[8]
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's offset in the same place as its output.[1]
Flink two-phase commit sinks
Flink takes consistent snapshots of operator state with checkpoint barriers, a variant of the Chandy-Lamport algorithm.[2] For external sinks, Flink 1.4.0 introduced TwoPhaseCommitSinkFunction: a sink pre-commits an external transaction when a checkpoint starts and commits it only after the checkpoint completes.[9] Current sinks expose a DeliveryGuarantee of NONE, AT_LEAST_ONCE or EXACTLY_ONCE.[5] With EXACTLY_ONCE, 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.[10]
Why table formats make good sinks
Open table formats 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.[7] The Apache Iceberg Flink sink guarantees exactly-once semantics, committing to the Iceberg table after each successful Flink checkpoint.[11] Apache Hudi lists exactly-once ingestion of new events from Kafka as a capability of its Hudi Streamer tool.[12] This is one reason the streaming lakehouse pattern writes streams directly into tables.
Where it breaks
- External side effects. Calls to APIs, emails or writes to systems outside the sink run again on replay. Only the sink's effects are protected.
- Non-idempotent
foreachBatchcode. A function that appends to a table or calls a service without usingbatchIdproduces duplicates when a batch is retried.[6] - At-least-once sinks. The Spark Kafka sink and
foreachsink are at-least-once, so downstream readers must tolerate or remove duplicates.[6] - Non-replayable sources. The Spark socket source, for example, does not provide end-to-end fault-tolerance guarantees and is meant only for testing.[6]
- Lost or reset checkpoints. The checkpoint holds the recorded progress. Deleting it or reusing identifiers incorrectly breaks deduplication, as the Delta
txnAppIdrule shows.[7] - Consumers outside the transaction. Exactly-once through Kafka transactions holds for consumers that read only committed messages (the
read_committedisolation level); the Flink Kafka sink documentation states the same condition.[1][10]
See also
References
- ↑ 1.0 1.1 1.2 1.3 Design: Message Delivery Semantics. Apache Software Foundation (Apache Kafka 4.4 documentation).Retrieved 2026-11-22.
- ↑ 2.0 2.1 2.2 2.3 Fault Tolerance via State Snapshots. Apache Software Foundation (Apache Flink 2.3 documentation).Retrieved 2026-11-22.
- ↑ 3.0 3.1 3.2 Real-time Mode (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 4.0 4.1 Structured Streaming: Getting Started (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 5.0 5.1 5.2 DataStream Connectors: Fault Tolerance Guarantees. Apache Software Foundation (Apache Flink 2.3 documentation).Retrieved 2026-11-22.
- ↑ 6.0 6.1 6.2 6.3 6.4 Structured Streaming: APIs on DataFrames and Datasets (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 7.0 7.1 7.2 7.3 Table streaming reads and writes. Delta Lake.Retrieved 2026-11-22.
- ↑ Kafka Streams Core Concepts. Apache Software Foundation (Apache Kafka 4.4 documentation).Retrieved 2026-11-22.
- ↑ Piotr Nowojski, Mike Winters.An Overview of End-to-End Exactly-Once Processing in Apache Flink (with Apache Kafka, too!). Apache Software Foundation, 2018-02-28.Retrieved 2026-11-22.
- ↑ 10.0 10.1 Apache Kafka Connector. Apache Software Foundation (Apache Flink 2.3 documentation).Retrieved 2026-11-22.
- ↑ Flink Writes (Apache Iceberg 1.12.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Streaming Ingestion (Apache Hudi 1.2.1). Apache Software Foundation.Retrieved 2026-11-22.