Idempotency
Make a repeated request have one intended effect.
The problem and the pattern
A caller retries a booking after losing the response. Accept an idempotency key scoped to the tenant and operation, store a request fingerprint and result, and use a unique constraint to arbitrate concurrent requests. Reusing a key for different input should produce a documented conflict.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
UNIQUE (tenant_id, operation, idempotency_key)
Key exists + matching fingerprint → replay saved result
Key exists + different fingerprint → conflict
New key → commit effect and saved result togetherTrade-offs
- Idempotency reduces duplicate effects but requires durable state.
- Define key retention and response replay semantics.
- Do not put sensitive payloads in plain idempotency keys.
When to avoid it
Avoid claiming a GET-like read cache is sufficient protection for a state-changing operation.
Your turn
Two identical requests with one key arrive simultaneously. Why is an in-memory dictionary insufficient across two app instances?
Reveal a worked answer
Each process has its own dictionary and both can create a booking. Enforce uniqueness and commit the operation record with the business write in PostgreSQL. Test concurrency and recovery after process restart.