At-most-once, at-least-once, exactly-once
A consumer crashes in the middle of a message. Is the message lost, handled twice, or neither? That depends on when you acknowledge it, and “exactly once” turns out to be something you build, not a setting you switch on.
01 Receive, work, acknowledge
Every consumer does three things with a message: it receives it, it does the work (writes a row, sends an email), and it acknowledges it so the broker can delete it. The broker only learns about the third. A crash can happen between any two, and the order you pick decides what a crash costs.
msg = queue.receive()queue.ack(msg)handle(msg) # crash here: lost
msg = queue.receive()handle(msg) # crash here: redeliveredqueue.ack(msg)
| Consumer crashes | Ack first (at-most-once) | Ack last (at-least-once) |
|---|---|---|
| Before the work starts | Lost: it was already acked | Delivered again; runs once |
| Halfway through the work | Lost, maybe half done | Delivered again; the done half repeats |
| After the work, before the ack | Done once (the ack came first) | Delivered again; runs twice |
02 How at-least-once doubles a top-up
Ana tops up her wallet by £20. The message m7 goes through a queue to a consumer that writes to the ledger and acks last. Watch what one badly timed crash does.
- just added
- in flight, hidden
- working on it
- crashed
- duplicate
- consumer free
Nothing has happened yet. The queue and Ana’s wallet ledger are empty.
Nothing here is a bug in the broker. It did what at-least-once promises: it kept the message until it heard an ack. The bug is a handler that is not safe to run twice.
03 Why the network cannot deliver exactly once
The broker sends a message and waits for an ack. No ack comes back. Either the message never arrived, or it arrived, the work was done, and the ack got lost. From the broker’s side these look exactly the same.
- If the broker resends, it may deliver twice: at-least-once.
- If it does not, it may deliver zero times: at-most-once.
- There is no third choice that is right in both cases. Waiting longer only narrows the gap, it never closes it.
So nobody really delivers exactly once over an unreliable network. What you can have is exactly-once effect: deliver at least once, and make the second delivery a no-op.
04 Exactly-once effect with a processed-ids table
Give every message a stable id, chosen once by the producer. In the consumer, record that id in the same database transaction as the work. A repeat then fails to insert the id, and you skip the work.
- just added
- in flight, hidden
- working on it
- crashed
- already processed
- consumer free
The same start, but the consumer now keeps a processed_messages table next to the ledger.
The id insert and the work must commit together. Check the table in one statement and write the ledger in another, without a shared transaction, and a crash between them, or two consumers racing, brings the duplicate straight back.
05 Effects outside your database
The trick needs the work and the id in one transaction. A card charge or an email is not in your database, so you cannot commit it with anything. Two options:
- Pass the id on. Many payment APIs take an idempotency key and return the first result for a repeat. Stripe, for example, reads an
Idempotency-Keyheader. Sendm7as the key and a redelivery cannot charge twice. - Accept a rare repeat. For an email, a duplicate once in a long while is usually cheaper than the machinery to prevent it. Decide this on purpose, per effect.
The producer has the mirror problem: it saves an order and publishes a message, and a crash between the two loses the message. The usual fix is an outbox: write the message into a table in the same transaction as the order, and let a separate process publish rows from that table, at least once.
06 What brokers mean by exactly-once
Some brokers advertise exactly-once. Each feature is real, but each covers a specific stretch of the path. Read what it leaves out.
| Feature | What it removes | What it does not cover |
|---|---|---|
| Kafka idempotent producer | Duplicate writes from producer retries, per partition | Duplicates your consumer creates |
| Kafka transactions | Duplicates in read-process-write between Kafka topics, with offsets committed in the same transaction | Writes to your database, emails, API calls |
| SQS FIFO deduplication | Repeat sends with the same deduplication id within a 5-minute window | A consumer that does not delete the message before its visibility timeout |
In Kafka, consumers must also read with isolation.level=read_committed to skip records from aborted transactions. Once your consumer touches anything outside Kafka, you are back to dedupe in your own store.
07 When the dedupe fails
DELETE FROM processed_messagesCleaning up too earlyThe ids table grows by one row per message. Expire old rows, but only well after the longest time a message can be redelivered, or a late repeat slips through.m7 can arrive after m8. If messages say “set balance to £40” rather than “add £20”, a late repeat can overwrite newer data. Prefer messages that are safe in any order, or carry a version number.