TL;DR
A delivery guarantee is the promise a messaging system makes about how many times a message reaches the receiver: at most once, at least once, or exactly once. Networks lose messages and replies, so a sender that wants a message to arrive must sometimes send it again. That creates a choice between losing messages and receiving duplicates, and the receiver's code has to be written for the choice the system made.
Builds on: Network Partition, Idempotent Operation · Leads to: Publish-Subscribe Messaging, the Kafka paper
Where it's used
Payment APIs. A card charge sent over the internet can time out before the reply comes back. The client does not know whether the charge happened, so it sends the request again. Payment providers ask clients to attach an idempotency key to each charge. A retry with the same key gets back the first result instead of charging the customer a second time.
Message brokers. A publish-subscribe system such as Apache Kafka lets a producer choose how hard to try. With retries on and no deduplication, a producer that gets no acknowledgement may write the same record twice. Kafka added idempotent producers and transactions in 2017 so that writes inside Kafka can be made exactly-once. Work a consumer does outside Kafka, such as updating a database, still needs its own protection.
Sensor readings. A temperature sensor that reports over a cellular link once a minute can accept at-most-once delivery. A lost reading is replaced by the next one a minute later, so resending wastes power and data. A bank transfer is the opposite case: losing it is not acceptable, so the sender retries until it is sure the transfer was received.
How it works
Take one tiny instance. A receiver keeps an account balance that starts at 0. The sender wants to add 20 and sends the request as message 1. The sender waits 2 seconds for an acknowledgement, a short reply that says "received". Replies can be lost too. In the asynchronous model a silent reply proves nothing: the sender cannot tell a lost message from a slow one.
- The receiver's balance is 0. The sender sends copy 1 of message 1, "add 20", with the id 1. Check: balance = 0.
- Copy 1 is lost in the network, so the receiver never sees it. Check: balance is still 0. A sender that stops here has delivered the message zero times. That is at-most-once.
- The sender follows at-least-once instead. After 2 seconds with no acknowledgement (a retransmission timeout), it sends copy 2 of message 1. This copy arrives. The receiver adds 20, so balance = 20, and sends an acknowledgement. Check: balance = 20.
- The acknowledgement is lost. After another 2 seconds the sender hears nothing, so it sends copy 3 of message 1. The sender still does not know that the work was done.
- Copy 3 arrives and the receiver adds 20 again: balance = 40. This acknowledgement arrives, and the sender stops. Check: three copies were sent, one was lost, two changed the balance, and the balance is 40 when it should be 20. This is at-least-once with no deduplication.
- The fix is for the receiver to remember which ids it has already applied. When copy 3 arrives it finds id 1 in its list, skips the addition, and sends the acknowledgement again. Check: balance = 20. The same three arrivals now give the right answer, and the effect of the message happened exactly once.
sequenceDiagram participant S as Sender participant R as Receiver S->>R: message 1, add 20 (lost) S->>R: message 1, add 20 (balance 20) R-->>S: acknowledgement (lost) S->>R: message 1, add 20 (balance 40 without deduplication)
The math
Let be the number of times the receiver applies a given message. Then:
- At most once: . The sender sends once and never retries.
- At least once: the sender retries until it hears an acknowledgement, so unless every copy is lost.
- Exactly once (in effect): the change to state happens one time, so , where the receiver counts only changes it made and ignores repeats it recognised.
Step 2 in symbols: the chance of a loss
Let be the probability that one copy is lost, with copies lost independently of each other. If the sender sends copies, the chance that at least one arrives is
At-most-once is the case , which gives .
Step 5 in symbols: counting arrivals
Let be the number of copies that arrive. Each copy arrives with probability , so by linearity of expectation
Without deduplication the receiver applies every arriving copy, so and the expected number of applications is .
Step 6 in symbols: deduplication
The receiver keeps a set of ids it has applied. It applies a copy only when that copy's id is not in . The change happens exactly once if at least one copy arrives, so
Deduplication removes the duplicates but cannot create a copy that never arrives. That is why exactly-once effect depends on retries as well.
Worked examples
- Each copy is lost with probability . The sender sends once, so .
- The chance that the message arrives is .
- Read it as at-most-once: half of the messages never reach the receiver, and nothing tells the sender which half.
- The sender retries until it hears an acknowledgement. Suppose it sends copies with .
- The chance that at least one copy arrives is .
- The expected number of arrivals is .
- With no deduplication and a charge of 20 dollars per arrival, the expected total charged is dollars, not 20.
- Interpretation: retries raise the chance of delivery from 0.5 to 0.875, and they also cost about half a copy of extra work on average.
- The client sends "charge 20 dollars" with the key pay-7781 and retries it after timeouts. Four copies are sent, and all four arrive. Check: four requests, one key.
- Copy 1: the server looks for pay-7781 and does not find it. It charges the card 20 dollars and stores the key with the result "charged 20", both in one database transaction. Both are saved or neither is. Check: 1 charge.
- Copies 2 to 4: the server finds pay-7781 and returns the stored result without charging again. Check: still 1 charge.
Try it: suppose the server skips the key table and charges on every arrival. How many charges happen, and what is the total? Answer: 4 charges, 4 times 20 dollars, which is 80 dollars.
Common mistakes and tradeoffs
It is tempting to think exactly-once delivery is a network feature that can be bought. In fact no sender can know, after a lost reply, whether the message or the reply was lost. What a system can achieve is exactly-once effect: the receiver applies the change once, using deduplication that is saved together with the change. The core problem is the unreliable link between the two machines, which the history below describes.
It is tempting to think duplicates are rare, so at-least-once is safe enough. In fact every retry after a lost reply creates a duplicate, and at scale losses happen every day, so duplicates arrive every day too.
It is tempting to think TCP already gives exactly-once delivery. In fact TCP removes duplicates and keeps order inside one connection. When a connection breaks, the application resends the request on a new connection, and the server sees the same request again. The application still needs an id.
It is tempting to save the dedup id and the change in two separate steps. In fact a crash between the two steps either applies the change twice or records nothing. Write both in one transaction.
When to use each: at-most-once suits cheap, repeatable data such as sensor readings, where the next message fixes a loss. At-least-once with idempotent handlers suits most business messages, because it is simple and fast. Exactly-once effect suits money and stock counts, and it costs storage for ids and extra latency per write. When several databases must change together, Two-Phase Commit is an alternative, but it can block when a participant fails.
History
In 1975 Akkoyunlu, Ekanadham and Huber wrote up the Two Generals' problem: two parties that communicate over a link that can drop any message cannot both be sure they agree on a decision. That result explained why no protocol can guarantee exactly-once delivery by itself. Engineers responded by building around it. Protocols such as Automatic Repeat Request retried until acknowledged, and at-least-once became the default. Duplicate handling was pushed to applications, which is why idempotent keys became common in web APIs. In 2017 Kafka added idempotent producers and transactions, so exactly-once processing became a feature of one streaming platform, while the consumer's own side effects still needed care.
Recap
- A message can be lost, and a sender that gets no reply cannot tell loss from delay.
- At most once: send once, never retry. The receiver gets zero or one copies. It is cheap and lossy.
- At least once: retry until acknowledged. The receiver gets one or more copies, so duplicates happen.
- Exactly once in effect: at least once, plus an idempotent operation or a stored id, saved in the same transaction as the change.
Read next: Publish-Subscribe Messaging for what a broker promises, and the Kafka paper for one design that stores topics as logs.
Check yourself
QuestionName the three delivery guarantees. Which one can leave a message undelivered?Show answer
At most once, at least once and exactly once (in effect). At most once can leave a message undelivered, because the sender never retries.
QuestionA link drops each copy with probability . The sender sends copies. What is the chance that at least one arrives?Show answer
. The answer is 0.9375.
QuestionWhy can a sender not achieve exactly-once delivery by itself?Show answer
The sender cannot tell a lost message from a lost reply. If it retries after a timeout, the receiver may already have applied the first copy. Exactly-once effect needs the receiver to recognise repeats, so the sender and receiver must cooperate.
QuestionA transfer API retries after timeouts. Which guarantee does the sender get, and what must the server do to avoid double transfers?Show answer
The sender gets at-least-once delivery, because it retries. The server must record each request id and return the stored result for a repeat, and it must save that record in the same transaction as the transfer.