LESSON 01 · Idempotency keys

Make a retried payment safe

A key chosen by the client marks repeated attempts as one operation, and it protects you only if it is stored durably alongside the effect.

SYSTEM VIEW

The failure path

Diagram view
Charges—
Key row—
Client sees—

Watch a $40 charge lose its reply and get retried, first with no key, then with one key reused on every attempt.

  1. The Client sends POST /charges for $40. The Payments API charges it at the Payment provider, but the 200 OK never reaches the Client, which times out. From where the Client stands, the charge may have failed, or succeeded with the response lost.

    What changed

    • Provider charges $40
    • 200 OK lost
    • Client times out
  2. Not retrying might leave the order unpaid, so the Client sends the same $40 again. Nothing marks it as a repeat, so the Payments API charges again and the Payment provider bills the customer twice.

    Don’t retry
    order may be unpaid
    Retry blindly
    billed twice

    What changed

    • Second $40 charge
  3. Replay it with a key the Client makes once for this checkout, 7f3a, and sends on every attempt. The Payments API inserts the key row as in progress and commits, in the Database that holds the charge: a separate cache can lose or evict it.

    Unique on
    (account_id, key)
    Key
    one per checkout

    What changed

    • Key 7f3a
    • Key row in progress
  4. The Payments API calls the Payment provider with key 7f3a, so the provider can deduplicate too. The provider charges $40 and answers, but the Payments API crashes before saving the response. The key row still says in progress, and the Client times out.

    What changed

    • Provider charges $40
    • Payments API crashes
    • Key row still in progress
  5. On restart the row still says in progress. Recovery asks the provider what happened, or repeats the call with the same key, as here: the Payment provider sees 7f3a and returns the first result, with no second charge. The 200 is saved on the row.

    What changed

    • Payments API back up
    • Provider dedupes 7f3a
    • Key row: 200 saved
  6. Replay from the saved row. The Client sends key 7f3a again; the insert hits the unique constraint, so the Payments API returns the row’s saved 200 without calling the provider. Had the first attempt still been in progress, the retry would get a 409.

    Row saved
    replay its 200
    Still in progress
    409

    What changed

    • Retry with key 7f3a
    • Client gets the saved 200
  7. Key 7f3a now asks for $400. The row keeps the request’s hash, so the mismatch is rejected rather than replayed. The cost is a row per request and an in-progress state to recover. Setting a status to shipped is already idempotent: no key needed.

    Row holds
    key, hash, response
    Keep keys
    longer than retries

    What changed

    • Same key, $400
    • Rejected: hash differs

Space plays · ← → step · Home resets

THE IDEA

How it works

A client sends POST /charges for $40 and its connection times out. The charge may have failed, or succeeded with the response lost. Not retrying might leave the order unpaid. Retrying might bill the customer twice. An idempotency key resolves this: the client generates one unique value per logical operation, such as a UUID for this checkout, and sends the same value on every retry. Stripe’s API accepts one in an Idempotency-Key header.

Where the key lives decides whether this works. Keep it in the same database as the charge record, with a unique constraint on (account_id, key). A separate cache can lose or evict it, allowing a second charge. Insert the key row as in progress and commit; call the payment provider, passing the key so it can deduplicate too; then store the response on the row. A retry hits the unique constraint and gets the saved response, or a 409 while the first attempt is still running. If the server crashes after the provider call, recovery asks the provider what happened or repeats the call with the same key.

Also store a hash of the request, so the same key with a different amount is rejected rather than replayed, and keep keys longer than any client keeps retrying. The cost is a row per request and an in-progress state to recover. Already idempotent operations, such as setting a status to shipped, do not need one. Chat System uses this for retried sends, News Feed for retried posts, and Google Drive for retried upload finalization.

DECISION POINT

A checkout service keeps idempotency keys in Redis with a 24-hour TTL and writes orders to Postgres. During a Redis failover, the last second of writes is lost, and a client retries a successful order. How do you change the design?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Generate one key per logical operation and reuse it on every retry.
  • Store the key durably in the database that holds the effect, under a unique constraint.
  • Pass the key to external providers, since your database cannot cover their side.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A client generates a new UUID as its idempotency key for each HTTP attempt, and customers still get charged twice on retry. What is wrong?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.