3. Exactly Once
- Meaning: Each message is delivered once and only once.
- No loss, no duplicates.
- Hardest to achieve in distributed systems.
How Kafka Does It:
- Kafka uses idempotent producers + transactional writes.
- This ensures:
- No duplicate writes from producers.
- Consumers commit offsets as part of the same transaction as processing.
Example:
- A payment system processes a message and commits offset in one atomic step.
- Even if the system crashes, Kafka guarantees no double charging.
Benefits
- No data loss and no duplicates.
- Strongest guarantee possible.
- Ensures perfect correctness in critical workflows.
Drawbacks
- Slower performance (transaction overhead).
- Higher complexity in configuration and operations.
- Not always necessary (often “at least once + idempotent consumer” is good enough).
Use Case:
- Financial transactions, inventory updates, or any workflow where duplication is unacceptable.
Diagram: Delivery Semantics
flowchart TD
A[Message Produced] --> B[Delivered to Consumer]
B -->|At Most Once| C[Processed 0-1 times<br/>Fast but risk of loss]
B -->|At Least Once| D[Processed >=1 times<br/>No loss but duplicates]
B -->|Exactly Once| E[Processed 1 time<br/>No loss, no duplicates]Quick Summary
| Semantics | Data Loss | Duplicates | Complexity | Use Cases |
|---|---|---|---|---|
| At Most Once | ✅ possible | ❌ none | Low | Logs, metrics |
| At Least Once | ❌ none | ✅ possible | Medium | Payments (idempotent), alerts |
| Exactly Once | ❌ none | ❌ none | High | Banking, inventory, critical data |
Conclusion
- At Most Once: simplest, but risky (data may be lost).
- At Least Once: safest default (no data loss, but handle duplicates).
- Exactly Once: strongest guarantee, but comes with extra complexity and cost.
In real-world systems, most teams use At Least Once with idempotent consumers (so duplicates don’t cause harm). Kafka’s Exactly Once is used only when absolutely necessary.
Pages: 1 2
Category: Kafka
