Retry with exponential backoff and jitter

Python

Backs off on each failure and spreads retries out, so a recovering service is not hit by every client at once.

import random, time

def retry(fn, attempts=5, base=0.5):
    for n in range(attempts):
        try:
            return fn()
        except TransientError:
            if n == attempts - 1:
                raise
            time.sleep(base * 2 ** n + random.random() * 0.1)

More in Python

Random picks