Thom's Blog

Response record

Failure pattern – Return the same response for every retry

Context

It can be important to return exactly the same response every time for the same request. Due to underlying state changes, a retried request might not naturally return the exact same response every time.

Example

The Stripe API models the status of a payout as a state machine with the following states: paid, pending, in_transit, canceled or failed. Some operations, e.g. cancel, are only valid when the payout in certain states. Retries of successful cancel requests should return the same successful response, even if the status of the payout has since changed.

Problem

How do we return the same response, even when the underlying state changes?

Solution

Before sending a response to the client, write it to a database. This could be indexed by an idempotency key. When handling a request, first check whether a response record exists for this request. If it does, simply return that response.

This is a special case of a recovery point.

See also

  • ACID transaction – Perform multiple writes, such that either all of them or none of them succeed
  • Idempotency key – Identify identical requests
  • Recovery point – Record current progress to allow recovery with minimal rework