Idempotency When You Don't Control the Other End

You fire a payment request to a partner's API. The connection times out after 30 seconds. Did the charge go through? You don't know. So you retry — and now the customer has been billed twice. This is the moment every vibe-coded integration eventually hits: idempotency when you don't control the other end.

The good news: you don't need the other side's cooperation to be safe. You need to design your own side so that a retry is harmless even if the remote system is dumb, flaky, or actively hostile to your assumptions. Below are the failures that actually show up in production, why they happen, and what to do about each.

Failure 1: You retry a request that already succeeded

This is the classic. Your HTTP client times out, your retry logic fires, and the remote service processes both requests. The remote never knew they were the same operation.

The mechanism: a timeout is not a failure signal. It means you stopped waiting. The remote may have committed the transaction at second 29 and sent the response at second 31. Your client gave up at 30.

Why it happens: most retry libraries (and most LLM-generated retry code) retry on timeout and on 5xx without any notion of operation identity. The retry is a fresh request.

How it ends: duplicate charges, duplicate emails, duplicate rows, duplicate webhooks. In a payment flow, that is a support ticket and possibly a chargeback.

How to fix it: attach an idempotency key to every request — a value you generate once per logical operation and reuse across all retries of that operation. If the remote honors it (Stripe, for example, accepts an Idempotency-Key header on POST requests and deduplicates on it), you are done. If the remote ignores it, you fall back to failure 2's strategy.

A subtle trap: generating the key inside the retry loop. If your code does for attempt in range(3): key = uuid4(), you have defeated the entire point. The key must be born once, upstream of the retry, and carried through.

The key belongs to the operation, not the attempt Generate the idempotency key when you create the work item (the order, the job, the message) and store it alongside it. Retries read it; they never mint a new one.

Failure 2: The remote has no idempotency support at all

Plenty of internal services, legacy SOAP endpoints, and third-party APIs simply do not deduplicate. No header, no token, no promise.

What you do then is make the effect idempotent from your side. Three patterns that work without any cooperation:

  1. Check-before-write. Query for the record you are about to create (by your own external ID) and skip the write if it exists. This is racy under concurrency but fine for low-throughput flows.
  2. Write a local ledger first. Record the intent with a unique constraint in your database before calling out. A second attempt hits the constraint and never reaches the network.
  3. Reconcile after the fact. Let the duplicate happen, but make downstream processing deduplicate on a natural key (order ID + line item, email + template + date).
Retry safety with and without the remote's help
Remote honors idempotency key
  • One key per operation, reused across retries
  • Duplicate requests deduplicated server-side
  • Caveat: keys expire — Stripe's last 24 hours
Remote ignores it
  • Local ledger with a unique constraint
  • Check-before-write on your external ID
  • Downstream reconciliation on a natural key
  • Irreversible effects need prevention, not cleanup

Here is how the three compare on the dimensions that matter when you are picking one at 2 a.m.:

Pattern Protects against Fails when Cost
Check-before-write Sequential retries Two retries race each other One extra read per call
Local ledger All retries from your service Multiple instances share no ledger A table and a unique index
Reconcile downstream Almost everything The side effect is irreversible (an SMS, a wire) Batch job and dedupe logic

The pattern you cannot skip: if the side effect is irreversible — money moved, an SMS sent, a physical label printed — only prevention works. Reconciliation cleans your database, not the customer's bank statement.

Failure 3: The remote is idempotent, but only for a window

Some APIs accept an idempotency key but expire it. Stripe, as of its documented behavior, retains idempotency keys for 24 hours. After that, the same key is treated as a new request. If your retry queue has a dead-letter job that replays a day and a half later, you are outside the window.

Why it bites: retries that happen minutes apart are safe; retries that happen after an incident, a backlog drain, or a manual replay are not. The dangerous retries are the slow ones, and those are exactly the ones nobody tests.

How to fix it: stamp every queued request with its creation time, and refuse to auto-replay anything older than the remote's documented retention window. Route those to a human or to reconciliation instead. Store the window as a config value per integration, not as a magic number buried in a retry decorator.

Failure 4: You deduplicate on the request, but the remote answers with a new ID

This one is quiet. Your first call succeeded; the retry also succeeded because the remote did not deduplicate. Now you have two remote objects, two IDs, and your system has stored only one of them. Months later, the orphaned object shows up in a report and nobody can trace where it came from.

How it ends: phantom records, mismatched balances, and an audit that takes a week instead of an hour.

How to fix it: never treat "I sent one request" as "there is one record over there." After a retry storm, run a lookup by your own correlation ID if the remote supports one, or by natural attributes (amount, timestamp bucket, customer) if it does not. Log every remote ID you ever received, including from attempts you considered failed.

Failure 5: The webhook fires twice and your handler was never built for it

The mirror image of outbound retries is inbound ones. Most webhook senders retry on non-2xx responses, and some retry on slow responses too. Your handler receives the same event twice — or three times — and does its work twice.

This is a place where vibe coding hurts specifically: an LLM asked to "handle the payment webhook" will happily write def handle(event): fulfill_order(event). It looks correct. It is correct exactly once.

How to fix it: store every processed event ID before acting on it, in the same transaction as the action if your database allows. If the insert collides, return 200 and do nothing. And return 2xx fast — do the heavy work asynchronously, because a slow handler is a retried handler.

Both directions need the same discipline Outbound: one key per operation, reused across retries. Inbound: one record per event ID, written before the side effect. If you only build one of the two, you have built half an idempotent integration.

Failure 6: The duplicate is invisible until the numbers don't add up

Duplicates rarely announce themselves. The charge succeeds twice, the customer notices a week later, and by then the trail is cold. The teams that catch this early are not smarter — they have a check that runs on a schedule.

A workable minimum: for every integration that moves money or creates records, run a daily query that counts operations per correlation key and flags any key with more than one. It is a handful of SQL and it turns a mystery into a ticket the same day. If you have ever had to explain to a customer why they were billed twice, you know the difference between catching it in 24 hours and catching it in three weeks.

What this looks like when you are wiring agents and automations

When the outbound call is made by an AI agent rather than a cron job, the failure surface grows: the agent may retry on its own reasoning ("the call failed, let me try again"), and it may not carry your carefully minted key forward. The fix is architectural, not prompt-level — the tool the agent calls should own the idempotency key and the local ledger, so the agent's decision to retry is harmless by construction. ASI Biont supports connecting to external services via API — learn more at asibiont.com — and the same rule applies there: the safety lives in the tool's implementation, not in the model's good intentions.

There is a security parallel worth keeping in mind. Just as an idempotency key is a token that must be generated once and never reused across operations, credentials are tokens that must never be reused across systems — and a weak password reused everywhere is the same class of mistake at a different layer..

The one rule that survives every remote

You cannot make the other side idempotent. You can make your side safe to retry: mint the key once per operation, persist the intent before the call, refuse replays outside the remote's documented window, and treat every inbound event as if it will arrive twice — because eventually it will. The remote's behavior is a variable you do not control. Your side of the contract is the part you can actually ship.

AssetMaterials for this article

A document from this article — download and apply it.

  • XLSXIdempotency Safety Design Sheet.xlsxXLSX · 9 KBDownload
← All posts

Ask AI about this article

Ask a question about this article — ASI Biont will answer based on its text and the attached files.