Safe and idempotent methods
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A phone loses signal just after the customer taps Pay. Did the order go through? Whether the app may send the same request again depends on two properties of each HTTP method.
Safe: the request changes nothing
Safe describes an HTTP method that only reads: the client asks for no change on the server. GET, HEAD and OPTIONS are safe, so they can be sent any number of times and the server data stays the same.
Two identical GET requests for product 6 return the same product, and the stock stays at 120.
Example 1 · GET twice: nothing changes
curl -s http://localhost:8080/api/v1/products/6curl -s http://localhost:8080/api/v1/products/6$ curl -s http://localhost:8080/api/v1/products/6
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}
$ curl -s http://localhost:8080/api/v1/products/6
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}What this code does
- Two identical GETs return identical products, and the stock stays at 120. Reading never changes the data.
- This is what makes GET safe to refresh, bookmark, cache and retry.
Safe does not mean nothing happens: the server may still write a log line or count a page view. What matters is that the client did not ask for a change and cannot be blamed for one.
Browsers, search engines and caches rely on this: they prefetch links, crawl pages and refresh GETs freely. A GET that deletes or buys something is therefore a serious bug.
Idempotent: twice has the same effect as once
Idempotent describes a method where sending the same request several times leaves the server in the same state as sending it once. PUT and DELETE are idempotent; the word comes from maths: doing it again changes nothing more.
A PUT sent twice, then a DELETE sent twice. Compare the state of the server, not the responses.
Example 2 · PUT twice: same state as once
curl -s -X PUT http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"price":1999.00,"stock":60,"isActive":true}'curl -s -X PUT http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"price":1999.00,"stock":60,"isActive":true}'$ curl -s -X PUT http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"price":1999.00,"stock":60,"isActive":true}'
{"id":7,"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"categoryName":"Accessories","price":1999.00,"stock":60,"isActive":true}
$ curl -s -X PUT http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"price":1999.00,"stock":60,"isActive":true}'
{"id":7,"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"categoryName":"Accessories","price":1999.00,"stock":60,"isActive":true}What this code does
- The first PUT changes the price from 2199.00 to 1999.00, so PUT is not safe.
- The second PUT sets the same values again. The product ends up exactly as after the first call, so PUT is idempotent.
Example 3 · DELETE twice: different answers, same state
curl -i -X DELETE http://localhost:8080/api/v1/products/14curl -i -X DELETE http://localhost:8080/api/v1/products/14$ curl -i -X DELETE http://localhost:8080/api/v1/products/14
HTTP/1.1 204 No Content
Date: Fri, 16 Oct 2026 02:37:27 GMT
$ curl -i -X DELETE http://localhost:8080/api/v1/products/14
HTTP/1.1 404 Not Found
Date: Fri, 16 Oct 2026 02:37:27 GMT
Content-Type: application/problem+json
Content-Length: 166
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Product 14 was not found.","instance":"/api/v1/products/14"}What this code does
- The first DELETE removes product 14, the pressure cooker nobody has ordered yet (
204). The second finds nothing to remove and answers404. - The responses differ, but the server state after one call and after two calls is the same: product 14 does not exist. Idempotent means same effect, not same response.
PUT is idempotent because "set product 7 to this exact version" gives the same product whether it arrives once or five times. DELETE is idempotent too: after the first delete the item is gone, and later deletes cannot make it any more gone.
Idempotent is about the effect on the server, not the response. The second DELETE of a product may answer 404 Not Found instead of 204, but the server state is the same: no product.
The table: which methods are which
A POST sent twice to the same URL. Each call adds another address.
Example 4 · POST twice: two new addresses
curl -s http://localhost:8080/api/v1/customers/1/addresses -H "Content-Type: application/json" -d '{"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059"}'curl -s http://localhost:8080/api/v1/customers/1/addresses -H "Content-Type: application/json" -d '{"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059"}'$ curl -s http://localhost:8080/api/v1/customers/1/addresses -H "Content-Type: application/json" -d '{"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059"}'
{"id":10,"customerId":1,"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059","isDefault":false}
$ curl -s http://localhost:8080/api/v1/customers/1/addresses -H "Content-Type: application/json" -d '{"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059"}'
{"id":11,"customerId":1,"label":"Hostel","line1":"Room 214, Boys Hostel, RVCE","city":"Bengaluru","state":"Karnataka","pincode":"560059","isDefault":false}What this code does
- The same POST sent twice creates address 10, then address 11. Each call adds, so the state after two calls differs from the state after one: POST is not idempotent.
- If this POST timed out and an app retried it blindly, the customer would find the same hostel address saved twice.
Every safe method is also idempotent, because doing nothing twice is still nothing. The reverse is not true:
GET,HEAD,OPTIONS: safe and idempotentPUT,DELETE: idempotent, not safe (they change data, but repeating adds nothing)POST: neither: two POSTs can create two orders or add two itemsPATCH: not guaranteed:{"stock":50}repeats safely, but "add 5 to stock" does not
Why it matters: retries, double clicks and refresh
Timeout is what happens when the client stops waiting for an answer. The client then does not know whether the server did the work: the order may be saved and only the answer lost.
A retried POST: the client never saw order 17, so it sent the request again. The stock then shows what happened.
Example 5 · A retried POST places two orders
curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":6,"quantity":1}]}'curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":6,"quantity":1}]}'curl -s http://localhost:8080/api/v1/products/6$ curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":6,"quantity":1}]}'
{"id":17,"customerId":1,"addressId":1,"status":"PLACED","orderedAt":"2026-10-16T08:07:29+05:30","items":[{"productId":6,"sku":"ACC-AD141","productName":"boAt Airdopes 141","quantity":1,"unitPrice":1299.00,"lineTotal":1299.00}],"total":1299.00}
$ curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":6,"quantity":1}]}'
{"id":18,"customerId":1,"addressId":1,"status":"PLACED","orderedAt":"2026-10-16T08:07:30+05:30","items":[{"productId":6,"sku":"ACC-AD141","productName":"boAt Airdopes 141","quantity":1,"unitPrice":1299.00,"lineTotal":1299.00}],"total":1299.00}
$ curl -s http://localhost:8080/api/v1/products/6
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":118,"isActive":true}What this code does
- Think of the second command as a retry after a timeout: the client never saw order 17, so it sent the request again.
- The server cannot tell a retry from a new order, so it creates order 18 and the stock drops by 2 (120 to 118). The customer pays twice.
Sending the request again is safe for idempotent methods and risky for POST, which can create a second order. The same problem comes from people: a double-click on "Pay", or a browser refresh after a form POST (browsers warn with "Confirm Form Resubmission").
HTTP libraries and proxies follow the same rule: many retry idempotent requests on their own after a broken connection, but they never retry a POST without being told to.
Find the bug
Find the broken promise
Tap the line that breaks the rule.
One of these GET requests is not safe. Which line?
Round 1 of 2Making POST safe to retry with an idempotency key
Idempotency key is a unique id the client creates for one action and sends in a header, usually Idempotency-Key. The server stores it with the first result, so a retry with the same key returns that result instead of repeating the work.
The same order POSTed twice with one key. The stock drops only once.
Example 6 · The same POST with an idempotency key
curl -i https://api.shop.example/v1/orders -H "Content-Type: application/json" -H "Idempotency-Key: 7f3c9a2e-41b8-4d0a-9c1e-5b2d8e6f1a40" -d '{"productId":3,"quantity":1}'curl -i https://api.shop.example/v1/orders -H "Content-Type: application/json" -H "Idempotency-Key: 7f3c9a2e-41b8-4d0a-9c1e-5b2d8e6f1a40" -d '{"productId":3,"quantity":1}'curl -s https://api.shop.example/v1/products/3$ curl -i https://api.shop.example/v1/orders -H "Content-Type: application/json" -H "Idempotency-Key: 7f3c9a2e-41b8-4d0a-9c1e-5b2d8e6f1a40" -d '{"productId":3,"quantity":1}'
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:37:32 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 72
Location: https://api.shop.example/v1/orders/503
{"id":503,"productId":3,"quantity":1,"total":69900.00,"status":"PLACED"}
$ curl -i https://api.shop.example/v1/orders -H "Content-Type: application/json" -H "Idempotency-Key: 7f3c9a2e-41b8-4d0a-9c1e-5b2d8e6f1a40" -d '{"productId":3,"quantity":1}'
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:37:33 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 72
Location: https://api.shop.example/v1/orders/503
Idempotent-Replayed: true
{"id":503,"productId":3,"quantity":1,"total":69900.00,"status":"PLACED"}
$ curl -s https://api.shop.example/v1/products/3
{"id":3,"sku":"MOB-IP15","name":"iPhone 15 128GB","price":69900.00,"stock":7}What this code does
- Both requests carry the same key. The first creates order 503; the second gets the saved answer back: the same order 503, marked with
Idempotent-Replayed: true. - The iPhone stock went from 8 to 7 only once. With the key, a retry after a timeout is now harmless.
A random UUID works well as a key. Two POSTs with one key give one order. Payment APIs such as Stripe work this way, and an IETF draft describes the header for every API.
The key belongs to the action, not the request: a new order gets a new key, and a retry of the same order reuses the old one.
Next step
Check your understanding
12 quick questions