Skip to main content
NISHORGO Engineering portfolio
notes

Idempotency keys, and why 'just retry' isn't a strategy

When the same request can arrive twice — because of retries, network failures, or duplicate events — a system needs a contract for what 'the same request' means. This is that contract.

Example / placeholder note. This post demonstrates the format and a real engineering concern that recurs across the production data platform and Navora. Replace it before publishing.

The contract

An idempotency key is a value the caller supplies with a request that says: “if you have already processed this exact request, return the original answer.” The server’s job is to remember the first response and replay it on subsequent calls, until the key expires.

Three things make this work:

  1. A unique identity. The key — usually an opaque token from the caller — is the system’s way of saying “the same request.” Without it, the server has no way to know two retries are the same request rather than two different requests that happen to look similar.
  2. A request fingerprint. The server should also store a hash of the request payload, so that the same key with a different payload returns a conflict instead of a quietly-wrong answer.
  3. A lifetime. Keys expire. The choice of expiry is a real engineering decision: too short and a slow retry looks like a new request; too long and the storage grows without bound.

Where it gets subtle

  • Side effects beyond the response. A POST /trips call may commit a trip and publish a trip.requested event. The retry must not publish the event again — or, if it does, the consumer must be idempotent. Idempotency on the boundary is necessary but not sufficient.
  • Concurrent first calls. Two retries racing for the same key. The “first response” needs a single-writer lock or a unique constraint on the key column. Without it, the server can record two different first responses.
  • Partial failures. The request committed, the response was lost. The retry sees no entry and tries again — and the system now has two of whatever the request did. The cure is to commit the row with the side effect in one transaction, then write the response.

What this looks like in practice

The Trip Service in Navora stores (rider_id, idempotency_key) as a unique row in idempotency_requests, alongside the trip it created. A repeated (rider, key) returns the existing trip. The same key with a different payload returns a conflict, because the payload hash differs. The expiry is configurable, but the default is “long enough for any realistic retry window” — because the alternative is silent duplicates.

When not to bother

Pure read endpoints. Endpoints whose effects are intrinsically idempotent at the database level (INSERT ... ON CONFLICT DO NOTHING and similar). Endpoints where a duplicate is recoverable. Not every endpoint needs a key. The mistake is treating the absence of a key as “no problem,” when in fact it just pushes the duplicate problem downstream.

← all notes