Idempotency in REST APIs: Safe to Retry?

An idempotent API request means sending it once or 100 times has the same effect on the server's state. GET, PUT, and DELETE are idempotent, making them safe to retry. POST is not, so retrying can create duplicates.
Why it exists
Networks are unreliable. A client might send a request, but the connection drops before the response arrives. The client doesn't know if the server processed the request. Idempotency provides a contract that allows the client to safely retry certain operations without causing unintended side effects.
The mental model
Think of idempotency as the difference between setting a value and incrementing it. Setting a thermostat to 70 degrees is idempotent; you can do it once or ten times, and the final temperature setting is still 70. Pressing the "+" button ten times is not; each press changes the state. In APIs, PUT /thermostat/temp with a body of {"value": 70} is idempotent. POST /thermostat/increment is not.
How it works
The HTTP specification defines the expected behavior for its methods. Safe, read-only methods like GET, HEAD, and OPTIONS are inherently idempotent. PUT is idempotent because it's defined as "create or replace"; sending the same PUT request multiple times just overwrites the resource with the same data. DELETE is idempotent because after the first successful request, the resource is gone, and subsequent requests do nothing to change that state. In contrast, POST and PATCH are not idempotent. A POST typically creates a new resource, so multiple requests create multiple resources.
When to use it
You rely on idempotency when building resilient clients. If a request for an idempotent method (PUT, DELETE) times out or fails with a network error, you can automatically retry it. This is fundamental to building reliable distributed systems where transient network failures are common. The server is responsible for implementing this behavior correctly.
When not to use it
You must not automatically retry non-idempotent methods like POST. Retrying a POST to a /payments endpoint could charge a customer multiple times. For these operations, you need a different strategy, such as generating a unique idempotency key in the request header that the server can use to recognize and discard duplicate requests.
One canonical example
A client sends DELETE /users/123. The request succeeds, the server deletes user 123, and sends a 200 OK response. The network fails, and the client never receives the response. The client, unsure of the outcome, sends the exact same DELETE /users/123 request again. This time, the server sees the user is already gone and returns a 404 Not Found. Despite two different responses (200 and 404), the server's state is correct and consistent: user 123 is deleted. This is idempotency in action.
Interview question
A client sends an API request, but the network fails before receiving a response. Which action is safest to retry without unintended side effects?
- a.Retrying a DELETE request for a specific user, even if the user might already be gone.Correct
- b.Retrying a PUT request to increment a numerical value on a resource.
- c.Retrying a POST request to create a new order, assuming the server will prevent duplicates.
- d.Retrying a PATCH request to partially update a resource's status.
Why? this is the answer
B is correct because DELETE is an idempotent method; sending it multiple times has the same final effect on the server's state (the resource remains deleted). A is incorrect because POST is not inherently idempotent, and retrying it can create duplicate resources unless the server is specifically designed with an idempotency key. D is incorrect because while PUT is an idempotent method for *setting* a value, an "increment" operation is not idempotent as each retry would change the value further, similar to the "incrementing" example given for non-idempotent operations.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles