liveBook · Manning

Idempotency

As explained in Think Distributed Systems

Idempotence is the property of an operation that applying it multiple times has the same effect as applying it once.

Idempotency means that applying an operation repeatedly has the same effect as applying it once: f(f(x)) = f(x). In distributed systems, this property makes duplicate message processing safe. Combined with at-least-once delivery and processing, it can provide exactly-once processing semantics despite retries. section 4.5 section 4.7

Idempotency is application-specific and end-to-end: both sender and receiver must preserve the guarantee. A write-once register is inherently idempotent because later writes change nothing; charging a credit card is not, because a duplicate request can create a second charge. section 4.5 section 4.6

A common retry-safe design follows these steps:

  • Generate a unique idempotence key, commonly a UUID, and attach it to the original request and every retry. section 4.5
  • Have the receiver recognize repeated keys and return the previously recorded result rather than process the request again. section 4.5
  • Record processing and request receipt transactionally so a crash cannot leave the system unsure whether the request was handled. section 4.5

Booking an airline ticket

A checkout request may time out after the payment processor charges the card, prompting the app to retry. Reusing one reservation-based idempotence key lets the processor return the original result instead of creating another charge. Without this protection, repeated app crashes can leave several pending charges. 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

Shows transactional recording and processing as a compact receiver-side deduplication pattern. section 4.5

How Think Distributed Systems covers it

How deep it goes
Defines the algebraic property, contrasts inherent and application-built cases, and outlines key-based transactional deduplication without offering a universal solution.
Examples
Uses write-once registers, credit-card charging, airline-ticket checkout, idempotence keys, and transactional request recording.
What it assumes
Assumes message delivery, processing, retries, and failures, while defining idempotence from the basic equation f(f(x)) = f(x).
What you can do after
Apply it via idempotence keys, receiver-side deduplication, and transactional recording when designing retryable distributed operations.
How it connects
Connects idempotency with at-least-once delivery, exactly-once processing semantics, message deduplication, transactions, retries, and failure recovery.

Also explained in

Compare how 17 books explain idempotency →

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.