Exactly Once Is a Lie: Idempotency and the Safe Retry
Here is a sequence that happens millions of times a day in any busy system. A client sends a write to a server. The server receives it, applies it, and sends back an acknowledgement. The acknowledgement is lost — a dropped packet, a connection reset, a timeout on a congested link — and never reaches the client. The write is done; the data is durable; everything on the server side went perfectly. But the client is sitting there with a request that never got an answer, and it faces a question it genuinely cannot answer: did the write land, or not?
It cannot answer it because the two failures that could have happened look identical from where it stands. Maybe the request never arrived and nothing was applied. Maybe the request arrived, was applied, and only the reply was lost. From the client's side both are the same thing — silence where an ack should be — and no amount of waiting distinguishes them, because waiting longer on a reply that was already lost just produces more silence. So the client does the only sensible thing: it retries. And the moment it retries, a write that may have already been applied is on its way to the server a second time.
This is why exactly-once delivery is a myth. You cannot guarantee a message crosses an unreliable network once and only once, because the sender can never safely conclude the first attempt worked, and a sender that stops retrying on ambiguity will sometimes drop writes that actually failed. The achievable goal is not to deliver exactly once; it is to behave as though you did. This post is about how.
The question you can't answer on a timeout
It is worth sitting with why the ambiguity is fundamental rather than a bug to be fixed. When a client times out waiting for a response, the outcome lives in one of three worlds: the request was lost before the server saw it; the server saw it, applied it, and the response was lost; or the server is simply slow and the response is still coming. The client cannot tell these apart, because the only evidence it has is the absence of a reply, and absence is produced identically by all three. This is the same impossibility that makes failure detection a guess rather than a lookup — silence has multiple causes and carries no information about which one occurred.
That leaves two unappealing options, and you must pick one up front. If the client does not retry on a timeout, it will sometimes silently drop a write that never landed — at-most-once delivery, which loses data. If it does retry, it will sometimes apply a write that already landed — at-least-once delivery, which duplicates. There is no third setting of the dial that gives you exactly-once, so the only engineering choice is: which failure do you prefer, and which one can you clean up after? For almost everything that matters, losing a write is unacceptable and a duplicate is merely a problem to be managed — so you choose at-least-once and then you make the duplicate harmless.
At-least-once is the honest guarantee
At-least-once delivery is a contract you can actually keep: the sender retries until it receives an acknowledgement, so the operation is delivered at least once and possibly several times. It never loses a write to a lost ack, because a lost ack simply triggers another attempt. The cost it hands you, in exchange for never losing anything, is duplicates — and the whole art is making those duplicates not matter.
Make the operation idempotent
The cleanest way to neutralize a duplicate is to use an operation that does not care how many times it runs. An operation is idempotent when applying it twice leaves the same state as applying it once, and a surprising number of operations already have this property for free. Setting a key to a value is idempotent: SET user:7 = "active" run three times leaves exactly the same state as running it once, so a retried SET is simply harmless. Deleting a key is idempotent. Adding an element to a set is idempotent. For these, at-least-once delivery needs nothing extra — the duplicate is a no-op by construction.
The operations that bite are the ones that accumulate. INCR views is the canonical offender: run it twice and the counter is two higher, so a retried increment double-counts, and a lost ack on a counter quietly inflates every number you report. Appending to a list, enqueuing a job, charging a card, sending an email — all of them change state by a delta rather than to a value, and a delta applied twice is wrong twice. The first design question for any write path is therefore whether you can express the change as a set-to-value instead of an add-a-delta, because if you can, the retry problem dissolves before you write a line of deduplication code.
Deduplicate at the resource, with a key
When the operation is not naturally idempotent, you make it idempotent by attaching an idempotency key — a unique identifier the client generates once per logical operation and reuses on every retry of that same operation. The server records which keys it has already applied and, on seeing a key it has processed before, skips the re-application and returns the stored result of the first attempt. The second INCR carrying op#42 does not increment; it returns what the first one returned. At-least-once delivery on the wire has become effectively-once at the resource.
// The client generates ONE key per logical operation and reuses it on every retry.
var opId = Guid.NewGuid(); // created once, before the first attempt
// The server applies each key at most once, and remembers the result.
if (seen.TryGet(opId, out var cached))
return cached; // a retry — return the first result, don't re-apply
var result = Apply(op); // first time: do the work
seen.Put(opId, result, ttl: dedupWindow); // remember it for the dedup window
return result;
Two details decide whether this actually holds. The first is where the dedup record lives: it has to sit with the state the operation mutates, replicated the same way, or a failover to a replica that never saw op#42 will cheerfully apply the duplicate the primary already absorbed — the dedup table is part of the data, not a cache beside it. The second is the dedup window: you cannot remember every key forever, so you keep them for a bounded time — comfortably longer than the client's maximum retry horizon — and accept that a retry arriving after the window has expired is indistinguishable from a fresh operation. Size the window to the retry policy, not to convenience. And where ordering matters as well as uniqueness, the key pairs naturally with a monotonic version or fence: the resource can reject not just a duplicate but a stale retry that a newer write has already superseded, which is the same high-water-mark discipline that keeps a cache or a lock from moving backward.
Wrap-up
Exactly-once delivery is unachievable because a lost acknowledgement and a lost request are the same silence to the sender, so a sender that must not lose writes must retry, and a retry can duplicate. The honest guarantee is at-least-once — never drop a write, and make the duplicate harmless — and the harmlessness comes from idempotency. Reach first for operations that are idempotent by nature, expressing a change as a set-to-value rather than an add-a-delta wherever you can, because then a retry is a no-op and there is nothing to deduplicate. For the operations that genuinely accumulate, attach an idempotency key the client generates once and reuses on every retry, deduplicate against it at the resource that owns the state, replicate the dedup record with that state, and bound how long you remember keys to the client's retry horizon. Do that and the ambiguous acknowledgement stops being a correctness problem: the client retries freely into the uncertainty, the resource absorbs the repeats, and the operation takes effect exactly once even though it crossed the wire more than once — which is the closest thing to exactly-once that an unreliable network will ever let you have.