← all notes
api-designcaching2 min read

Idempotency keys at the edge

February 3, 2026

Why the boundary, not the handler

A client sends a POST, the network hiccups, the client retries. The handler runs twice. If the operation was "create order," there are now two orders. Neither the client nor the server is wrong. The network is allowed to lose responses; the client is allowed to retry; the system has to absorb both.

Putting idempotency logic inside each handler means every handler has to remember it. One forgets, and the guarantee breaks without a sound. Enforce it at the gateway, before the request reaches the handler, and the guarantee becomes structural instead of aspirational.

The mechanics

The client sends an Idempotency-Key header on every mutating request. The gateway:

1. Checks the cache for the key. Hit? Return the stored response, status code and all. The upstream never sees the request. 2. Miss? Mark the key as in-flight, call upstream, store the response, release. 3. Concurrent duplicate (same key, request still in flight)? Block until the first completes, then returns the same stored response.

Step 3 is the one most implementations skip, and the one that actually prevents duplicates under real retry pressure.

Window and storage

The cache needs a retention window. Twenty-four hours is a common default. Long enough to cover client retry loops, short enough to bound storage. Keys should be scoped per client. Two different clients using the same key must not collide.

What idempotency does not fix

It makes retries safe. It does not make concurrent-but-distinct requests safe, does not serialize them, and does not protect against a client that decides to send two genuinely different operations. It is a replay guard, not a transaction.