Retries with backoff
Read · 1 of 3
Most failures are temporary
APIs time out, rate limits kick in, a server restarts. Most of these errors go away if you simply try again.
But retry instantly and you hammer a service that's already struggling. Exponential backoff waits 1 second, then 2, then 4, then 8, giving it room to recover. And always cap the tries: some errors are permanent.
Read this lesson as text
Try again, but wait longer each time.
Most failures are temporary
APIs time out, rate limits kick in, a server restarts. Most of these errors go away if you simply try again.
But retry instantly and you hammer a service that's already struggling. Exponential backoff waits 1 second, then 2, then 4, then 8, giving it room to recover. And always cap the tries: some errors are permanent.
How long did we wait?
waited = 0
delay = 1
for attempt in range(1, 5):
waited += delay
delay *= 2
print(waited)Four waits, doubling each time from 1 second. Total?
- 15
- 16
- 8
Show the answer
15 1 + 2 + 4 + 8 = 15 seconds. Doubling grows fast, which is why you cap both the tries and the longest wait.
Exercise: Retry with backoff
Input: the most tries allowed and the first delay in seconds, then a pattern like FFS: what each try does (F fails, S succeeds).
After a failure (if another try is allowed) print try i: failed, waiting Ds and double the delay. On success print try i: ok (waited Ts total).
If the last allowed try fails, print try i: failed then gave up after N tries (waited Ts).