Stack OverflowLennart SvenssonProblem & solution
Idempotency when you don't control the other end
Summary
Idempotency is a solved problem when you control the API, but the author works at maesn, a unified API that calls 30+ accounting systems that offer no idempotency key. When a request times out, the caller cannot tell whether the entry was created, so retries risk duplicates. The author tested one platform by uploading the same batch twice; both succeeded, and the duplicate was only flagged as a yellow notice in the import log. Three workarounds—pre-check lookups, timing-dependent searches, and post-hoc cleanup jobs—each break in practice. The author argues that providers should offer an idempotency key or a unique reference field that rejects duplicates with a 409, replacing all workarounds with a safe retry.
Context
The author works at maesn, a unified API that passes customer data to 30+ accounting and ERP systems. Many of those systems offer no idempotency key, so when a request times out the caller cannot tell whether the entry was created. The author tested one platform by uploading the same batch twice; both succeeded, and the duplicate was only flagged as a yellow notice in the import log, easily missed.
Approach / What changed
The author proposes that API providers give callers an idempotency key or a caller-supplied unique reference field that the provider refuses to accept a second time, returning a 409 with the existing entry. This replaces three unreliable workarounds: pre-check lookups, post-hoc cleanup jobs, and timing-dependent searches.
Takeaways
When an API provider offers no idempotency key, a timeout leaves the caller unable to distinguish a lost request from a lost response, making every retry a gamble on duplicates.
Pre-check lookups fail when the first write hasn’t finished before the retry search runs, or when the search index is updated asynchronously, and they double API calls per entry, consuming rate limits.
Post-hoc cleanup jobs cannot fix duplicates in locked months, require reversal entries that can themselves be duplicated, and must pull all entries for a period, which is slow and races against ongoing changes.
FAQ
Why doesn't a pre-check lookup prevent duplicates reliably?
A pre-check lookup can miss a duplicate if the first write hasn't finished before the retry search runs, or if the search index hasn't updated yet. It also doubles the API calls per entry, consuming rate limits and making timeouts more likely.
What is the main weakness of a post-hoc cleanup job?
A cleanup job runs after the fact, so the duplicate exists in the books until the job runs. On some systems there is no delete for journal entries, so the fix requires a reversal entry that can itself be duplicated. At month-end, locked entries cannot be touched, forcing corrections into the next period.
What details make an idempotency key trustworthy for callers?
The provider should store the key with the response, return a 409 for a duplicate key still in flight, return a 422 for the same key with a different body, and leave a failed request's key free so a transient outage doesn't block the operation until the key expires.
Source and freshness
- Source
- stackoverflow.blog
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.