At-Least-Once Delivery
What you'll learn: How message brokers guarantee delivery by potentially sending messages multiple times, and why your consumers must handle duplicates.
The Core Guarantee
At-least-once delivery means the message broker guarantees every message will reach the consumer, but might deliver the same message more than once. This happens when the broker isn't certain whether delivery succeeded—for example, if a consumer acknowledges receipt but the acknowledgment gets lost in the network.
The broker plays it safe: "I'm not 100% sure you got it, so I'll send it again."
Why Duplicates Happen
Consider this sequence:
- Broker sends message to consumer
- Consumer processes it successfully
- Consumer sends acknowledgment back
- Network hiccup—acknowledgment never reaches broker
- Broker assumes failure, resends the message
- Consumer receives the same message again
The broker chose availability over exactness. Rather than risk losing a message, it risks sending duplicates.
The Idempotency Requirement
Since duplicates are inevitable, your consumer must be idempotent—processing the same message multiple times should have the same effect as processing it once.
Bad (not idempotent): balance = balance + 100 (running twice adds $200!)
Good (idempotent): if transaction_id not in processed_set: balance += 100; mark transaction_id as processed
Real-World Analogy
Think of a worried parent texting "Did you get home safe?" If you reply but they don't see it (bad signal), they'll text again. You receive the question twice—but answering "yes" multiple times doesn't change reality. Your response is naturally idempotent.
Compared to At-Most-Once
With at-most-once delivery (lesson 678), you risked losing messages. With at-least-once, you risk duplicate messages but never lose them. Most systems prefer duplicates over data loss.
Key Takeaway: At-least-once delivery guarantees no message loss but requires idempotent consumers to handle inevitable duplicates safely.