Safe retries need a durable operation identity
Design a bounded deduplication contract with scoped keys, request fingerprints, atomic effect recording, concurrent-attempt behavior, and explicit retention limits.
On this page
The question for today
What you’ll understand
- Distinguish an idempotent effect from a client observing a successful response.
- Specify key scope, payload mismatch handling, and atomic deduplication state.
- Calculate retention capacity and explain why eviction can end a replay guarantee.
A client loses its connection after asking a service to create an order. Retrying may be necessary, but a second execution could create a second order. The protocol needs a stable identity for the intended operation so repeated attempts can refer to the same effect. A header containing a key is only the beginning. The hard requirements are agreement about what that key means, concurrent handling, durable state, and what happens after the service forgets it.
Define the same operation before detecting duplicates
An idempotent method has the same intended server effect when repeated as when performed once under its defined semantics; it need not produce identical response bytes on every attempt. Deduplication gives an application a more specific operation-identity contract. Scope the identifier to the relevant tenant, caller authority, and operation type so unrelated users cannot accidentally share a result. The client must reuse the same identifier for retries of one intended action and choose a new one for a genuinely new action. Automatically generating a new key per retry defeats the purpose.
Bind the identifier to a stable request fingerprint covering the semantically relevant input. Reusing a key with a different amount, destination, or model parameter should produce a defined mismatch error rather than silently returning an unrelated result. Canonicalization must match the application's meaning; bytewise JSON equality can distinguish harmless formatting differences, while overaggressive normalization can collapse distinct requests. Authenticate and authorize every attempt, including result retrieval. A deduplication hit is not permission to reveal another principal's response, and an unpredictable key does not replace those checks.
Record the result at the effect's actual boundary
A typical state model distinguishes an absent operation, an in-progress operation, and a completed operation with a stored result or retrieval reference. Concurrent attempts must not both observe absence and execute independently. Use a storage mechanism whose atomicity and durability cover the claimed invariant. If the effect and completed deduplication record are committed together in one transaction, a crash cannot expose one without the other under that transaction's guarantees. If the side effect occurs in an external system outside that boundary, a local record alone cannot provide the same guarantee; the external interaction needs its own compatible protocol.
Specify what a second attempt sees while the first is pending: it might wait within its remaining deadline, receive an in-progress response, or query a status resource. A worker timeout does not prove the worker stopped, so simply letting a second worker repeat the action after a lease expires can duplicate effects. Recovery must prevent stale execution from committing under the chosen mechanism. Store enough completion evidence for an ambiguous client to reconcile later. The transport's success or failure status is then one observation about an attempt, while durable operation state establishes the business outcome.
Forgetting a key changes the guarantee
Retention must cover the retry and reconciliation horizon promised to clients. Once a completed key is forgotten, the same old request can look new unless another durable business invariant prevents replay. Time-to-live, manual cleanup, disaster recovery, and capacity eviction all affect that horizon. Make expiry behavior explicit: reject an expired operation identity when possible, require a new business decision, or state the narrower guarantee. Calling a cache a deduplication store does not make eviction harmless. Tests should include a crash after effect commit, concurrent retries, payload mismatch, lost responses, and replay just before and after the supported retention boundary.
The cache lab demonstrates the danger of finite remembered identities without implementing durable deduplication. For EACH run Reset, explicitly set BOTH capacity and stride, predict, then Play or Step. At t=0/N=16, (C,s)=(4,4) remembers a four-identity cycle and produces twelve hits; (3,4) produces none because every replay follows eviction. Edits retain t and recompute the trace from empty. These hits are only toy LRU membership: there is no atomic effect, persistence, fingerprint, tenant scope, or expiry clock. Explain which missing guarantees a real operation store must add before treating a remembered key as safe replay evidence.
Work it through
Budget the promised replay horizon
A fictional service receives two new operation IDs per second and promises to retain completion records for 600 seconds. Each record averages 256 bytes, excluding result bodies, indexes, replicas, and overhead. Assume steady arrivals.
Expected retained distinct operations are 2×600=1,200. Repeated attempts of an existing retained key do not add new operation identities in this model.
Raw record payload is 1,200×256=307,200 bytes, or 300 KiB. Real storage needs additional capacity for the explicitly excluded costs and workload bursts.
If a capacity policy evicts records after only 300 retained operations at this rate, the effective steady horizon is about 300/2=150 seconds, contradicting the 600-second promise unless another durable invariant preserves it.
Retention is part of the correctness contract, not merely a memory optimization. An undersized evicting cache can invalidate the advertised retry window.
The living notebook
Explore the model
Interactive lab · cache
An evicted identity can look new again
For EACH run Reset, re-set BOTH C and s, predict, then Play or Step. At N=16/t=0, (4,4) yields twelve hits and (3,4) yields zero over the same four-key cycle. Edits retain t and recompute from empty. This LRU membership model has no durable effects, atomic claims, request fingerprints, authorization, or guaranteed retry horizon.
- Hits / requests
- 0 / 16
- Hit rate
- 0%
- Address-stream period
- 16 addresses
A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forFinite remembrance can fail on replay after eviction; deduplication correctness requires more than an in-memory key hit.
Pause & reason
Check your understanding
Your turn
Try it yourself
A client retries key K with amount 20 after an earlier attempt under K used amount 10. The prior operation may have committed. What should a well-defined deduplication contract do, and why is returning the old result silently unsafe?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Distinguish a retry of the same intended action from a new semantic request sharing an identifier.
Compare with the solution
Check the scoped key's stored fingerprint and return a defined mismatch error or equivalent explicit conflict. Silently treating amount 20 as the same action hides a changed request; executing it as new can duplicate or alter effects. Authorization and a separate decision about a genuinely new operation remain necessary.
Keep the idea
Take it with you
- A retry needs the same scoped identity and the same semantic request.
- Durable effect recording must share a compatible atomic correctness boundary.
- Retention and eviction determine how long replay protection can be promised.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.