Batch & Streaming Pipelines advanced 7 min read 12 flashcards

Exactly-Once Semantics, Precisely

What "exactly once" actually guarantees, why it is a statement about effects rather than about deliveries, and the two mechanisms that provide it.

Exactly-once delivery over an unreliable network is impossible, and every system claiming it is claiming something else. The claim that can be honoured is exactly-once processing: each input record affects the pipeline's state and output exactly once, no matter how many times it is delivered or how many times the job restarts. Understanding the difference is what prevents a team from believing a guarantee they do not have.

Why delivery cannot be exactly-once

A sender transmits and waits for an acknowledgement. The acknowledgement is lost. The sender cannot distinguish "the message was not received" from "the message was received and the ack was lost", so it must either retransmit, risking duplicate delivery, or not retransmit, risking loss. There is no third option, and this is a property of the network rather than of any implementation.

So the guarantee must be moved to effects. Deliver at least once and ensure that duplicate deliveries produce no additional effect.

The two mechanisms

Idempotent writes. Give each record a deterministic key and write with an operation that is naturally repeatable: an upsert keyed by that identifier, or a set assignment rather than an increment. Reprocessing overwrites with the same value and nothing changes. This is the simplest approach and it is only available when the sink supports keyed upserts and when the computation produces a deterministic key. Aggregations that accumulate do not fit, because an increment is not idempotent.

Transactional commits with checkpointed state. The pipeline periodically snapshots its state, and output writes are staged in a transaction committed atomically with the snapshot. On failure the job restores the last snapshot and reprocesses from the corresponding input offsets; the uncommitted transaction is aborted, so the reprocessed records produce the only committed output. Flink's checkpoint barriers and Kafka's transactional producer with read-committed consumers are the reference implementations.

The second mechanism is what people mean by exactly-once in a streaming engine, and it requires the sink to participate. A sink that cannot take part in a two-phase commit, which includes most HTTP endpoints and many databases as typically used, downgrades the guarantee to at-least-once regardless of what the engine provides.

When it breaks

The guarantee ends at the boundary. A pipeline with exactly-once internal state and an at-least-once sink is an at-least-once system. Guarantees are properties of a path, not of a component, and the weakest link sets the strength.

Non-determinism breaks replay. Reprocessing must produce identical results, so any use of wall-clock time, random numbers without a fixed seed, or iteration over an unordered collection makes the recomputed output differ from the original. The transaction commits successfully and the output is wrong, which is worse than a visible failure.

Side effects are not transactional. Sending an email, charging a card, or calling an external API cannot be rolled back by a checkpoint abort. Those actions need their own idempotency keys carried through to the external system, and the pipeline's guarantee does nothing for them.

Checkpointing has real cost. Snapshotting large state consumes IO and adds latency, and the checkpoint interval bounds how much work is redone after a failure. Short intervals mean constant overhead; long intervals mean expensive recovery. Transactional sinks also mean output is only visible at commit boundaries, so the checkpoint interval directly sets end-to-end latency, which surprises teams who tune it purely for recovery time.

Interpretation drifts between teams. "Exactly-once" is used loosely enough that it is worth restating precisely in a design document: which stages, which sinks, under which failure modes. Two teams agreeing they have exactly-once semantics and meaning different scopes is a routine source of production surprise.

Check yourself

12 flashcards for this concept

Click a card to reveal the answer.

Drill the whole track