Idempotency is a property of an operation that yields the same result regardless of execution count. Run once, run a hundred times — same effect.
Examples:
- Idempotent:
SET status='paid' WHERE order_id=X— regardless of how many times run, order becomes "paid" - NOT idempotent:
INSERT INTO emails (recipient) VALUES ('x@example.com')— run 10 times = 10 emails - Idempotent:
INSERT ... ON CONFLICT (email_id) DO NOTHING— call repeats but effect = 1 row
Why critical in automation: everything fails — network, containers, parsers. Without idempotency, a retry causes duplicates (10 emails to the same client) or corruption (3 card charges). With idempotency, retry is safe — you can run a task however many times.
Implementation patterns:
- Idempotency keys — unique ID per operation, checked before execution. Stripe API is the classic.
- Unique constraints in the database
- Hashing input — output cache per input hash
- Idempotent receivers on the target API side