Skip to main content

Exponential Backoff with Jitter

Python

Spread retries out so a recovering service isn't stampeded.

Z
Zainab Bello
Sample Oct 3, 2026
Python · 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import random
import time


def backoff(attempt: int, base: float = 0.5, cap: float = 30.0) -> float:
    return random.uniform(0, min(cap, base * 2 ** attempt))


for attempt in range(5):
    try:
        call_service()
        break
    except TimeoutError:
        time.sleep(backoff(attempt))
4 Fire 1 Learned something 2 Saved me time 1 Mind-blown
Sign in to react and star

Remixes (1)