Three delivery guarantees
What you'll learn:
- explain what each of the guarantees at-most-once, at-least-once and exactly-once promises and what it pays for that;
- choose a guarantee for a metric, an email and a payment;
- acknowledge a message at the right moment and predict what happens when a consumer crashes;
- make a consumer idempotent so that a repeated delivery breaks nothing.

Back to the courier service. A shop hands a parcel in at the warehouse, a courier takes it from the warehouse to the customer, and the customer signs for it. The parcel can go missing on the way, so the service decides in advance what to do when it is unclear whether the parcel arrived.
A message travels the same way: from a producer through a broker to a consumer.
A delivery guarantee is the system's promise about what happens to a message on a failure: whether it can be lost and whether it can arrive twice.
Where a message can go missing
The route has three legs, and something can break on each of them:
- Producer → broker: the network drops, and the producer does not know whether the broker stored the message.
- Broker: the server crashes before it has saved the message to disk.
- Broker → consumer: the consumer receives the message and crashes in the middle of processing.
Worst of all, the sender often cannot tell one failure from the other. The courier got no signature: was the parcel lost, or only the signature? From the outside both cases look the same.
So the choice comes down to two options. You can skip bringing the parcel again and risk losing it, or bring it again and risk the customer ending up with two parcels.
Three guarantees
At-most-once
At-most-once means "no more than once". The sender passes the message on once and does not retry. The courier comes once: if the customer is out, the parcel is gone.
A message can be lost, but it never arrives twice. This is the cheapest guarantee: it needs neither acknowledgements nor retries.
At-least-once
At-least-once means "at least once". The sender repeats the delivery until it gets a confirmation. The courier brings the parcel again and again until the customer signs.
A message is never lost, but it can arrive twice. If only the signature went missing, the courier brings a parcel the customer already has. A copy of a message that was already delivered is called a duplicate.
Most systems with brokers run on at-least-once.
Exactly-once
Exactly-once means "exactly once": a message is neither lost nor repeated. It sounds like what you always want, but this guarantee does not come free. To lose nothing, you still need retries. So someone has to recognize duplicates and drop them, and that costs extra writes and checks on every message.
In practice you usually build effectively-once instead, "exactly once in effect": a message may arrive twice, but the effect happens once. For that the consumer must be idempotent: processing the same message again changes nothing.
Kafka can do exactly-once, but only inside itself, when a program reads from Kafka and writes back to Kafka. As soon as the result leaves it, for example for your database, what remains is at-least-once plus a duplicate check.
Which guarantee to choose
The choice depends on what is worse for this message: a loss or a repeat.
| Message | Guarantee | Why |
|---|---|---|
| Metric "CPU load 40%" | at-most-once | a new value arrives a second later, and one missing point on the chart goes unnoticed |
| Email "your order has shipped" | at-least-once | losing the email is worse than sending it twice now and then |
| Payment | at-least-once with a duplicate check | you can neither lose the money nor charge it twice |
The acknowledgement sets the guarantee
A parcel ends with a signature, and a message delivery with an acknowledgement. In chapter 1 the consumer sent it, but there are acknowledgements at both ends of the route, and the guarantee depends on when they are sent:
- the producer gets an acknowledgement from the broker that the message is stored;
- the consumer acknowledges to the broker that it processed the message.
Each broker names these acknowledgements its own way, but they mean the same thing.
Without acknowledgements you get at-most-once: nobody knows whether the message arrived, and nobody repeats it. Acknowledgements together with retries give at-least-once.
Key points
- A message can go missing on any leg of the route: at the producer, in the broker and at the consumer.
- At-most-once can lose a message but never repeats it; at-least-once never loses it but can repeat it.
- Exactly-once never comes free: it is retries plus dropping duplicates.
- Acknowledgements decide the guarantee: the broker acknowledges the write to the producer, the consumer acknowledges the processing to the broker.