Retry without multiplying side effects
Classify errors, cap attempts, and preserve cancellation.
Know what can repeat
A temporary connection failure during a read is often safe to retry. A write may have completed before its response was lost. Require a server-supported idempotency key or a way to reconcile status before repeating a write. Do not retry validation or authorization failures.
Budget the loop
Limit attempts and total elapsed time. Back off with jitter so many clients do not hammer a recovering dependency together. Check ctx.Done before sleeping and before the next attempt. Return an error that preserves the last useful cause and the operation context.
select {
case <-time.After(delay):
case <-ctx.Done():
return ctx.Err()
}
Test the uncertain result
Simulate a write that succeeds remotely but drops the response, then retry with the same key. Assert one remote effect. Also test a permanent 400, repeated 503s, and cancellation during backoff. These cases prove safety much better than merely checking that a loop ran three times.
Check your understanding
Why is a timeout after a write an uncertain result?
Show explanation
The remote service may have completed the write before the response was lost. A blind retry can duplicate the effect.
You can read for free. Create an account to save your progress.
← Previous lesson: Bound outbound HTTP callsPut this lesson into practice
A practice activity at this level in the same module.
Try: Fan out with a bounded worker poolFurther reading
Primary references for this module. Check the current version when behavior or framework guidance may have changed.