Computing Exponential Backoff

Concepts covered: Retry & Failure Handling

Exponential backoff is a formula, and you should be able to run it in your head before you trust a library to run it for you. After the n-th consecutive failure, the wait before the next attempt is wait = min(cap, base * 2 ** (n - 1)). base is the wait after the first failure. The exponent doubles it with each further failure. cap is the ceiling the wait can never exceed. With the animation's settings, base is 1 second and cap is 60 seconds.

Libraries differ on whether the first failure is numbered 0 or 1. Check which before comparing numbers; the shape of the curve is the same either way.

Failure numberbase * 2^(n-1)After the 60 s capCumulative sleep
11 s1 s1 s
22 s2 s3 s
34 s4 s7 s
48 s8 s15 s
516 s16 s31 s
632 s32 s63 s
764 s60 s123 s
8128 s60 sNo sleep: attempt 8 was the last of the budget

Doubling beats adding a second each time because a linear ramp of 1, 2, 3, 4 seconds grows too slowly to matter: after 10 failures a worker is still calling every 10 seconds, and 100 such workers still send 10 calls per second into a service that may need a minute of quiet. Doubling reaches a full minute between calls by the seventh failure.

The cap keeps the wait finite

Without the cap the doubling never stops. By the eleventh failure the computed wait passes a quarter of an hour, and by the twentieth it is measured in days. A worker sleeping that long is indistinguishable from a hung task, and no outage is helped by a client that checks back next week. The cap turns the curve into a ramp with a flat top: doubling while the service most needs room, then a steady polling interval of 60 seconds once the wait is long enough. With the cap in place, the 7 waits in the table add up to about 2 minutes before the eighth and final attempt.

Jitter: a random point inside the window

Full jitter replaces the computed wait with a random draw between 0 and that wait: sleep = random.uniform(0, min(cap, base * 2 ** (n - 1))). The capped value becomes the top of a window rather than the wait itself. After the third failure a worker no longer sleeps 4 seconds; it sleeps somewhere between 0 and 4 seconds, and the 100 workers in a wave each draw their own number. Here are 3 workers that failed at the same moment, across their first 4 failures.

FailureWindow (0 to)Worker A drawsWorker B drawsWorker C draws
11 s0.8 s0.2 s0.5 s
22 s1.7 s0.4 s1.1 s
34 s0.3 s3.6 s2.2 s
48 s6.9 s1.5 s4.8 s

The average sleep is half the window, so jitter also shortens the typical wait. The cap still matters, because the window stops at 60 seconds and no draw exceeds it. Each worker draws fresh on every attempt, which breaks the synchronisation. Workers A and B failed in the same millisecond, but their later attempts are independent, so they collide only by chance rather than every time.

import random


def backoff_seconds(failures, base=1.0, cap=60.0):
    raw = base * 2 ** (failures - 1)
    window = min(cap, raw)
    return random.uniform(0, window)

This function is the whole policy. Call it with the number of consecutive failures so far, sleep for what it returns, and stop calling it once the attempt budget is spent. The retry loop from the previous section needs 1 change: replace the constant WAIT_SECONDS with backoff_seconds(attempt).

A retry policy uses base 0.5 s, cap 30 s and full jitter. A worker has failed for the 6th consecutive time. Which statement about its next sleep is right?

About this lesson

This section is part of the Failure Handling: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.