Cloud
Retry with Exponential Backoff
It takes O(1) work to compute each delay — you're just doubling a number. But the delays themselves grow fast (1, 2, 4, 8, ...), and that's the whole point: it spreads retries out over time.
The idea, in plain English
If you call a friend and they don't pick up, you don't redial ten times in a row right away. You wait a bit, try again, and if that fails too, you wait even longer next time. That's 'exponential backoff,' and computers use the same trick. After a failed call to another service, your app waits a short time before retrying. Then it doubles that wait every time the call fails again — 1 second, 2 seconds, 4 seconds, and so on. This stops you from flooding an already-struggling service with retries.
How it works
- 1Try the operation. If it succeeds, you're done.
- 2If it fails, work out how long to wait before the next try. Start with a base delay, then double it after every failure (1s, 2s, 4s, 8s, and so on).
- 3Retry after that delay, up to a set maximum number of tries. If it still hasn't succeeded by then, give up and report the failure.
When you'd use it
Use this when you call something over a network that fails now and then for temporary reasons, like a brief network hiccup or a service that's briefly overloaded. Backing off gives it time to recover instead of piling on more load right when it's struggling.
Common beginner mistakes
- Retrying right away with no delay at all. This can make an overloaded service even worse (a 'retry storm').
- Forgetting to set a maximum number of tries. Without one, a permanently broken service gets retried forever.
- Actually pausing the program (sleeping) inside example code just to prove the delay works. In real systems, you compute the delay and schedule the retry. You don't freeze the whole program while you wait.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Attempt 1: failed, waiting 1s before retry
Attempt 2: failed, waiting 2s before retry
Attempt 3: failed, waiting 4s before retry
Attempt 4: succeededNot sure this is the right topic? See the learning paths → or where this leads →