Delivery guarantees
Read first: Kafka, Idempotency and the exactly-once myth
Ack before the work finishes and a crash can drop the message for good. Ack after the work and a crash can run it twice. Exactly-once is a property of a protocol with a hard boundary, and that boundary ends at an email send or a card charge unless the outside system stores a key of its own.
A broker can promise something about redelivery. It cannot promise that your database, your mail provider, and a card network all observed one effect. The three delivery words are definitions of that boundary. They are not moods, and they are not a checkbox on a client library. Kafka already covered what acks means for the log itself. This page is what happens after a consumer has the record in memory.
Core concepts
Name the guarantee by the crash you can survive, and say which system is inside the sentence.
At-most-once. The message is handed to the handler zero times or one time. You get this in two ways. The producer sends and does not retry a timeout, so a lost response means it will never try again, even if the broker actually stored the record. Or the consumer acks before it processes, so the broker will not send the record again, even if the process dies before the work exists. Loss is allowed. A second delivery from the broker is not. That says nothing about a handler that itself calls a card network twice before it acks. The phrase is about the bus.
At-least-once. The handler sees the message one or more times. The producer retries until the broker confirms the append. The consumer acks only after the effect has landed. A timeout sits in the gap: the effect may have happened and the ack may have failed, so the broker sends the record again. Duplicates are normal. Loss, given a durable broker and a consumer that eventually runs, is what you are refusing. This is the setting you should assume unless you have written down a tighter protocol. The practical pair is at-least-once delivery plus a handler that can absorb the duplicate. That pair is Idempotency and the exactly-once myth. A natural idempotent write sets state, “the ledger row for 8841 exists with amount 40.” An imposed one stores a client-generated key and the outcome, and a retry returns the stored outcome.
Exactly-once is a protocol, and the protocol has an edge. Inside the edge, a retry does not create a second effect. Outside it, a retry might. There are a few real edges, and none of them is “the whole company.”
Kafka’s idempotent producer is one edge. A retried produce does not append a second copy of the same batch. The edge is that producer’s session and the log. A consumer that increments a counter is outside it.
Kafka transactions are a wider edge, still inside Kafka. A consume-transform-produce can commit the input offsets and the output records together. A consumer that only reads committed records does not see a torn write, and a restart does not append those output records twice. An email sent in the middle of that transaction is not recalled by an abort. A card capture is not recalled by an abort. The protocol cannot roll back a system that never enlisted.
A database transaction is an edge around one database. If the ledger insert and the “I have handled message 8841” row commit together, a crash retries the message and the second attempt finds the row. Effectively once into this database. If the inbox table lives in a different database from the ledger, you have two systems again and a crash between them. That hole is The outbox pattern and sagas.
A card network is an edge only if it stores your key. Stripe idempotency is the published shape of that idea. The client sends a key. The server stores the key with the outcome and, on a retry, returns the stored outcome instead of charging again. Brandur Leach’s account of the pattern adds the parts people skip. The key also stores a fingerprint of the parameters, so the same key with a different request is an error. Concurrent retries with the same key must lock, so two workers do not both run. Foreign side effects are separated by recovery points: record that you are about to call out, then call out, then record that you did. A local transaction can roll back. Once you have called out, you cannot roll that call back, so a retry continues from the recovery point instead of repeating the charge. Keys are a short-term tool, on the order of 24 hours in the published guidance, not a permanent archive.
Put those edges next to each other and the myth shrinks to a sentence. No client setting makes a database write and a third-party call into one exactly-once action. You get at-least-once on the wire, an idempotent handler at the database, and a stored key at each outside system you cannot roll back. Anyone who says “we enabled exactly-once” owes you the list of systems inside the protocol and the list left outside.
Ack timing is the whole consumer-side definition. Ack before the database commit and the broker’s view is at-most-once, whatever your slide says. Ack after the commit and the broker’s view is at-least-once. Commit the offset in the same transaction as the row and the database’s view is once, for that row. The worked example is the first of those, because it is the one that deletes money from the ledger while leaving the charge in place.
A worked example
Message m-8841 says a payment of 40 was captured. The consumer’s job is to insert one ledger row. The card network has already been asked to capture, using the idempotency key charge-8841, and it returned success. You are recording a fact that the outside world has already made true. Losing the message now means the books are wrong. Applying it twice means the books are wrong in the other direction.
Assumptions:
- 1 million such messages a day. A sketch.
- On the buggy build, 0.2 percent of attempts die after the ack has returned and before the database commit returns. An assumption chosen so the count is visible. It is not a measured failure rate.
- On the fixed build, the same 0.2 percent die after the commit returns and before the ack returns. Same assumption, other side of the line.
- The ledger table may or may not have a primary key on the message id. The example considers both.
A day has 86,400 seconds.
rate
1,000,000 / 86,400 = 11.6 messages per second
shortcut: 1,000,000 / 100,000 = 10 per second
the gap, under the 0.2 percent assumption
0.002 * 1,000,000 = 2,000 messages a day
Eleven or twelve messages a second looks too small to fail. Two thousand holes a day is a full-time reconciliation job. The rate is not what makes this safe. The crash window is.
The buggy order, which is at-most-once wearing an at-least-once costume:
1. read m-8841
2. ack m-8841 the broker forgets it
3. begin
4. insert ledger row
5. commit
crash anywhere after 2 and before 5 returns
redelivery: no
ledger row: no
card capture: yes, it already happened
The bus did what you asked. You told it the message was done, then you died. There is no second delivery coming. Support looks up 8841 and finds a charge at the processor and an empty ledger. The fix is not a retry setting on the consumer. The message is gone. The fix is a reconciler that can list captures the ledger does not know, which is a second pipeline you now have to run forever, plus a code change so the next crash does not add to the pile.
People hit this by accident. A client library commits offsets on a timer. The timer fires after the fetch and before the insert. A framework acks when the handler is scheduled, not when it returns. A teammate acks at the top of the function so a poison message cannot loop, and moves the insert underneath. The symptom is a low error rate and a ledger that drifts. Lag looks healthy, because the offset moved. That is the nasty part. The metric you trusted for “we are keeping up” is also the metric that says you deliberately skipped.
The order that matches at-least-once:
1. read m-8841
2. insert ledger row, primary key = m-8841
3. commit
4. ack
crash after 3 and before 4
redelivery: yes
with a primary key: the second insert hits the key, you ack, one row
without a primary key: a second row, a second credit of 40
Those 2,000 crashes a day become 2,000 redeliveries. With the key, they are 2,000 no-ops and 2,000 second acks. Without the key, they are 2,000 doubled credits. The unique key is the protocol edge, and it is one database wide. You must treat the conflict as success. If the handler logs the duplicate as a failure and refuses to ack, message 8841 becomes a poison record. The consumer retries it forever, lag sticks on that partition, and one payment fills the on-call channel. Idempotent means “the second time is a successful no-op,” not “the second time throws.”
There is a tighter edge, and it is still not the card. Insert the ledger row and insert an inbox row with primary key m-8841 in the same database transaction, then ack. A crash before the commit leaves neither row, and the redelivery retries cleanly. A crash after the commit is the duplicate case, and the inbox row makes the second attempt a no-op even if someone later “simplifies” the ledger schema and drops the uniqueness you were depending on. The ack can still double-fire. The transaction is what makes the double-fire harmless. Store that inbox in another database and you are back to a crash between two commits.
Now put the card back in the handler, because someone will “optimize” the flow into one function.
1. call the card network, key charge-8841
2. ack the bus
3. commit the ledger
If the crash is between 1 and 3, the recovery point is gone: the bus will not redeliver, and the ledger has no row. The card key would have saved you on a redelivery, and you arranged to have no redelivery. That is the worst combination in this page. Record that you are about to charge, charge, record that you did, commit the ledger and the inbox, then ack. A retry resumes at the recorded point. It does not invent a new idempotency key. A new key on each attempt is how a timeout that actually succeeded becomes a second charge. And if the retry might happen after the processor has forgotten the key, on the order of a day in the Stripe guidance, your own row is the memory that has to last. The processor’s cache is not the system of record for your saga.
flowchart TD
readBug[Read message 8841] --> ackBug[Ack before the commit]
ackBug --> crashBug[Process crashes]
crashBug --> gone[Bus will not redeliver]
flowchart TD
readOk[Read message 8841] --> insertOk[Insert row with primary key]
insertOk --> commitOk[Commit the database]
commitOk --> ackOk[Ack the bus after the commit]
The first diagram is how 2,000 payments a day go missing under the assumption above. The second is how those same crashes become duplicate reads of one row. Neither diagram includes the card network. Draw it as a separate box when you add it, and do not put it inside a commit you do not control.
Tradeoffs
| Choice | What you gain | What you spend |
|---|---|---|
| Ack before processing | No duplicate work from redelivery, and a poison message is easier to drop | A crash deletes the work. The offset moves past a record you never applied |
| Send and do not retry | The producer never double-appends | A lost response drops the record even if the broker stored it |
| Ack after a successful commit | Crashes retry. You do not lose the ledger row | The handler must tolerate duplicates, or the retry doubles the effect |
| Inbox row in the same transaction as the effect | A duplicate delivery is a key conflict, so the database applies once | The inbox has to live in that database, and you still ack at-least-once on the wire |
| Kafka transaction around consume and produce | Offsets and output records commit together, inside Kafka | Any call that left Kafka in the middle is not in the transaction |
| Idempotency key at the card network | A retried capture returns the first outcome | Key lifetime is short, concurrent retries need a lock, and a different payload with the same key must be an error |
| Reconcile the processor against the ledger | You can see the holes the ack bug created | You are admitting the path is lossy and paying for a second system to audit it |
The tradeoff to say in one line: moving the ack earlier reduces duplicates and creates losses; moving it later reduces losses and creates duplicates. You pick losses only for data you can drop, such as a metric sample. You do not pick losses for a payment record. Duplicates are acceptable exactly as far as the handler is idempotent, and no farther.
Pitfalls
- Trusting the client default. Many consumers commit offsets on an interval. Symptom: a crash during the insert skips a slice of messages, lag stays low, and the ledger total is short by that slice. Read the library’s ack point before you trust the word in the config file.
- Treating a duplicate-key error as a failed message. Symptom: one payment id retries until the attempt limit, then lands on a dead-letter queue, while the row was written the first time. The user is fine and the on-call is not. Catch the conflict and ack.
- Generating a fresh idempotency key per retry. Symptom: timeouts that were actually successes become second charges, and the ledger, if it only records the second response, does not even agree with itself. The key is chosen once, when the operation is created, and stored.
- Calling the card network and then acking before the local commit. Symptom: captures at the processor with no ledger row, and no message left to replay. This is the worked example. The recovery is a reconciliation job, not a consumer restart.
- Declaring exactly-once because the producer is idempotent. Symptom: the log has one copy of each batch, and the counter or the email still doubles, because the duplicate happened on the read side. Say the edge out loud.
- An inbox table in a second database. Symptom: the ledger commits, the inbox commit fails, the message is retried, and you insert a second ledger row because the inbox never recorded the first. Same database, same transaction.
- A retry budget that is longer than the key lifetime at the processor. Symptom: the retry on day two is a new charge, because the vendor forgot the key and you treated a stored-outcome miss as “never happened.” Persist the outcome yourself if the retry can outlive the vendor’s memory.
In the interview
Say the ack point in the first five minutes, before anyone asks for a product. “The consumer writes the ledger row, commits, then acks. A crash in between redelivers, so the primary key is the message id and a conflict is success. The card capture uses a key we generate once and store. I am not calling that exactly-once. It is at-least-once plus idempotent effects, and the card is outside the database transaction.”
The number is the size of the crash window, not a fantasy percentile. One million messages a day is about 12 a second. A fraction of a percent of those dying in the gap is thousands of bad rows a day. Small QPS does not make a wrong ack safe.
Skip a taxonomy lecture that never lands on this handler. Skip the claim that a bus feature covers the email. The question that usually follows is “what if we crash after charging the card and before writing the row?” Walk the recovery point: the row that says you are about to charge has to exist before the call, and the bus must not have been acked yet, so a retry can resume. If they ask about two services, you have left this page and entered the outbox. Say that the inbox and the business row share a transaction, and stop unless they want the saga.
Check yourself
The handler acks message 8841 and then crashes before the ledger commit returns. What will the bus do, and what does the ledger contain?
The bus will not redeliver. You already acked, so its promise is at-most-once from that moment. The ledger has no row for 8841. If the card was captured earlier in the path, the charge exists only at the processor. Lag does not show the hole, because the offset moved. You need a reconciler, and you need to move the ack to after the commit.
The handler commits the ledger insert and crashes before the ack. There is no unique key on the message id. What does the retry write?
The bus redelivers, because the ack never happened. That is at-least-once. The insert runs again and creates a second ledger row for the same payment. The duplicate is in the database, not in the log. A primary key, or an inbox row committed with the insert, turns the second attempt into a no-op you then ack.
The producer is idempotent and the topic is healthy. Why is that not a promise that the card was charged once?
The idempotent producer stops a retried append from appearing twice in the log. The card network is not in that protocol. A handler that calls the network, times out, and calls it again will capture twice unless the network stored an idempotency key and the handler reused it. Producer identity, consumer offsets, and the card key are three different edges.
You insert the ledger row and the inbox row in one database transaction, then ack. Where does the exactly-once claim stop?
It stops at that database. A redelivery finds the inbox row and does not apply a second effect there. The ack itself is still at-least-once on the wire. An email sent before the commit, or a charge that used a new key, sits outside the transaction and can still duplicate. Two databases, one for the inbox and one for the ledger, are also outside the claim, because a crash can separate those commits.
A retry of a capture generates a new idempotency key because the first call timed out. What outcome are you allowing?
The first call may have succeeded and lost the response. The second call is a new operation to the processor, so it can capture again. The key has to be chosen once and stored with the attempt. A timeout resumes that attempt. It does not mint a new charge. If the processor only remembers keys for about a day, a later retry has to consult your row, not assume a forgotten key means the charge never happened.
Based on: Designing Data-Intensive Applications, Kleppmann and Riccomini; Jay Kreps, Neha Narkhede, and Jun Rao; Brandur Leach, Stripe idempotency.