Distributed systems fail in partial ways. A network timeout does not tell you whether the request arrived, executed, and was lost on the way back — or never arrived at all. Without idempotency, retrying that request can double-charge a customer, send the same email twice, or insert duplicate records. This article covers the practical patterns you need and the traps most teams fall into.

In a recent development, an operation is idempotent if applying it multiple times produces the same result as applying it once. DELETE /people/42 is naturally idempotent: deleting an already-deleted user returns 404 but leaves the system in the same state. POST /payments is not: a naive implementation creates a new payment on each call. (Related: University launches $5m smart system that checks who is...)

Recent findings suggest that the goal is not to prevent duplicate requests — networks are unreliable and clients will retry. The goal is to make duplicates safe.

The most common approach for POST endpoints is the idempotency key: the client generates a unique key (usually UUID v4), sends it as a header, and the server deduplicates on it. (Related: School sends out minibus to catch absent pupils playing...)

Here is a minimal implementation in Go using PostgreSQL:.

Two things worth noting: store the full response — status code and body — not just a "done" flag. Replaying the exact original response is what makes the client treat the duplicate as a no-op. Also store error responses; if the card was declined, the client needs to know that, not receive a silent 200 on retry.

Recent findings suggest that for some operations you can skip a separate idempotency store entirely. If a payment's ID is derived from hash(user_id + date + amount), inserting the same payment twice hits the same primary key and fails gracefully.

This works well for batch imports and event processing. It does not work when the same operation can legitimately recur (charging the same amount on two different days).

In a recent development, idempotency collapses when you need to write to a database and publish an event. The classic failure: you commit the DB write, then crash before publishing — or you publish, then the write fails.

The outbox pattern keeps everything in one transaction:.

According to recent reports, the DB write is atomic. The publish is at-least-once, so consumer deduplication is still required — but at least you will never lose an event.

Checking idempotency after the side effect. The check must happen before any external call. If you call your payment processor first and then check the idempotency store, you have already charged the card before deduplicating.

According to recent reports, not purging old records. An idempotency store that grows forever is a liability. Stripe retains keys for 24 hours. Set a TTL that covers your retry window and schedule a cleanup:.

Recent findings suggest that assuming your dependencies are idempotent. Each layer of a distributed call chain needs its own idempotency key, passed through. An idempotent API can still trigger non-idempotent downstream calls.

Using timestamps or client IPs as keys. Timestamps collide under load; IPs are shared. Keys must be client-generated UUIDs with enough entropy — typically UUID v4.

Treating Kafka producer idempotency as enough. enable.idempotence=true prevents duplicate messages within one producer session but does not survive restarts. Consumer-side deduplication is still required.

Industry observers note that idempotency is not an edge case — it is a design requirement for every distributed write that might be retried, which is all of them. The pattern is simple: client-generated key, server-side result storage, exact response replay. The complexity comes from TTLs, error handling, ordering of checks, and propagating keys through call chains. Getting this right before production is cheaper than debugging duplicate payments after.

If you are running an audit of your API's resilience posture, the free architecture and security checklists we publish cover idempotency alongside authentication, rate limiting, and input validation — in a format you can hand to a team.

I run AYI NEDJIMI Consultants, a cybersecurity consulting firm. We publish free security hardening checklists — PDF and Excel.

→ Read original article