Skip to content
Reliability & Integration

beginner · Interactive lab

Idempotency

The same request can arrive more than once. Learn how an idempotency key lets the receiver create the business effect only once, and why a correlation ID cannot do that job.

By the end: Tell apart logical operations, delivery attempts and business effects.

Your challenge

Start here. This lab opens with a common mistake, and it is wrong: the Warehouse API treats any correlation ID it has seen before as a duplicate, and anything else as new. Read each arrival, decide, and test.

See it happen

Deliver twice, create once

Warehouse: one order per operation
Slowed down so you can follow
ATTEMPTSANSWERSCREATESCLAIMS / CHECKSIntegration LogicWarehouse API● READYIDEMPOTENCY LEDGERORDERS CREATED
Integration Logic sends each order to the Warehouse API, possibly more than once. The Warehouse API claims or checks the order's idempotency key in the Idempotency ledger, creates the order in Orders created when it should, and answers Integration Logic.
  1. Integration Logic → Warehouse API: attempt
  2. Warehouse API → Idempotency ledger: claim / check
  3. Warehouse API → Orders created: creates
STEP 1 / 7

One order, one attempt

Integration Logic asks the Warehouse API to create order ORD-701: one logical operation, one delivery attempt. The Warehouse API creates it and answers 200 OK. One business effect.

Configure

Your receiver's decisions

For each arriving request, choose what the Warehouse API should do. Read the idempotency ledger, not the correlation ID.

What should the Warehouse API do with this request?
Claim the key, then create the order
· Record the operation as in progress first, then create it.
Replay the stored result
· Send the first answer again; create nothing.
Report it is still in progress
· Say it is being processed; start nothing new.
Reject as a conflict
· Refuse it; create nothing.
  1. ORD-701 · attempt 1 — First attempt

    Integration Logic sends ORD-701 for the first time.

    Ledger
    K-701 is not in the ledger
    Request
    key K-701 · 2 × SKU-44
    Correlation ID
    COR-11 · new here
  2. ORD-701 · attempt 2 — Retry after a lost answer

    The answer to attempt 1 was lost, so Integration Logic retries.

    Ledger
    K-701 is COMPLETED for 2 × SKU-44
    Request
    key K-701 · 2 × SKU-44
    Correlation ID
    COR-11 · seen before
  3. ORD-702 · attempt 2 — Retry while the first is still running

    Attempt 1 is still being processed when this retry arrives.

    Ledger
    K-702 is PROCESSING for 1 × SKU-10
    Request
    key K-702 · 1 × SKU-10
    Correlation ID
    COR-12 · seen before
  4. ORD-703 · attempt 2 — Same key, different request

    The caller reuses ORD-703's key for a changed order.

    Ledger
    K-703 is COMPLETED for 2 × SKU-44
    Request
    key K-703 · 5 × SKU-44
    Correlation ID
    COR-13 · seen before
  5. ORD-704 · attempt 1 — New order in the same checkout

    A second order in the same checkout as ORD-701.

    Ledger
    K-704 is not in the ledger
    Request
    key K-704 · 1 × SKU-90
    Correlation ID
    COR-11 · seen before
  6. ORD-701 · attempt 3 — Resent under a new trace ID

    A recovery job sends ORD-701 again and starts a new trace.

    Ledger
    K-701 is COMPLETED for 2 × SKU-44
    Request
    key K-701 · 2 × SKU-44
    Correlation ID
    COR-19 · new here
  7. ORD-702 · attempt 3 — Resent while the first is still running

    A recovery job sends ORD-702 again while attempt 1 is still running.

    Ledger
    K-702 is PROCESSING for 1 × SKU-10
    Request
    key K-702 · 1 × SKU-10
    Correlation ID
    COR-20 · new here

Two requests can share a correlation ID and still be different operations; a retry can carry a new correlation ID and still be the same one.

Learn more

Why this pattern exists

When an answer is lost, the caller retries, and the Warehouse API may receive the same request again. Three counts stop matching: logical operations (orders someone wants), delivery attempts (sends), and business effects (orders actually created). An idempotency key is a name for one logical operation: the caller creates it once and puts it on every retry, and the receiver keeps a ledger of the keys it has seen. A correlation ID has a different job: it traces a conversation across systems. One conversation can contain several different operations, and a retry sent by another process can start a new trace, so a correlation ID cannot tell a repeat from a new request.

Integration Logic sends orders to the Warehouse API, which keeps an Idempotency ledger and creates orders. Each graded case is one arriving request with a fixed ledger and correlation history. No real API is called.

  • Tell apart logical operations, delivery attempts and business effects.
  • Decide what a receiver does with an arriving request from its idempotency ledger: new, in progress, completed, or a conflicting reuse of the key.
  • Explain why a correlation ID is not duplicate protection, and why the key must be claimed before the business effect.

The rule this lesson applies: Idempotency does not make delivery exactly-once: attempts can still arrive more than once. It makes the business effect happen once for the protected operation. The receiver claims a new key before it creates anything: NEW becomes PROCESSING in one indivisible step (for example, an insert that fails if the key already exists), then the order is created, then the key is marked COMPLETED with its result. Checking the key, creating the order and only then writing the key is not enough: two attempts arriving together can both pass the check. A completed key with the same request is answered by replaying the stored result; a key still in progress starts nothing new; the same key with a different request is a caller mistake and is rejected, not guessed at. Real systems also decide how long to keep keys and what counts as the same request; this lesson keeps those fixed. Scope: this Foundations lesson assumes the idempotency ledger itself is reliable. Claiming the key first prevents the simple check-then-act race, but it is not by itself a complete crash-recovery solution: if the Warehouse API crashes after creating the order but before recording COMPLETED, that is a separate recovery problem. Production systems may address it with transactional coordination, reconciliation, leases with recovery, or related patterns; those mechanisms are outside this lesson.