Does a Retried API Request Open a Second Record?

Under RFC 9110 a repeated POST need not return the same result. A request key stores the first response and returns it to the retry.

Does a Retried API Request Open a Second Record?

A client sends the same request again when the response does not arrive. On the network that is correct. On the server the request may have run twice. The first call opened the record and the response was lost on the way. The second call opens a second record. The user sees one action. The database holds two rows.

RFC 9110 separates HTTP methods into safe and idempotent. GET, repeated, does not change the server's state. PUT writes over the same address. POST has no such promise. If the same POST arrives twice, the server may produce two results. Leaving this as "the client made a mistake" ignores the timeout.

The fix is a key that is separate from the body. The client sends the same key on every attempt. The server stores the key with the first response. When the same key arrives again, the work does not run again; the stored response is returned. The key lives longer than the client's retry window. After that it is deleted. Keeping it forever bloats the table.

Stripe's API reference writes this arrangement under idempotent requests. The document belongs to a payment system. The arrangement is not specific to payment. The same split applies to every POST that opens a record, places an order or sends a command. The key is a column separate from the user's identity. Identity does not tell two different actions apart.

When FenergyTech designs an endpoint that opens a record, the retry is keyed from the start. The endpoint is tested so that a timeout does not open a second row.

A timeout looks like this. The client sends a POST, the server opens the row, the response is lost on the way. The client sends the same body again. Without a key, a second row opens. The user sees one order. The table holds two. RFC 9110 does not say a repeated POST returns the same result. It does say that for GET and PUT. The key is therefore not part of the body. It is a separate header.

The key is stored with the first response. When the same key arrives, the work does not run again. The stored response is returned. If the same key arrives while the first request has not finished, the second request does not start a new piece of work. It waits, or it returns 409. If both run, the race erases the point of the key.

The lifetime is longer than the client's retry window. If the window is 30 seconds, the key stays for a few minutes. Keeping it forever bloats the table. When the time is up, the key is deleted. The same body arriving after that is a new piece of work. That bound is written for the client as well. Stripe's page on idempotent requests writes the same arrangement for payment. The arrangement holds for every POST that opens a row.

Sources: RFC 9110, HTTP Semantics, IETF, 2022, section 9.2. Stripe, API Reference, Idempotent requests.

Share:

Planning a similar project?

We can talk through how the topic in this article applies to your product.

More articles

Browse our other technical articles