L17 Timeouts and retries
21 minSuggested first: L16 Clients and redirects
Outcome
Calculate a bounded retry schedule and decide when it is unsafe to retry an operation.
Learn
A timeout bounds how long you wait; it doesn't prove the server did nothing. A retry policy says which failures are eligible, how many attempts are allowed, and how long to wait between them. Backoff makes the waits grow; jitter spreads retries out so callers don't all retry at once.
An idempotent operation has the same intended effect when it's repeated; its responses needn't match.
Worked example
The policy allows three attempts in all, a 100 ms timeout per attempt, waits of 50 ms then 100 ms, and no jitter. Every attempt times out. When does the caller give up?
Attempt 1: 0–100 ms wait 50 ms
Attempt 2: 150–250 ms wait 100 ms
Attempt 3: 350–450 ms stop
Predict first, then show the worked explanation
Attempts start at 0, 150 and 350 ms. The last timeout arrives at 450 ms, assuming no other overhead, and no fourth attempt happens because three is the total.
Practice and check
Common mistake
Mixing up retries and attempts. Note whether a count includes the first attempt. In real systems, account for an overall deadline as well as per-attempt timers.
Practice on a real repository
Try the first puzzle below, then read the policy conditions in the second.
- Exponential retry backoffFind · Easy · sindresorhus-got
- Retry-After versus backoffArchitecture · Medium · sindresorhus-got
Related topics: Retries and timeouts
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- RFC 9110: HTTP Semantics (IETF)
- Timeouts, retries and backoff with jitter (Amazon Builders' Library)