principleCriticalpending
Principle: Idempotency for reliable distributed operations
Viewed 0 times
idempotencyretrydistributedduplicatereliabilityapi
Problem
In distributed systems, operations can be retried due to network failures, timeouts, or message duplication. Non-idempotent operations cause duplicate charges, double-sends, or data corruption.
Solution
Design operations so that executing them multiple times has the same effect as executing once:
POST /payments { idempotency_key: 'uuid-123', amount: 50 }
Server checks: already processed this key? Return cached result.
Bad: UPDATE balance SET amount = amount + 50 (not idempotent)
Good: UPDATE balance SET amount = 150 WHERE version = 3 (idempotent)
INSERT INTO payments (idempotency_key, amount)
VALUES ('uuid-123', 50)
ON CONFLICT (idempotency_key) DO NOTHING;
PUT /users/123 { name: 'Alice' } — safe to retry
POST /users { name: 'Alice' } — might create duplicates
Track processed message IDs, skip duplicates.
- Use idempotency keys for mutations:
POST /payments { idempotency_key: 'uuid-123', amount: 50 }
Server checks: already processed this key? Return cached result.
- Make operations naturally idempotent:
Bad: UPDATE balance SET amount = amount + 50 (not idempotent)
Good: UPDATE balance SET amount = 150 WHERE version = 3 (idempotent)
- Use unique constraints in the database:
INSERT INTO payments (idempotency_key, amount)
VALUES ('uuid-123', 50)
ON CONFLICT (idempotency_key) DO NOTHING;
- PUT is idempotent, POST is not (by convention):
PUT /users/123 { name: 'Alice' } — safe to retry
POST /users { name: 'Alice' } — might create duplicates
- Consumer-side deduplication for message queues:
Track processed message IDs, skip duplicates.
Why
Networks are unreliable. Any operation that can be retried must produce the same result regardless of how many times it runs.
Revisions (0)
No revisions yet.