Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Networking › HTTP

Retries and idempotency

No response came back, so the request was sent again

Why it exists — When no response arrives, the client cannot tell whether the request never reached the server or was processed and only the response was lost, so sending it again risks having the same work done twice. Idempotency identifies requests that leave the server in the same state no matter how many times they run as when they run once, so such requests can be safely sent again. An idempotency key lets the server recognize a request it has already handled even for requests that are not idempotent, such as POST.

Where it's used today — For POST APIs where work must not be done twice, such as payment, transfer, and order creation, you receive an idempotency key to prevent duplicates. Automatic retries in HTTP clients and gateways are usually turned on only for idempotent requests such as GET, PUT, and DELETE. A change such as "increase the quantity by 1" is sometimes turned into an idempotent one by changing it to a value assignment such as "set the quantity to 3".

On a network, a request or a response can be lost, and when no response arrives, the client sends the same request again. Depending on the HTTP method, running a request several times either gives the same result (the method is idempotent) or it does not.

Checked against real execution · The requests on the screen were sent with curl 8.22.0 to a Spring Boot 4.1.1 server while iptables dropped request or response packets, and all 90 combinations were checked

Concepts in this lab

Safe methods and idempotent methods
Safe methods are read-only methods that do not change the state of the server: GET, HEAD, OPTIONS, and TRACE. Idempotent methods are methods for which sending the same request several times has the same effect on the server as sending it once: all the safe methods plus PUT and DELETE. POST and PATCH are not guaranteed to be idempotent, so sending one twice can cause it to be processed twice. This is the basis for deciding whether a request may be sent again when no response arrives.
HTTP methods
The part of an HTTP request that indicates the action the request wants to perform on a resource. GET means read, POST means create or a processing request, PUT means replace the whole resource, PATCH means modify part of it, and DELETE means delete. Use each one according to its meaning so that HTTP behavior such as caching and retries works as expected.
Idempotency and idempotency keys
Where it's used today — In payment approval, money transfer, and order creation APIs, the client sends a key in a header such as Idempotency-Key, and the server records that key using a database unique key or Redis and filters out requests with the same key. Message consumers also use it to filter out duplicates.
Idempotency is the property that performing the same request several times gives the same result as performing it once. A network problem can cause the same request to be sent again, so operations that must not be duplicated, such as payments, must be made idempotent. An idempotency key is a unique value that the client attaches to each request. When a request arrives with a key that the server has already processed, the server does not process it again and returns the earlier result.
HTTP status codes
Why it exists — If results were reported only as text in the body, each server would write them differently, and browsers, proxies, and monitoring tools could not tell whether a request succeeded. A status code reports the result as a fixed number, so these tools can do caching, retries, and error counting without being adjusted for each server.
Where it's used today — It is used to branch handling by the kind of error, such as a frontend that sends the user to the login screen on a 401 and to a "page not found" notice on a 404. In operations, you watch the ratio of 5xx responses and, if it suddenly rises, treat it as an outage and send an alert.
A three-digit number that shows the result of the server's handling of a request. 2xx means success, 3xx means a move to another location (redirection), 4xx means a problem with the client's request, and 5xx means an error on the server side. Common ones are 200 (success), 201 (created), 400 (bad request), 401 (authentication required), 403 (no permission), 404 (not found), and 500 (server error).

Try these

Running with the default settings

  1. This is the initial server state. Order 1 (mouse, quantity 2) exists.
  2. First request: The server handled the request (201 Created (order 2)). But the response was lost on its way back. The client cannot tell whether the server handled the request, so it sends the same request again.
  3. Retry #1: The server handles the request and returns the response 201 Created (order 3). This time the response reached the client.

Result of this run

Code

POST /orders HTTP/1.1
Content-Type: application/json

{ "item": "keyboard", "quantity": 2 }

// Client: if no response arrives within 3 seconds,
// send the same request again, unchanged

Assumptions of this model

The definition of idempotency follows the HTTP standard (RFC 9110). A method is idempotent if the intended effect on the server of sending the same request several times is the same as that of sending it once, and GET, HEAD, OPTIONS, and TRACE (the safe methods) and PUT and DELETE belong here. The standard does not guarantee that POST and PATCH are idempotent (a PATCH can be designed to be idempotent, depending on its content). The Idempotency-Key header is a convention widely used in payment APIs and similar services, and it has not been finalized as a standard. All 90 combinations (five requests, three kinds of loss, 1 to 3 losses, and whether the idempotency key is sent) were actually run and checked against the screen. The requests on the screen were sent with curl 8.22.0 to a Spring Boot 4.1.1 server (Spring MVC, embedded Tomcat, orders kept in an H2 in-memory database), and curl was set to send the same request again when no response arrives within 3 seconds (--max-time 3 --retry). The loss was produced by having the firewall of the Linux kernel on the server side (iptables 1.8.13) actually drop the packets that carry the request or the response on the first few connections. The number of sends was confirmed by the number of connections the server received, the number of executions and the response to each request by the record kept by the server, and the server state by the orders left in the database. The order handling and the idempotency key handling on the server (process a key seen for the first time and store the response, return the stored response for a key already seen) are a controller made up for this example, following the description on the screen. Requests with the same key that arrive at the same time, and how long a key is kept, are not covered. The client and the server are Linux containers on a Docker network on one PC.

Previous lab: HTTP caching and ETag · Next lab: Redirection

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these