Skip to content
All writing

NOTES & IDEAS

HTTP Idempotency: Why Repeating PUT Does Not Mean Repeating the Response

PUT is idempotent by its defined semantics. The important property is the intended effect on the resource, not an identical status code or response body.

HTTPAPI DesignBackend

“PUT. Same requests will give same result.”

That was my short answer to an HTTP interview question. The method was right, but “same result” needs a more precise explanation.

An API client usually cares about what happened to the resource. It may lose a connection after sending a request and have no way to know whether the server applied the change. Idempotency helps explain what repeating that request should mean.

Think about the effect

RFC 9110, section 9.2.2 defines idempotency in terms of the intended server-side effect: several identical requests should have the same intended effect as one. The response itself does not have to be identical.

Consider an imaginary preferences resource:

PUT /users/42/preferences
Content-Type: application/json

{
  "theme": "dark",
  "language": "en"
}

The requested outcome is that this resource has the supplied representation. Sending the same representation again should not mean “add another dark theme” or “advance to the next language.”

MDN's PUT reference describes the method as creating a resource or replacing its representation. That makes “set this resource to this state” a helpful way to reason about the example.

This is an illustrative request, not an endpoint I implemented or tested.

Responses can change

Suppose an API permits creating that resource with PUT. The first successful request might return 201 Created. A later successful replacement might return 200 OK or 204 No Content, depending on the API. Those are valid distinctions described in the PUT reference.

The resource can still end up in the same intended state.

Similarly, a request ID, response timestamp, or diagnostic header might differ between attempts. Comparing response bytes would answer a different question from comparing the intended resource change.

For a review of an API contract, I would therefore ask: “What business effect can happen twice?” That points directly at the behavior retrying clients depend on.

A method name is a contract

Imagine an endpoint named PUT /wallet/42 whose handler increments a balance on every call. Writing the method name above it would not make the increment safe to repeat.

Contrast two imaginary operations:

  • Set the notification preference to disabled.
  • Append a new notification to the delivery queue.

The first describes a target state. The second describes another occurrence of an action. Choosing a method requires thinking about that difference.

RFC 9110 allows incidental effects such as separate logging for repeated requests. Its idempotency definition concerns the requested effect, rather than requiring the server to behave identically in every internal detail. It also classifies DELETE and safe methods as idempotent. See HTTP method semantics.

A useful contract review

For an imaginary preference service, I would review these cases:

  1. Apply a representation to a missing resource.
  2. Repeat the identical request.
  3. Inspect the stored preference state after both attempts.
  4. Confirm that no business operation is repeated unintentionally.
  5. Check how the documented response distinguishes creation from replacement.

These are suggested checks, not results from a running service.

The explanation I would use now is: PUT is idempotent because repeating the same request should preserve the same intended resource effect. That is more useful than promising every retry returns the same response.