Delivery Guarantees & Idempotency
Definition
Delivery guarantee
- In plain English
- A delivery guarantee is a messaging system's promise about whether each message arrives at most once, at least once or effectively once.
- Think of it like
- It is like a courier promising either "maybe delivered", "delivered, possibly twice" or "delivered exactly once with checks".
Idempotency
- In plain English
- An operation is idempotent when doing it twice has the same effect as doing it once.
- Think of it like
- It is like pressing a lift's call button repeatedly — it still comes once.
At-most-once and at-least-once
- In plain English
- At-most-once delivery never repeats a message but can lose it, while at-least-once never loses a message but can repeat it.
- Think of it like
- Like a postcard with no tracking versus a courier who redelivers if unsure.
Effectively-once processing
- In plain English
- Effectively-once processing allows repeated message deliveries while ensuring that a protected business effect is applied only once.
- Think of it like
- It is like accepting repeated order slips but shipping only one parcel for that order.
Why it exists
A partner credits miles, but its response disappears. The partner retries because it cannot tell whether the credit succeeded.
Without a repeat check, the same action credits miles twice. A queue can cause the same problem after an acknowledgement disappears.
How it works
Idempotency key
- In plain English
- An idempotency key is a unique ID sent with a request so the server can recognise and ignore repeats.
- Think of it like
- Like an order number the shop checks before shipping again.
At-least-once only works if the message is safely stored and someone keeps retrying until it succeeds. A tool that promises exactly-once can only keep that promise inside its own system, not for emails or card charges it triggers.
| Guarantee | Can lose | Can repeat | Cost | Example |
|---|---|---|---|---|
| At-most-once | Yes | No redelivery | Less retry work | Replaceable status ping |
| At-least-once | Not within its stated durable retry guarantees | Yes | Durable storage and retries | Saved background job |
| Effectively once | Depends on delivery and storage guarantees | Deliveries yes; protected effects no | Atomic repeat tracking | Miles credit |
The API atomically stores a credit and its key, loses its response, then returns the recorded result when the app retries.
CREATE TABLE processed_requests (
caller_id TEXT NOT NULL,
idempotency_key TEXT NOT NULL,
request_body TEXT NOT NULL,
result TEXT NOT NULL,
UNIQUE (caller_id, idempotency_key)
);- Choose one action key
Reuse the same key for retries of one intended action; use a new key for a new action.
- Save atomically
For a local balance, store the key, balance change, and result in the same transaction.
- Handle concurrent requests
Use the uniqueness rule to let only one transaction apply the effect; repeats read its saved result.
- Check request identity
Reject a reused key with different request data; retain keys beyond the expected retry window.
A unique key alone cannot protect an external card charge. Pass the key to a payment provider that enforces it, and reconcile unknown outcomes.
Examples
Airline loyalty
A partner retries a 2,000-mile credit after a timeout. Reusing the idempotency key returns the original result without crediting again.
Store the 2,000-mile credit and its key in one transaction. The retry returns the recorded result.
Retry credit key abc → Read recorded result → No second credit.
The insight
Retries need protection against repeating business effects.
Bakery orders
A customer double-taps Pay for one order. Both attempts use the same idempotency key, so protected processing charges once.
Both taps represent one intended payment. Reuse the key through your server and payment provider rather than generating one per tap.
POST /payments HTTP/1.1
Host: bakery.example
Idempotency-Key: order-841-payment-1
Content-Type: application/json
{"order_id":"841","amount_pence":1000}The insight
Protect user-facing actions from double submits.
City traffic lights
Monitoring-only traffic-light status pings arrive every second using at-most-once delivery. Control commands use a separate safety-reviewed path.
An occasional missing monitoring sample is acceptable for this display. A network outage can lose many samples, not only one.
- Display when the last sample arrived.
- Mark the display stale when updates stop; do not use it to control signals.
The insight
Not everything needs strong guarantees; match the guarantee to the cost of loss.
Trade-offs
Pros
- Retry protected actions safely.
- Prevent repeated credits or charges.
- Return consistent saved results.
Cons
- Store keys and results.
- Define retention and request matching.
- External effects need provider cooperation.
Use when
- Money or credits must not repeat.
- Consumers receive at-least-once messages.
Avoid when
- Extra keys may be unnecessary for a naturally idempotent set operation.
- Extra tracking may be unnecessary for harmless duplicate monitoring samples.
Quick check
Takeaways
- Messages can be lost or delivered repeatedly.
- At-least-once delivery plus atomic idempotent processing can give effectively-once effects.
- Protect external effects with matching provider guarantees.