Idempotency
Idempotency is the property of an operation where performing it multiple times produces the same result as performing it once. In AI and software systems, idempotency is crucial for reliability — it ensures that retry logic, network failures, and duplicate requests don't cause unintended side effects.
Why Idempotency Matters
- Retry Safety: When a request fails and is retried, an idempotent operation can be safely re-executed without worrying about duplicate effects.
- Network Unreliability: In distributed systems, a client may not receive a response even though the server processed the request. Without idempotency, retrying creates duplicates.
- At-Least-Once Delivery: Message queues and event systems often deliver messages at least once — idempotent handlers prevent duplicate processing.
Examples
- Idempotent: Setting a value (`SET x = 5`), HTTP PUT (replace a resource), HTTP DELETE (delete a resource — deleting twice is the same as once).
- NOT Idempotent: Incrementing a value (`x = x + 1`), HTTP POST (creating a new resource — posting twice creates two resources), appending to a log.
Idempotency in AI Systems
- LLM API Calls: LLM inference is inherently idempotent for the same input (with temperature=0) — calling twice gives the same result without side effects.
- Database Writes: Use upsert (INSERT ON CONFLICT UPDATE) instead of plain INSERT to make writes idempotent.
- Payment Processing: Use idempotency keys to ensure a charge is only processed once even if the API call is retried.
- Event Processing: Deduplicate events using unique event IDs before processing.
Implementation Techniques
- Idempotency Keys: Include a unique request ID with each API call. The server checks if it has already processed that ID and returns the cached result.
- Upserts: Database operations that create or update based on whether the record exists.
- Deduplication: Track processed message IDs and skip duplicates.
- Conditional Updates: Use version numbers or ETags — only apply the update if the current version matches the expected version.
Idempotency is a foundational design principle for building reliable distributed systems — if an operation isn't idempotent, make it idempotent or handle duplicates explicitly.