Software Architecture / Interactions: distribution, failure, and operation / Lesson 14
Design idempotent request contracts
Design operation keys, consistency boundaries, and safe retry outcomes after a timeout.
On this page
Lesson goals
- Define operation identity, retry scope, and retention.
- Test crashes, concurrency, authorization, and the business effect.
Model uncertain completion
A client can time out after a server commits a state change but before the response arrives. Retrying without a contract can create a second reservation or payment. Define the user-visible operation, its owner, and the durable business invariant before choosing a retry mechanism. A network failure alone cannot reveal whether the original work committed; the design needs a way to identify and inspect that operation.
Define the operation key
An idempotency key can identify one logical request across repeated transmissions, provided the service defines its scope, lifetime, and relationship to the request payload. The synthetic request example shows the same key on a retry. It is not enough to generate a new key for every attempt, because the service would see different operations. Do not put secrets or personal data in a key that appears in logs.
POST /reservations HTTP/1.1
Idempotency-Key: lab-operation-42
Content-Type: application/json
{"item":"lab-item"}
The client reuses the key for the same logical request; behavior depends on the service contract.
Coordinate key and effect
The service must coordinate the keyโs durable record with the business effect so a crash cannot leave a completed effect with no usable deduplication record. Define behavior for concurrent identical requests, in-progress work, and reuse of the same key with a different payload. A cache-only record or an assumed atomic transaction across unrelated services may leave a gap. Document the actual consistency boundary and recovery path.
Return a meaningful result
Specify whether a repeated request returns the original result, a status reference, or a documented conflict, and ensure authorization is checked on every request. A key is an identifier, not permission to see another userโs result. The example state record illustrates in-progress and completed states without prescribing a storage engine. Define retention and what happens after expiry so clients understand the safe retry window.
key: lab-operation-42
request fingerprint: synthetic-value
state: in-progress | completed
result: stored reference when completed
The record illustrates possible information; it prescribes no implementation and claims no result.
Test failures and concurrency
In an isolated environment with synthetic data, send the same logical request twice, send concurrent copies, and simulate a lost response after commit. Verify that the intended business effect occurs once and that the user can learn the outcome. Also test key reuse with a different payload and the documented expiry boundary. Preserve error and latency evidence; exactly-once delivery is not required to design an idempotent effect.
Common errors and fixes
Generating a new key for every retry
- Why it happens
- Attempts lost their shared logical operation identity.
- How to fix it
- Reuse the key for the same request within the contract.
- Prevent it next time
- Document key lifecycle for clients.
Recording the key inconsistently with the effect
- Why it happens
- A crash left a gap between the record and business commit.
- How to fix it
- Design an explicit consistency boundary and recovery path.
- Prevent it next time
- Test crashes at each commit boundary.
Treating knowledge of the key as authorization
- Why it happens
- The requester and resource were not checked.
- How to fix it
- Enforce authorization for result access and every retry.
- Prevent it next time
- Separate identifiers from access control.
2 practice exercises
1. A reservation commits and its response is lost. How can the same key help?
Show worked solution
The service can associate the retry with the durable operation and return its status or result under the contract without creating another reservation.
2. A key is reused with different content. What is required?
Show worked solution
The contract must define mismatch detection and an appropriate response, such as a documented conflict, without an ambiguous effect or unauthorized disclosure.
Key takeaways
- Define operation identity, consistency boundary, and retry window.
- Test concurrency, crashes, authorization, and the business effect.