The first retry implementation most of us write looks reasonable:
tsfor (let attempt = class="syntax-number">0; attempt < class="syntax-number">3; attempt += class="syntax-number">1) { try { return await callProvider() } catch { await wait(backoff(attempt)) } }
The loop is not the difficult part.
The difficult part is deciding whether another call represents the same operation, a new attempt, or an action that may already have succeeded.
That is a domain decision.
Failure does not always mean nothing happened
If a provider returns a clear validation error before accepting a request, retrying is usually pointless.
If the connection times out after the request was sent, the outcome is unknown. The provider may have completed the action and lost only the response.
These situations should not share one catch block.
I classify outcomes into at least:
- Rejected before execution.
- Failed safely and retryable.
- Completed successfully.
- Outcome unknown.
- Permanently failed.
Unknown requires reconciliation or provider idempotency. Blind retry is a business decision to risk duplication.
Preserve intent and attempt identities
One user action can produce several technical attempts.
The business intent needs a stable identity. Each attempt needs its own identity and timestamps.
textcollection intent C -> attempt T1: timed out, outcome unknown -> reconciliation: provider reports failed -> attempt T2: settled
Both attempts belong to the same intent, but they are not one transaction. The history must retain what happened to each.
This matters for support, dispute handling, analytics, and downstream financial interpretation.
Ask whether the operation is safe to repeat
Read operations are often retryable, though expensive reads can still cause load problems.
Writes need stronger analysis:
- Does the destination accept a stable idempotency key?
- Is the key scoped to the correct account and operation?
- Does the destination return the original result on repetition?
- How long does it retain the key?
- Can the same key be reused with a different payload?
- Is there a status endpoint for reconciliation?
If the provider does not support safe repetition, the local system may need a queued operator review for unknown outcomes.
Backoff protects both systems
Immediate retries can turn one dependency failure into a traffic spike.
Exponential backoff with jitter spreads work over time:
textdelay = random(0, min(cap, base * 2^attempt))
The numbers should reflect the dependency and user journey. A background report can wait longer than an interactive request. A payment outcome should not remain hidden behind thirty minutes of silent retries.
Retries also need a budget. The budget may be an attempt count, elapsed time, deadline, or combination.
Respect ordering
Retry queues can reorder related work.
If an update fails and a later delete succeeds, retrying the update afterward may recreate or corrupt state. If two balance observations arrive out of order, the older one should not replace the newer fact.
Useful protections include:
- Aggregate sequence numbers.
- Source versions or timestamps.
- Compare-and-set transitions.
- Per-aggregate queue partitioning.
- Explicit obsolete-attempt detection.
A retry should revalidate whether its action is still meaningful in current state.
Make exhaustion visible
After the retry budget is consumed, the work needs a durable final state.
I want to know:
- Which operation exhausted retries?
- What was the last classified error?
- What is the next permitted action?
- Does a person need to reconcile it?
- Can the user safely try again?
Moving a message to a dead-letter queue without an owner only changes where it is forgotten.
Alerting should usually detect new exhaustion events over a time window, not remain permanently active because a cumulative counter once increased.
Do not retry every layer
The client, API gateway, application, SDK, queue worker, and provider may each have automatic retries.
Three retries at four layers can create far more than twelve attempts, especially when timeouts overlap. It also makes the observed latency difficult to explain.
The retry policy should have a clear owner. Other layers need compatible timeouts and, where possible, stable propagation of the same operation identity.
The useful question
Before adding a retry, I ask:
If the first attempt actually succeeded, what will this next attempt do?
If the answer is unclear, the system is not ready for an automatic loop.
Reliable retries come from explicit identity, outcome classification, bounded timing, ordering rules, and reconciliation. The loop is only the final few lines.