Courses System design · Part 4 · Lesson 3
Delivery Guarantees and Idempotency Keys
In the message queue lab we saw that when a worker stops in the middle of a job, its message goes back to the queue. Good: nothing was lost. But one question remains: what if the worker had already done the job and stopped just before saying “done”? Then the message runs again. If the job is “send a welcome email”, the user gets two emails. If it is “charge the card”, the card is charged twice.
This lesson is about that problem.
Three levels of guarantee
At most once
The sender sends the message once and does not wait for a confirmation. If the message is lost on the way, it is gone. No message arrives twice, but some never arrive.
Fits: data where losing a few items does not matter, such as page view counts or a car’s live position that is sent again a second later.
At least once
The sender sends again until it gets a confirmation. No message is lost, but some arrive twice or more. Why? Because the confirmation can be lost too: the receiver got the message and did the work, but the sender never heard back, so it sends again.
Most queues and logs (RabbitMQ, SQS, Kafka) give this by default.
Exactly once
Every message has its effect exactly once. This is what everyone wants. But in a real network, where both messages and confirmations can be lost, a sender can never be sure whether the previous message arrived.
So in practice “exactly once” is built like this:
at-least-once delivery + processing that is safe to repeat = exactly-once effect
You cannot remove the repeats, but you can make them harmless.
Operations that are safe to repeat (idempotent)
An operation is idempotent if doing it again does not change the result.
| Operation | Safe to repeat? |
|---|---|
| “Set the balance to 500” | Yes: twice is still 500 |
| “Add 100 to the balance” | No: twice adds 200 |
| “Cancel order 881” | Yes: a cancelled order stays cancelled |
| “Send an email” | No: two emails arrive |
Some jobs are not idempotent by nature. For those there is a simple tool.
The idempotency key
The client makes a unique ID for each job before the first attempt, and sends the same ID with every retry. The server keeps the keys it has seen, with their results:
- A request arrives with key
pay-7f3a. The server has not seen it: it charges the card and stores “keypay-7f3a→ success, receipt 551”. - The answer is lost on the way. The client retries, with the same key.
- The server knows the key: it does not charge again, and returns the same “success, receipt 551”.
The result: the client can retry without fear, and the card is charged once.
Three important details
- Storing the key and doing the job must happen together. If the payment is made and the server crashes before it stores the key, the next attempt pays again. Usually both are written in one database transaction, or the key column gets a unique constraint.
- Two attempts at the same time. If the second attempt arrives before the first has finished, the server must mark the key as “in progress” and make the second one wait, or refuse it.
- Do not keep keys forever. A few days is usually enough; most retries happen in the first minutes.
In queues: the safe consumer
Workers on a queue use the same idea. Every message carries an ID (for example the order ID). Before doing the job, the worker checks: “Have I already done this ID?” If yes, it only confirms the message and moves on.
In the partitioned log, when a group moves its offset back and reads messages again, it is this safe consumer that prevents doing the work twice.
In an interview
- Whenever the design has a queue or retries, ask: “What happens if this message arrives twice?”
- For anything that changes money or stock, name the idempotency key.
- If the interviewer says “exactly once”, explain that it is built from “at least once + idempotency”.
Summary
- At most once: no repeats, but messages may be lost. At least once: nothing lost, but repeats may happen.
- Exactly once in practice means at-least-once delivery plus processing that makes repeats harmless.
- The idempotency key is the simplest tool for this.