Sub topic 6
Delivery guarantees: at-most-once, at-least-once, effectively-once
This chapter assumes you know what a broker, a producer and a consumer are, and that you understand database transactions. It explains what a broker promises about delivering each event, and what that promise means for the code you write.
Why delivery is not simple
A broker and a consumer are separate processes connected by a network. Messages can be lost, and acknowledgments can be lost. A consumer can crash halfway through its work. The broker cannot tell whether the consumer finished or not, so it must choose between two risks: losing the event or delivering it again.
The choice depends on one detail: when the consumer acknowledges (acks) an event. An ack tells the broker "I am done, you can forget this event."
The three guarantees
At-most-once. The event is acked before it is processed, or it is never retried. Each event is handled zero or one times. If the consumer crashes after the ack, the event is lost. This fits data where a gap is harmless, such as a metrics sample or a heartbeat.
At-least-once. The event is acked after processing, and the broker redelivers anything that is not acked in time. Each event is handled one or more times. Nothing is lost, but duplicates happen. This is the default in most brokers and the usual choice for business events.
Exactly-once. Each event has exactly one effect. A network cannot give this for arbitrary side effects, because the "did it finish?" ambiguity cannot be removed. Systems that claim it limit the scope. Kafka transactions, for example, make reads, writes and offset commits atomic only inside Kafka.
Effectively-once. This is what you build in practice. You combine at-least-once delivery with a consumer that tolerates duplicates, so the end result is the same as if each event were handled once.
Comparing the guarantees
| Guarantee | Ack timing | Failure outcome | Consumer must |
|---|---|---|---|
| At-most-once | Before processing | Event lost | Accept gaps |
| At-least-once | After processing | Event duplicated | Handle duplicates |
| Effectively-once | After processing, with deduplication | No visible duplicate effect | Be idempotent |
Making duplicates harmless
An operation is idempotent if doing it twice has the same result as doing it once. Setting a status to shipped is idempotent. Adding 25 to a balance is not. The standard fix is to record the IDs of handled events and skip any repeat:
balances = {"acct-1": 100}
processed_ids = set()
def handle_deposit(event):
if event["event_id"] in processed_ids:
return # duplicate delivery: do nothing
balances[event["account"]] += event["amount"]
processed_ids.add(event["event_id"])
event = {"event_id": "e-42", "account": "acct-1", "amount": 25}
handle_deposit(event)
handle_deposit(event) # redelivered by the broker
print(balances["acct-1"]) # 125, not 150The next chapter covers idempotent consumers in more detail.
Common errors
- Acking first by accident. Auto-ack settings in some client libraries acknowledge on receipt. Your "at-least-once" system then behaves as at-most-once.
- Assuming the broker removes duplicates. Broker-side deduplication usually covers only the producer-to-broker hop, not the consumer's side effects, such as emails or calls to other services.
- Non-idempotent side effects. Sending an email or charging a card twice cannot be undone. Pass the event ID as an idempotency key to the downstream service where it supports one.
Exercise
A consumer reads an "invoice.paid" event, sends a receipt email, and then acks. It crashes after sending the email but before the ack. What happens on redelivery, and how do you make the outcome effectively-once?
Show answer
The broker redelivers the event because it never received the ack, so the consumer sends a second email. To get effectively-once behavior, record the event ID (or use it as an idempotency key with the email service) and skip events already handled. Ideally, record the ID in the same transaction as the state change, so a crash cannot separate the two.