liveBook · Manning

Idempotency key

As explained in Think Distributed Systems

A client-supplied identifier that lets an API associate repeated requests with the same logical operation and return a consistent result.

An idempotency key is a client-generated UUID attached to an operation and every retry. The receiver uses it to recognize duplicates and return a consistent result. section 4.5

The key makes a normally non-idempotent operation, such as charging a credit card, behave consistently when messages are duplicated. Reliable deduplication requires recording the key transactionally with the side effect and enforcing uniqueness. section 4.5

Retrying an airline booking

If checkout times out, the app retries with the same reservation number as the key. A payment API can save the first status and response, then return that same result for later requests, including failures. section 4.6

Listing 4.1 Transactional recording and processing
requests := { }


on message request do

  if request in received then
    return response
  end
  begin transaction #1
    process(request) #2
    received = received + request  #2
  commit transaction
  return response
end

Follow the transactional flow: check whether the request was received, process it, record it, commit both together, and return the response. section 4.5

How Think Distributed Systems covers it

How deep it goes
Explains UUID assignment, retry reuse, transactional recording, uniqueness enforcement, and response reuse, but not implementation code beyond pseudocode.
Examples
Uses write-once registers, credit-card charges, airline ticket booking, reservation numbers, and Stripe API responses.
What it assumes
Assumes familiarity with requests, retries, responses, and transactions while motivating the problem through duplicated message delivery.
What you can do after
Apply idempotency keys to retryable payment or booking requests and reason about transactional receiver-side deduplication.
How it connects
Connects message delivery, retries, deduplication, transactional processing, UUIDs, and exactly-once processing semantics.

Also explained in

Compare how 2 books explain idempotency key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Think Distributed Systems. The section links point to the parts of the book it draws on.