Problem
Networks time out, clients retry, and brokers redeliver. Without idempotency, these normal behaviors cause duplicate charges, repeated emails, and corrupted state.
Ensure that repeating an operation produces the same effect as executing it once.
Networks time out, clients retry, and brokers redeliver. Without idempotency, these normal behaviors cause duplicate charges, repeated emails, and corrupted state.
Idempotency is the property that makes retries safe. Because distributed systems cannot distinguish a lost request from a lost response, any operation that might be repeated must be designed to tolerate repetition.
The client generates a unique key, often a UUID, for each logical action and sends it in a header. On first receipt, the server atomically records the key as in progress, performs the work, and stores the response with the key. A repeated request with the same key returns the stored response without redoing the work. If a duplicate arrives while the first is still running, it waits or receives a conflict response. Requests that reuse a key with a different payload are rejected as misuse.
-- Atomic idempotent processing
BEGIN;
INSERT INTO idempotency_keys (key, request_hash, status)
VALUES (:key, :hash, 'in_progress')
ON CONFLICT (key) DO NOTHING;
-- if no row inserted, return stored response
INSERT INTO payments (id, amount) VALUES (:id, :amount);
UPDATE idempotency_keys SET status = 'done', response = :resp
WHERE key = :key;
COMMIT;
Teams adopt this approach when operations are retried by clients, gateways, or brokers; payments, orders, and other non-repeatable effects are involved. It is a poor fit when operations are naturally idempotent reads, or when duplicates are harmless and cheaper to tolerate than to prevent. The central tensions are storage cost vs duplicate protection window; strict locking vs throughput; server-side deduplication vs client discipline. The main benefits are that it delivers: makes retries safe; tolerates at-least-once delivery; simplifies client error handling; prevents costly duplicate side effects. The main costs to budget for are: requires key storage and expiry management; concurrent duplicate requests need locking or atomic checks; hard to apply to external side effects; clients must generate stable keys. Governance typically looks like this: API standards mandate idempotency keys for non-safe operations. Expect adoption cost to be low to medium: storage and API conventions, and a best fit for any team building networked or event-driven services. Critics point out that key scoping and expiry mistakes silently permit duplicates, and that idempotency of internal state does not extend to third-party side effects. Treat these concerns as design inputs, and revisit the decision as traffic, team size, and failure history change.