System Design Academy

Saving…

Lesson 9 of 9 in Module 40 of 54 lessons complete

Delivery Guarantees & Idempotency

Module 4, lesson 9Concept12 min

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.

GuaranteeCan loseCan repeatCostExample
At-most-onceYesNo redeliveryLess retry workReplaceable status ping
At-least-onceNot within its stated durable retry guaranteesYesDurable storage and retriesSaved background job
Effectively onceDepends on delivery and storage guaranteesDeliveries yes; protected effects noAtomic repeat trackingMiles credit

The API atomically stores a credit and its key, loses its response, then returns the recorded result when the app retries.

A retry reuses a saved result instead of adding miles again.

The API atomically stores a credit and its key, loses its response, then returns the recorded result when the app retries.

A uniqueness rule rejects repeated keys within one caller.
SQL
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)
);
  1. Choose one action key

    Reuse the same key for retries of one intended action; use a new key for a new action.

  2. Save atomically

    For a local balance, store the key, balance change, and result in the same transaction.

  3. Handle concurrent requests

    Use the uniqueness rule to let only one transaction apply the effect; repeats read its saved result.

  4. 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.

Repeated requests keep the original credit effect.

Retry credit key abc → Read recorded result → No second credit.

The insight

Retries need protection against repeating business effects.

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

A payment request times out, and the client retries. What prevents a double charge?
Which is naturally idempotent?

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.

Try the quick check above whenever you feel ready.

Previous: Event-Driven Architecture