Sub topic 5
At-most-once, at-least-once, and exactly-once delivery
This chapter assumes you know what a producer, a broker, a queue, and a consumer are. It also assumes you know that a consumer sends an acknowledgement (ack) after handling a message. A delivery guarantee describes how many times the consumer's code runs for each message the producer sends, when something fails along the way.
Where messages get lost or repeated
A network can drop a packet, a consumer can crash, and a broker can restart. After any of these, the sender often cannot tell whether the other side received or handled the message. It has two choices: send again, which risks a duplicate, or give up, which risks a loss. The three guarantees are three ways of making that choice.
The three guarantees
| Guarantee | Delivery count | When the ack happens | Cost |
|---|---|---|---|
| At-most-once | 0 or 1 | Before processing (or no ack at all) | Messages can be lost |
| At-least-once | 1 or more | After processing succeeds | Duplicates must be handled |
| Exactly-once | Exactly 1 effect | Needs extra coordination | Complexity, lower throughput, limited scope |
At-most-once
The broker treats a message as done as soon as it hands it over. If the consumer crashes mid-task, the message is gone. In RabbitMQ, setting auto_ack=True gives you this behavior. It suits data where a loss is harmless, such as a metrics sample that will be followed by another one a second later.
At-least-once
The consumer acknowledges only after it finishes. If it crashes first, the broker redelivers the message. Nothing is lost, but the same message can arrive twice. The consumer from the earlier chapter does this:
import pika
def process(body):
print("handling", body)
def handle(ch, method, properties, body):
try:
process(body)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.queue_declare(queue="tasks", durable=True)
ch.basic_qos(prefetch_count=10)
ch.basic_consume(queue="tasks", on_message_callback=handle, auto_ack=False)
ch.start_consuming()Here the ack on the success path is what makes this at-least-once. A crash inside process or between process and basic_ack leads to redelivery. The producer side has the same issue. If it publishes and the connection drops before it sees a confirmation, it will retry, and the broker may receive the message twice.
Most production systems use at-least-once, because losing work is usually worse than repeating it.
Exactly-once
True exactly-once delivery over an unreliable network is not achievable in general. The sender cannot distinguish a lost message from a lost acknowledgement. What systems offer instead is exactly-once effect (also called effectively-once). They combine at-least-once delivery with deduplication, so repeats do not change the outcome. Some brokers offer transactional features that do this inside their own boundary, for example consuming from one topic and writing to another. The guarantee usually stops at that boundary. An email sent or a payment charged by your consumer is outside it.
Choosing in practice
- Ask what a lost message costs. If the answer is "nothing important", at-most-once is fine and simple.
- If a loss is unacceptable, use at-least-once with manual acks.
- Make the handler safe to run twice. That is how you reach exactly-once effect, and the next chapter shows how.
Common errors
- Acknowledging at the start of the handler "to be safe". This silently turns at-least-once into at-most-once.
- Assuming the broker never duplicates. Redelivery after a crash, a timeout, or a lost ack is normal behavior.
- Trusting an exactly-once flag without reading its scope.
Exercise
A consumer reads a message, charges a customer's card, and then crashes before sending the ack. The broker uses at-least-once delivery. What happens next, and what must the consumer do to keep the customer from being charged twice?
Show answer
The broker sees no ack and redelivers the message, so the handler runs again and would charge the card a second time. The consumer must be idempotent: for example, record the message ID (or pass it as an idempotency key to the payment provider) so that the second run detects the duplicate and skips the charge.
Exercise
Which guarantee results from calling basic_consume with auto_ack=True, and why?
Show answer
At-most-once. The broker considers the message delivered as soon as it sends it, so if the consumer crashes during processing, the message is not redelivered and is lost.