Back to Tutorials

Exactly-Once Semantics in Distributed Systems

Idempotent operations, deduplication, two-phase commit, sagas, and consistency guarantees in distributed systems

70 minutes
6Detailed Sections
Senior Level

Distributed systems over unreliable networks must define delivery semantics for messages/requests. At-Most-Once: message delivered 0 or 1 times.

Implementation: no retries. Problem: lost messages acceptable?

Rarely. At-Least-Once: message delivered 1+ times.

Implementation: retry until ACK received. Problem: duplicates possible (network ACK lost, retry sent).

Exactly-Once: message delivered exactly 1 time. Implementation: hard (impossible to guarantee); use idempotent operations + deduplication instead.

Real-world: At-least-once common in practice (Kafka, RabbitMQ); exactly-once simulated via idempotency. Exactly-once semantics: define behavior such that duplicates don't cause problems (idempotent).

Example: payment (exactly-once required; paying twice = disaster) vs analytics (at-least-once acceptable; double-count is minor). Cost: exactly-once is expensive (deduplication, state management); use only when necessary.

Key Takeaways

1
At-Most-Once: Fire-and-forget; message may be lost; rarely acceptable
2
At-Least-Once: Retries ensure delivery; duplicates possible (most common)
3
Exactly-Once: Duplicates handled gracefully via idempotence; not guaranteed, simulated
4
Duplicate Window: Set timeout for deduplication cache (match deduplication + retry timeout)
5
Cost: Exactly-once semantics require state management and deduplication (expensive)
6
Real-world: Use at-least-once + idempotence for exactly-once semantics

Visual Diagram

Client -> Server: At-most-once (no retry), At-least-once (retry until ACK), Exactly-once (idempotent + dedup)

Sign in to unlock

Sign In Free