Why a retry can do the work twice
A timeout doesn't tell you whether the server acted. The request may have died on the way in, or the server may have created the invoice and lost the connection on the way out. From the client's side, both look the same: no answer.
HTTP already draws a line here. RFC 9110 calls a method idempotent when sending it many times has the same intended effect as sending it once: PUT, DELETE and the safe methods such as GET. POST and PATCH aren't on that list. The RFC says a client should not retry a non-idempotent request automatically unless it has some way to know the request is idempotent, or that the first one was never applied.
Most writes that matter are POSTs: create a customer, charge a card, send an invoice, place an order. So the question is how to give a POST that "some way to know".
How an idempotency key works
The client makes up a unique value for each thing it means to do once, and sends it in a header, Idempotency-Key. The server stores the key with the result of the first request. When the same key comes back, the server skips the work and returns the stored result.
Stripe's API works this way, and the IETF is writing the same idea down for every HTTP API in "The Idempotency-Key HTTP Header Field". As of December 2, 2026 it is version 09 (November 21, 2026), still an Internet-Draft of the httpapi working group, not an RFC. It credits Stripe's and PayPal's docs as inspiration, and its implementation list names Stripe, Adyen, Dwolla, WorldPay and others that already send the header.
What the server stores, and for how long

A server that honors keys has to answer three questions beyond storing them, and the draft and Stripe's docs answer them in much the same way:
| Case | IETF draft | Stripe (API v1) |
|---|---|---|
| Same key, same request, first one finished | Answer as if the request completed once | Return the saved status code and body, a 500 error included |
| Same key, first one still running | 409 Conflict | Not saved; you can retry |
| Same key, different request | 422, if the server keeps a fingerprint of the request | An idempotency_error: the parameters don't match |
| How long a key lasts | The server sets an expiry and should publish it | Keys can be removed once they're at least 24 hours old; after that, the same key is a new request |
So the server keeps four things per key: the key, a fingerprint of the request (a hash of its body is enough), whether it is still running, and the response it gave. The claim has to be atomic, a unique index or an insert-if-absent, or two copies arriving together will both run. One way to write it:
# An example written for this post, not code from our platform.
async def create_invoice(request, store):
key = request.headers.get("Idempotency-Key")
if key is None:
return problem(400, "Idempotency-Key is missing")
fingerprint = sha256(request.body).hexdigest()
# Claim the key in one atomic write: of two requests, only one inserts it.
seen = await store.insert_if_absent(
key, {"fingerprint": fingerprint, "state": "running"}, ttl=timedelta(hours=24)
)
if seen is not None:
if seen["fingerprint"] != fingerprint:
return problem(422, "This key was used with a different request")
if seen["state"] == "running":
return problem(409, "The first request with this key is still running")
return seen["response"] # the first answer, sent again
try:
response = await do_create_invoice(request)
except Exception:
await store.release(key) # the work failed: let a retry run it
raise
await store.finish(key, response)
return responseWhen the work and the saved answer live in the same database, write them in one transaction. Otherwise a crash between the two leaves an invoice with no saved answer, and the next retry creates a second one.
Where the key comes from
The draft recommends a UUID or a similar random value, and Stripe suggests V4 UUIDs, keys of up to 255 characters, and no sensitive data such as email addresses in them. Whatever the format, a retry must send the same key as the first try, so a random key made inside the retry loop protects nothing.
Background jobs make that easy to get wrong, because a job that retries starts from scratch. Our own invoicing code takes the other route: it builds each key from the record it acts on, so every run of the job arrives at the same key. It calls Stripe from a background job that only runs once a Stripe key is set on the server.
# apps/api/app/modules/projects/jobs.py (trimmed)
def customer_key(project_id, email):
# One customer per project and billing email, however often the job runs.
return f"project-{project_id}-customer-{sha256(email.lower().encode()).hexdigest()[:16]}"
key = f"milestone-{milestone['_id']}-{milestone.get('attempt', 1)}"
invoice = await stripe.create_invoice(..., idempotency_key=f"{key}-invoice")
await stripe.add_invoice_item(..., idempotency_key=f"{key}-item")
return await stripe.finalize_and_send(invoice["id"], idempotency_key=key)- The milestone's id ties the key to the thing being billed, so a retry of the job finds the invoice it already started.
- The attempt number changes only when someone sends it again on purpose, after fixing the amount for example. A changed request gets a new key, which is what the draft asks for.
- Only a short hash of the billing email goes into the key.
- characters at most in a Stripe idempotency key
- 255
- after which Stripe may forget a key (API v1)
- 24 h
- attempts at most for a job in our worker, the last within minutes
- 5
Our worker waits 10, 20, 40 and then 80 seconds between tries, so every retry lands well inside Stripe's 24 hours. A queue that retries over days would outlive the key and create the invoice again.
When the API has no keys
Some APIs offer nothing of the kind. The LinkedIn API our blog uses to share a post on the founder's profile accepts no idempotency key, so a retry after a lost answer could publish the same post twice. In that case the client has to do the remembering:
- 01Claim the action in your own database before calling the API. Our job sets the share to
postingwith one atomic update, and does nothing if another run already holds it or it went out. - 02On a clear answer, record it:
sharedwith the post's id, orfailedwith the reason. - 03On a lost answer, don't retry. Mark it failed and say why. A claim left
postingfor more than 15 minutes shows in the admin as "a share stopped halfway: check the LinkedIn profile before you share it again".
At most once, or at least once
Without keys you choose which failure you can live with. For a public post we chose at most once: a share that never happened is easy to fix by hand, a duplicate on someone's profile is not.
A short checklist
A timeout doesn't tell you whether the server acted.
- Send a key with every write that creates something or moves money, and keep the same key across retries.
- Build the key from the action you mean to do once, so every attempt shares it.
- On the server, claim keys atomically, fingerprint the request, and publish how long keys last.
- Keep retries inside that window.
- Where an API has no keys, claim the action in your own database and send failures to a person.
Keys cover the requests you send. For the events a payment provider sends back to you, read Stripe webhooks you can trust. If a product of yours takes payments and you want this checked, see what fits.


