Skip to content
Coding · Easy · Retries · Target O(attempts) · Mid

Exponential backoff with jitter

Produce the sleep schedule for a retrying client: exponential growth, a cap, and full jitter, without overflowing on large attempt counts.

Statement

Write retry_schedule(base_ms, cap_ms, attempts, jitter) that returns the sleep before each retry. Backoff doubles from base_ms and is capped at cap_ms. With jitter = "full", each sleep is a uniform random value in [0, backoff).

The function is used by thousands of clients at once, so think about what happens to the backend when they all retry on the same schedule.

Examples

base_ms = 100, cap_ms = 2000, attempts = 6, jitter = none

→ [100, 200, 400, 800, 1600, 2000]

base_ms = 100, cap_ms = 2000, attempts = 3, jitter = full, rand = 0.5
→ [50, 100, 200]

Constraints

attempts up to 10⁴. base_ms × 2^attempts must not overflow or produce inf; the cap applies first. rand is injected so the schedule is testable.

Free to try · sign in to save your attemptSolve it in the editor with visible tests
Open in the app