Inferensys

Glossary

Exactly-Once Semantics

A delivery guarantee in distributed systems ensuring that a message is processed only once, even in the face of failures, preventing data duplication or loss.
Developer building agentic RAG system, retrieval pipeline diagram on laptop, technical workspace with notes.
MESSAGE DELIVERY GUARANTEE

What is Exactly-Once Semantics?

A delivery guarantee in distributed systems ensuring that a message is processed only once, even in the face of failures, preventing data duplication or loss.

Exactly-once semantics is a message delivery guarantee in distributed streaming systems ensuring that each record is processed precisely one time, eliminating both data loss and duplication. It is the strongest consistency guarantee, achieved through a combination of idempotent producers, transactional writes, and checkpointing in stream processors like Apache Flink or Kafka Streams.

This guarantee is critical for stateful operations like financial transactions or inventory counts, where duplicates cause overcharging or stock discrepancies. It is implemented by atomically committing consumed offsets and output results to a state store, allowing the system to recover from failures by resuming from a consistent, non-duplicated checkpoint.

MESSAGE DELIVERY GUARANTEES

Key Characteristics of Exactly-Once Semantics

Exactly-once semantics (EOS) is a delivery guarantee ensuring that a message is processed only once, even in the face of failures, preventing data duplication or loss in distributed systems.

01

Idempotent Producers

The first pillar of EOS. An idempotent producer ensures that retrying a send operation does not result in duplicate records. The broker assigns a Producer ID (PID) and sequence number to each message. If a retry sends a message with a sequence number already committed, the broker discards it as a duplicate. This prevents the common 'at-least-once' duplication scenario where a producer retries after a network timeout without knowing the original write succeeded.

PID + Seq#
Deduplication Mechanism
02

Transactional API

The second pillar enabling end-to-end EOS across multiple partitions and topics. The transactional API allows an application to group multiple produce and consume operations into an atomic unit. Key properties:

  • Atomic multi-partition writes: Either all messages across partitions are committed, or none are visible.
  • Read-Process-Write: Consume from a source topic, transform, and produce to a sink topic exactly once.
  • Isolation: Consumers configured with read_committed isolation level see only committed messages, filtering out aborted transactions and duplicates.
Atomic
Multi-Partition Writes
03

Two-Phase Commit Protocol

The underlying mechanism for distributed transactions in streaming systems. The process:

  1. Begin Transaction: Producer registers a new transactional session.
  2. Write Phase: Messages are written to partitions but marked as 'pending'—invisible to read_committed consumers.
  3. Commit/Abort: The producer sends a commit marker to all involved partitions. This marker atomically flips pending messages to committed. If the producer crashes before committing, the transaction coordinator times out and aborts, preventing partial writes.
Commit Marker
Atomic Flip Signal
04

Failure Recovery & Zombie Fencing

EOS must handle producer failures gracefully. Zombie fencing prevents a crashed and restarted producer from corrupting state:

  • Each new transactional producer session receives a monotonically increasing epoch number.
  • If a zombie producer (with an older epoch) attempts to commit or send, the broker rejects the request with a ProducerFencedException.
  • The transaction coordinator uses this epoch to guarantee that only the latest active instance can finalize a transaction, ensuring correctness during failover.
Epoch Fencing
Zombie Prevention
05

Performance Overhead Considerations

EOS is not free. The guarantees impose measurable overhead:

  • Increased Latency: Transactional commits require additional round-trips to the transaction coordinator.
  • Reduced Throughput: Up to 20-30% throughput reduction compared to at-least-once semantics due to coordination and marker writes.
  • State Overhead: Brokers must maintain transaction state and pending markers in memory.
  • Best Practice: Use EOS only for critical financial, billing, or inventory pipelines where duplication is unacceptable. For analytics pipelines tolerant of rare duplicates, at-least-once with idempotent consumers is often sufficient.
20-30%
Throughput Reduction
06

End-to-End Exactly-Once with External Systems

Achieving true end-to-end EOS requires coordination beyond the streaming platform. When the sink is an external database or service:

  • Idempotent Writes: Design the sink to accept a unique key and perform an upsert, making duplicate writes harmless.
  • Two-Phase Commit Integration: Use connectors that implement a two-phase commit protocol with the external system (e.g., Kafka Connect with JDBC sink).
  • Outbox Pattern: Atomically write the event and update the business entity in the same local database transaction, then stream the event. This guarantees that the event is published if and only if the state change persists.
Upsert + 2PC
Sink Integration Pattern
MESSAGE PROCESSING GUARANTEES

Delivery Semantics Comparison

A technical comparison of the three fundamental message delivery guarantees in distributed streaming systems, outlining their mechanisms, failure modes, and performance characteristics.

FeatureAt-Most-OnceAt-Least-OnceExactly-Once

Core Guarantee

Messages are never duplicated but may be lost

Messages are never lost but may be duplicated

Messages are processed precisely one time with no loss or duplication

Data Loss Risk

High

None

None

Data Duplication Risk

None

High

None

Producer Acknowledgment

Fire-and-forget; no ack required

Ack required; retry on timeout

Idempotent producer with transactional writes

Consumer Offset Commit Strategy

Commit before processing

Commit after processing

Commit as part of atomic transaction with output

Failure Recovery Mechanism

None; lost messages are not retried

Uncommitted messages are re-delivered after consumer restart

State snapshot and idempotent replay from last checkpoint

Idempotency Required

Transactional Support

End-to-End Latency Overhead

Lowest

Moderate (retry overhead)

Highest (transaction coordination + deduplication)

Throughput Impact

Highest throughput

Slightly reduced due to retries

10-30% lower than at-least-once due to transactional overhead

Out-of-Order Handling

Not applicable

May cause duplicate side effects

Idempotent writes tolerate reordering

Use Case Suitability

Metrics, non-critical telemetry, best-effort logging

Most analytical pipelines, stateless ETL

Financial ledgers, inventory management, billing systems

Example Technologies

UDP, basic Kafka producer (acks=0)

Default Kafka consumer, RabbitMQ with manual ack

Kafka transactions, Apache Flink with two-phase commit

EXACTLY-ONCE SEMANTICS

Frequently Asked Questions

A technical deep dive into the mechanisms and trade-offs behind ensuring each record in a distributed streaming pipeline is processed precisely one time, eliminating the risks of duplication or data loss.

Exactly-once semantics is a delivery guarantee in distributed streaming systems ensuring that a message is both delivered and processed only once, even in the face of network failures or system crashes. It works by combining idempotent producers, which prevent duplicate writes to a log, with transactional APIs that atomically commit consumed offsets and produced output. This mechanism prevents the two common failure modes: at-least-once (duplication) and at-most-once (data loss). In practice, systems like Apache Kafka and Apache Flink implement this using a two-phase commit protocol over a distributed log, where a transaction coordinator manages the atomicity of read-process-write cycles.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.