Library
7 min read

At most once vs at least once vs exactly once

A queue hands a message to a consumer. The consumer does some work and tells the queue "done" (an ack), and the queue deletes the message. Everything interesting happens when the consumer crashes, or the network drops, somewhere in that sequence. The three delivery guarantees are just names for what you get when that happens:

  • At most once: a message is never processed twice, but may be lost.
  • At least once: a message is never lost, but may be processed more than once.
  • Exactly once: never lost, never repeated. Everyone wants this one, and over a network, as a delivery guarantee, nobody can have it.
Your answer

A consumer takes 'credit ₹100 cashback' off a queue, credits the wallet, and then crashes before it can ack. The queue redelivers the message to another consumer. What happens to the user's wallet, and how would you stop it?

Where the ack goes decides everything#

Credit ₹100 cashback from a queue message
  1. 1Deliver message m-81
  2. 2Credit ₹100 to user 42
  3. 3Consumer crashes
  4. 4No ack within the visibility timeout, so the queue hands it out again
  5. 5Deliver message m-81 again (delivery 2)
  6. 6Credit ₹100 to user 42 (again)
  7. 7Ack: queue deletes m-81
Paid 2 times
At least once. Nothing is ever lost, but a crash between the work and the ack means the work runs again. This is the default for most queues (SQS, RabbitMQ, Kafka consumers), so every consumer must expect duplicates.

Ack before processing gives at most once. Once acked, the queue forgets the message; a crash before the work is done loses it. Acceptable for data where losing a little is fine: metrics samples, presence pings, debug logs.

Ack after processing gives at least once. A crash before the ack means the queue never heard "done", so after a visibility timeout (SQS) or a consumer group rebalance (Kafka) it delivers the message again. Nothing is lost, and the work may run twice. This is the default behaviour of SQS, RabbitMQ with manual acks, Kafka consumers committing offsets after processing, Google Pub/Sub, and webhook senders. It is the one you will almost always use.