Failure Is Normal: Timeouts, Retries & Backoff
Definition
Timeout
- In plain English
- A timeout is the longest you'll wait for a response before giving up.
- Think of it like
- It is like hanging up after the phone rings for a minute.
Retry
- In plain English
- A retry is sending a failed request again in the hope it succeeds the next time.
- Think of it like
- It is like redialling when a call drops.
Exponential backoff
- In plain English
- Exponential backoff means waiting longer after each failed attempt, such as 1, 2, then 4 seconds.
- Think of it like
- It is like knocking, waiting, then waiting longer before each next knock.
Why it exists
Your refill service waits for an insurer that stopped responding. Waiting calls keep connections busy until other users cannot get through.
If every caller immediately tries again, the failing insurer receives even more work. Your recovery attempt can prolong the outage.
How it works
Jitter
- In plain English
- Jitter adds a small random amount to each wait so clients don't all retry at the same instant.
- Think of it like
- Like a crowd leaving a stadium through different gates at slightly different times.
Retry storm
- In plain English
- A retry storm is when many clients retry at once and the extra traffic keeps a struggling service down.
- Think of it like
- Like everyone redialling a jammed helpline at once.
Retry budget
- In plain English
- A retry budget caps retries to a small share of normal traffic, such as 10%.
- Think of it like
- Like a rule that you only redial three times an hour.
- Set a deadline
Put a timeout on every network call and a total deadline around all attempts.
- Check safety
Retry only operations safe to repeat, such as idempotent reads or charges protected by an idempotency key.
- Spread attempts
Increase the wait after failures, then add jitter.
- Stop
Cap attempts and total time, and refuse retries when the shared retry budget is exhausted.
A client times out, waits with jitter, receives 503, waits longer, then receives 200.
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
rng := rand.New(rand.NewSource(7))
flaky := func(ctx context.Context, attempt int) bool { return ctx.Err() == nil && attempt == 2 }
for attempt := 0; attempt < 3; attempt++ {
one, stop := context.WithTimeout(ctx, 20*time.Millisecond)
success := flaky(one, attempt)
stop()
fmt.Println("attempt", attempt+1, "success", success)
if success || attempt == 2 {
return
}
wait := (10 * time.Millisecond << attempt) + time.Duration(rng.Intn(5))*time.Millisecond
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
}
}| Result | Retry unchanged? |
|---|---|
| Timeout | Only if safe to repeat; the first attempt may have completed |
| 503 Service Unavailable | For safe operations within budget |
| 429 Too Many Requests | Wait for Retry-After, then check safety and budget |
| 400 Bad Request | No; correct the request |
| 401 Unauthorized | No; fix authentication first |
Examples
Stop waiting forever
Insurer checks have a two-second timeout and up to three retries with backoff.
Three retries mean four total attempts. A total deadline can stop the sequence earlier.
A refill request calls the insurer and returns a temporary failure when the two-second timeout expires.
The insight
Every network call needs a timeout.
Spread reconnects
After ten minutes offline, 10,000 chargers reconnect together and overload the backend again.
Ten thousand chargers each wait a random delay up to sixty seconds before reconnecting to the backend.
Spreading attempts over 60 seconds gives about 167 attempts per second on average. Actual seconds vary, so keep server-side limits too.
The insight
Jitter reduces synchronized stampedes.
Avoid duplicate deposits
A lost response led the app to charge a deposit again without an idempotency key.
Reuse the same idempotency key so the provider recognises the original operation. A timeout does not prove a charge failed.
Cap retries at 10% of original requests across the service. For 1,000 original requests, that permits at most 100 retries.
The insight
Retry only safe operations, and budget the extra traffic.
Trade-offs
Pros
- Survives brief failures.
- Frees resources from slow calls.
- Spreads recovery traffic.
Cons
- Adds load and waiting time.
- Short timeouts reject healthy slow calls.
Use when
- Every network call needs a timeout.
- Temporary failures affect safe operations.
Avoid when
- Repeating an action can duplicate its effect.
- The request needs correction before it can succeed.
Quick check
Takeaways
- Put a timeout on every network call.
- Retry only safe operations, with exponential backoff and jitter.
- Cap retries so they don't become the outage.