Idempotency & Safety
Two properties decide whether a request can be safely repeated. They are easy to mix up, so let’s pin them down precisely.
Safe vs idempotent
Section titled “Safe vs idempotent”- Safe — the request has no observable effect on server state; it only reads.
GET,HEAD, andOPTIONSare safe. - Idempotent — making the request N times leaves the server in the same state as making it once.
GET,HEAD,OPTIONS,PUT, andDELETEare idempotent.POSTand (in general)PATCHare not.
Every safe method is idempotent, but not every idempotent method is safe — DELETE changes state yet is idempotent.
flowchart TD
M{Method} --> GET[GET / HEAD: safe + idempotent]
M --> PUTDEL[PUT / DELETE: idempotent, not safe]
M --> POST[POST: neither]
M --> PATCH[PATCH: usually neither] Why it matters: retries
Section titled “Why it matters: retries”Networks drop responses. When a client (or proxy, or load balancer) does not hear back, it may retry. If the method is idempotent, retrying is harmless. If it is not — like a POST that charges a card — a blind retry could charge twice.
Making POST safe to retry
Section titled “Making POST safe to retry”When you genuinely need a non-idempotent create to tolerate retries, use an idempotency key: the client sends a unique Idempotency-Key header, and the server remembers the result for that key so a repeat returns the original outcome instead of acting twice. We cover the mechanics in Versioning & Caching.