Backoff and jitter
Wait, and wait longer
Section titled “Wait, and wait longer”The last lesson ended with a retry that fires the moment an attempt fails. That is the worst moment for a dependency that is struggling: it is hit again while it has had no time to recover. The remedy is to wait before retrying, and to wait longer each time. Exponential backoff doubles the wait after every failure, so the first retry is quick, in case the failure was a blip, and later ones come further apart if it was not.
The lab’s exponential backoff starts at 0.5 s and doubles up to a ceiling of 4 s, so the waits before the four retries are 0.5, 1, 2 and 4 s. A request that uses all of them has waited 0.5 + 1 + 2 + 4, which is 7.5 s, before its fifth attempt. The ceiling matters: Amazon’s article describes capped exponential backoff, so that the waits stay bounded. Polly’s retry strategy has the same options: a backoff type of constant, linear or exponential, a maximum delay, and a switch for jitter. Its defaults are three retries after the first attempt and a constant delay of 2 s.
Mental model. Backoff decides how long each client waits. It does nothing about clients that all started waiting at the same moment, and they all wake up at the same moment.
Clients that fail together retry together
Section titled “Clients that fail together retry together”Suppose a dependency fails and 30 requests that arrived at the same instant all fail at the same instant. Each waits 0.5 s, and all 30 retry together. They fail again together, wait 1 s, and retry together again. Backoff has spaced the waves out and made them rarer, but each wave is as tall as the one before. Amazon’s article tested this with clients competing for one database row: with exponential backoff alone the calls clustered at particular moments, and adding jitter spread them out.
Jitter makes each wait random. The article gives the formulas for the delay d that a backoff would use: full jitter waits a random time between 0 and d, and equal jitter waits d / 2 plus a random time between 0 and d / 2. The lab has both, in the Jitter setting as Full and Equal, with None as the plain backoff.
Try it: waves and spread
Section titled “Try it: waves and spread”Both labs below send 30 requests in one burst at the start of every second, which is what many clients on the same schedule do. The dependency goes silent from 5 s to 13 s, each attempt waits a 1 s timeout, and a request may retry four times. The first lab uses exponential backoff with no jitter.
Requests arrive in bursts and wait 0.5, 1, 2 and 4 seconds between attempts. Requests that failed together wait the same time, so they retry together and the load comes back in waves.
While the dependency is down, every attempt fails.
Share of attempts that fail on their own, even when the dependency is up.
Attempts the dependency can serve in any one second. It turns the rest away.
An error comes back after 0.1 s. With no answer, the caller waits the whole timeout before it can retry.
Each layer calls the next one and retries that call. The last layer calls the dependency.
How long a caller waits for an answer from the dependency.
The wait before a retry. None retries at once, fixed waits 0.5 s every time, and exponential starts at 0.5 s and doubles each time, up to 4 s.
Jitter makes each wait random. Full picks anywhere from zero to the delay, equal picks from half the delay to the delay. With no backoff there is no wait to randomise.
Each layer may retry only a share of the calls it makes, after a reserve of ten retries.
Closed, calls go through. After enough failures in a row it opens and calls fail at once. After a pause it is half-open and lets one probe through.
The lab runs in real time. Step moves on by one tenth of a second, Skip by five seconds.
What is happening
The dependency is down from 5 s to 13 s. Every attempt made now fails.
The caller makes up to 5 attempts, so one user request can become up to 5 attempts at the dependency.
In the last second, user requests: 30; attempts at the dependency: 90; served: 0. The dependency can serve 60 a second.
Without jitter, requests that fail together wait the same time and retry together, in waves. Attempts in the busiest tenth of a second of the last second: 60.
- User requests
- 300150 succeeded, 0 gave up, 150 still trying
- Attempts at the dependency
- 4201.4 for each user request, the most for one: 3
- At most for one request
- 5attempts at the dependency: 5
- Attempts, last second
- 90served: 0, capacity: 60, busiest tenth of a second: 60
- Circuit breaker
- Offswitched off
- Retries refused
- 0no retry budget
- Attempts (line)
- Served (filled)
- Outage (hatched)
| Seconds | New requests | Attempts | Served | Busiest tenth of a second | Dependency |
|---|---|---|---|---|---|
| 9 to 10 | 30 | 90 | 0 | 60 | Down |
| 8 to 9 | 30 | 60 | 0 | 30 | Down |
| 7 to 8 | 30 | 60 | 0 | 30 | Down |
| 6 to 7 | 30 | 30 | 0 | 30 | Down |
| 5 to 6 | 30 | 30 | 0 | 30 | Down |
| 4 to 5 | 30 | 30 | 30 | 30 | Up |
| 3 to 4 | 30 | 30 | 30 | 30 | Up |
| 2 to 3 | 30 | 30 | 30 | 30 | Up |
| Call | Attempts so far | For each user request | At most for one request |
|---|---|---|---|
| Layer 1 to the dependency | 420 | 1.4 | 5 |
Circuit breaker log
The circuit breaker is switched off.
At 10 s, user requests so far: 300 (150 succeeded, 0 gave up, 150 still trying). Attempts at the dependency: 420, 1.4 for each request. Circuit breaker: Off.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances in steps of 0.1 s. An attempt that succeeds, or that is answered with an error, takes one step, and a retry is sent on the step after the failure at the earliest, so no backoff means 0.1 s. A seeded random draw decides each failure rate and each jitter, so the same settings give the same run.
- Each layer calls the next one and retries that call. The layers above wait for the answer of the layer below, and only the attempt at the dependency has a timeout. Real services usually have a timeout at every layer, which leaves work running below after a caller has given up and can make the amplification worse than shown.
- The dependency fails in three ways: an outage window, a failure rate, and a capacity (it serves a fixed number of attempts in any one second and turns the rest away). A failure is either an error at once or silence until the timeout; real failures are a mix of both. A turned-away attempt costs the dependency nothing.
Simplifications
- A retry budget gives each layer a bucket that fills with a share of the calls it makes and loses one for each retry, starting with ten retries in reserve. One circuit breaker guards the calls to the dependency: it opens after a number of failures in a row, lets one probe through after a pause, and closes when the probe succeeds. Real libraries differ in how they count failures and how many probes they allow.
- No real client library and no connection-level effect is modelled: no connection set-up, no connection pool, no queueing in the network, no load balancer choosing another instance. The numbers illustrate the mechanism; they are not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
The lab opens at 10 s. The status line says that without jitter, requests that fail together retry together, and that the busiest tenth of a second of the last second had 60 attempts. Attempts, last second shows 90 for the whole second, so 60 of them came in a single tenth of a second. Now the same traffic with full jitter.
The same bursts and the same backoff, but each wait is a random part of the delay. The retries are the same in number and arrive spread out instead of in waves.
Steady spreads the requests evenly. In bursts, a second of requests arrives all at once, as when many clients act on the same schedule.
While the dependency is down, every attempt fails.
Share of attempts that fail on their own, even when the dependency is up.
Attempts the dependency can serve in any one second. It turns the rest away.
An error comes back after 0.1 s. With no answer, the caller waits the whole timeout before it can retry.
Each layer calls the next one and retries that call. The last layer calls the dependency.
How long a caller waits for an answer from the dependency.
The wait before a retry. None retries at once, fixed waits 0.5 s every time, and exponential starts at 0.5 s and doubles each time, up to 4 s.
Jitter makes each wait random. Full picks anywhere from zero to the delay, equal picks from half the delay to the delay. With no backoff there is no wait to randomise.
Each layer may retry only a share of the calls it makes, after a reserve of ten retries.
Closed, calls go through. After enough failures in a row it opens and calls fail at once. After a pause it is half-open and lets one probe through.
The lab runs in real time. Step moves on by one tenth of a second, Skip by five seconds.
What is happening
The dependency is down from 5 s to 13 s. Every attempt made now fails.
The caller makes up to 5 attempts, so one user request can become up to 5 attempts at the dependency.
In the last second, user requests: 30; attempts at the dependency: 98; served: 0. The dependency can serve 60 a second.
Jitter makes each wait random, so requests that failed together retry at different times. Attempts in the busiest tenth of a second of the last second: 35.
- User requests
- 300150 succeeded, 0 gave up, 150 still trying
- Attempts at the dependency
- 4511.5 for each user request, the most for one: 4
- At most for one request
- 5attempts at the dependency: 5
- Attempts, last second
- 98served: 0, capacity: 60, busiest tenth of a second: 35
- Circuit breaker
- Offswitched off
- Retries refused
- 0no retry budget
- Attempts (line)
- Served (filled)
- Outage (hatched)
| Seconds | New requests | Attempts | Served | Busiest tenth of a second | Dependency |
|---|---|---|---|---|---|
| 9 to 10 | 30 | 98 | 0 | 35 | Down |
| 8 to 9 | 30 | 83 | 0 | 34 | Down |
| 7 to 8 | 30 | 60 | 0 | 30 | Down |
| 6 to 7 | 30 | 30 | 0 | 30 | Down |
| 5 to 6 | 30 | 30 | 0 | 30 | Down |
| 4 to 5 | 30 | 30 | 30 | 30 | Up |
| 3 to 4 | 30 | 30 | 30 | 30 | Up |
| 2 to 3 | 30 | 30 | 30 | 30 | Up |
| Call | Attempts so far | For each user request | At most for one request |
|---|---|---|---|
| Layer 1 to the dependency | 451 | 1.5 | 5 |
Circuit breaker log
The circuit breaker is switched off.
At 10 s, user requests so far: 300 (150 succeeded, 0 gave up, 150 still trying). Attempts at the dependency: 451, 1.5 for each request. Circuit breaker: Off.
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- The lab advances in steps of 0.1 s. An attempt that succeeds, or that is answered with an error, takes one step, and a retry is sent on the step after the failure at the earliest, so no backoff means 0.1 s. A seeded random draw decides each failure rate and each jitter, so the same settings give the same run.
- Each layer calls the next one and retries that call. The layers above wait for the answer of the layer below, and only the attempt at the dependency has a timeout. Real services usually have a timeout at every layer, which leaves work running below after a caller has given up and can make the amplification worse than shown.
- The dependency fails in three ways: an outage window, a failure rate, and a capacity (it serves a fixed number of attempts in any one second and turns the rest away). A failure is either an error at once or silence until the timeout; real failures are a mix of both. A turned-away attempt costs the dependency nothing.
Simplifications
- A retry budget gives each layer a bucket that fills with a share of the calls it makes and loses one for each retry, starting with ten retries in reserve. One circuit breaker guards the calls to the dependency: it opens after a number of failures in a row, lets one probe through after a pause, and closes when the probe succeeds. Real libraries differ in how they count failures and how many probes they allow.
- No real client library and no connection-level effect is modelled: no connection set-up, no connection pool, no queueing in the network, no load balancer choosing another instance. The numbers illustrate the mechanism; they are not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
Here Attempts, last second is 98, and the busiest tenth of a second has 35. The second lab made slightly more attempts in that second, spread across it instead of bunched. Press Skip 5 s twice in each lab, to 20 s, and read the table’s Busiest tenth of a second column for the eight seconds it shows. Without jitter the column reaches 90, which is three times the 30 requests of a burst, all in one tenth of a second. With jitter it never goes above 38.
The totals tell the other half. At 10 s the first lab has made 420 attempts at the dependency and the second 451. At 20 s the figures are 1,440 and 1,441. Jitter did not reduce the number of retries; the two labs make the same ones. It changed when they arrive: instead of tall peaks, the load in any tenth of a second stays low.
The same idea in code
Section titled “The same idea in code”The C# example below is a retry policy with the pieces of this lesson: exponential backoff with full jitter, a cap on the delay and on the attempts, and a rule that only transient failures of operations that are safe to repeat are retried. It runs on a fake clock, so the waits are instants, and the random generator is seeded so that the waits are the same on every run.
// Retries done carefully: exponential backoff with full jitter, a cap on attempts, a shared retry budget,// and a rule that only transient errors on idempotent calls are retried.using ShardCraft.Playground;
var clock = new FakeClock();var jitter = new Random(7); // seeded, so the "random" waits are the same on every runvar policy = new RetryPolicy(clock, jitter, baseDelayMs: 100, capMs: 2_000, maxAttempts: 4, retryBudget: 6);
// 1. Two transient failures, then success. Safe to retry: it is a read.Run("read, 2 transient failures", new FlakyService(clock, "tto"), isIdempotent: true);
// 2. A permanent error. Retrying a 400 only repeats it, so the first failure is final.Run("read, permanent error", new FlakyService(clock, "p"), isIdempotent: true);
// 3. A transient error on a write with no idempotency key. A retry might charge twice, so it is not retried.Run("write without a key", new FlakyService(clock, "t"), isIdempotent: false);
// 4. The same write with an idempotency key: now a retry is safe.Run("write with a key", new FlakyService(clock, "to"), isIdempotent: true);
// 5. The dependency is down for good: stop after maxAttempts, and the budget shrinks by what was spent.Run("dependency down", new FlakyService(clock, "tttttttt"), isIdempotent: true);
// 6. Another call during the outage: the budget has run out, so it gets one try and no retries.Run("budget exhausted", new FlakyService(clock, "tttttttt"), isIdempotent: true);
Console.WriteLine($"retry budget left: {policy.BudgetLeft}");
void Run(string name, FlakyService service, bool isIdempotent){ var start = clock.UtcNow; Console.WriteLine($"-- {name}"); var outcome = policy.Execute(service.Call, isIdempotent, (attempt, waitMs, e) => Console.WriteLine($" attempt {attempt} failed ({e.Message}), wait {waitMs} ms")); Console.WriteLine($" => {outcome} after {service.Calls} call(s), {(clock.UtcNow - start).TotalMilliseconds:0} ms");}
class RetryPolicy(FakeClock clock, Random jitter, int baseDelayMs, int capMs, int maxAttempts, int retryBudget){ public int BudgetLeft { get; private set; } = retryBudget;
public string Execute(Func<string> call, bool isIdempotent, Action<int, int, Exception> onFailure) { for (var attempt = 1; ; attempt++) { try { return call(); } catch (PermanentFailureException e) { return $"gave up: {e.Message} (not retryable)"; } catch (TransientFailureException e) { if (!isIdempotent) return $"gave up: {e.Message} (not safe to repeat)"; if (attempt >= maxAttempts) return $"gave up: {e.Message} (max attempts)"; if (BudgetLeft == 0) return $"gave up: {e.Message} (retry budget empty)"; BudgetLeft--;
// Full jitter: wait a random time between 0 and min(cap, base * 2^attempt), so clients that failed // together do not come back together. var ceiling = Math.Min(capMs, baseDelayMs * (1 << (attempt - 1))); var waitMs = jitter.Next(0, ceiling + 1); onFailure(attempt, waitMs, e); clock.Advance(TimeSpan.FromMilliseconds(waitMs)); // virtual time: no real waiting } } }}
// A real client also puts a timeout on every attempt, honours a Retry-After header, and counts retries across// all calls (a budget of, say, 10 % of requests) so that a failing dependency is not hit by a storm of retries.-- read, 2 transient failures attempt 1 failed (503 service unavailable), wait 38 ms attempt 2 failed (503 service unavailable), wait 175 ms => reply 3 after 3 call(s), 213 ms-- read, permanent error => gave up: 400 bad request (not retryable) after 1 call(s), 0 ms-- write without a key => gave up: 503 service unavailable (not safe to repeat) after 1 call(s), 0 ms-- write with a key attempt 1 failed (503 service unavailable), wait 66 ms => reply 2 after 2 call(s), 66 ms-- dependency down attempt 1 failed (503 service unavailable), wait 5 ms attempt 2 failed (503 service unavailable), wait 73 ms attempt 3 failed (503 service unavailable), wait 271 ms => gave up: 503 service unavailable (max attempts) after 4 call(s), 349 ms-- budget exhausted => gave up: 503 service unavailable (retry budget empty) after 1 call(s), 0 msretry budget left: 0This is the exact code that produced this output, run with the .NET SDK when the site was built. Nothing runs in your browser.
The delay ceiling starts at 100 ms and doubles: 100, 200, 400, 800, 1,600 ms, and the cap of 2,000 ms would apply from the sixth wait, which a policy of 4 attempts never reaches. Each wait is a random value between 0 and the ceiling. In the first run the dependency fails twice, then answers. The two waits are 38 ms, below 100, and 175 ms, below 200, and the request ends after 3 calls and 213 ms. In the run where the dependency never recovers, the waits are 5, 73 and 271 ms, and the policy gives up after 4 calls and 349 ms. A permanent error, the 400, is not retried at all. A write with no key is not retried either, because a retry could charge twice; the same write with an idempotency key is. The last two runs show a retry budget, which the next lesson is about. The code in the page is never run in the browser: the output was produced by a CI run of the same file.
What happens if jitter makes a wait almost zero?
Section titled “What happens if jitter makes a wait almost zero?”Full jitter picks a wait anywhere from 0 up to the delay, so some waits are close to nothing. Look at User requests in the two labs at 20 s. Without jitter it says 540 succeeded, none gave up and 60 are still trying. With jitter it says 528 succeeded, 40 gave up and 32 are still trying. A request has four retries, and in the lab a retry is never sent sooner than a tenth of a second after the failure. With full jitter a request that draws short waits spends its four retries early, inside the outage, and gives up. Without jitter the waits are always the full 0.5, 1, 2 and 4 s. A request that starts at 5 s makes its fifth attempt 4 × 1 + 7.5, which is 11.5 s later, four timeouts of 1 s and 7.5 s of waiting, so after the outage has ended at 13 s. Equal jitter, which waits at least half the delay, avoids the near-zero waits. The spread is not free: it also gives up some of the waiting that backoff provided.
- Backoff waits longer after each failure: 0.5, 1, 2 and 4 s in the lab, 7.5 s in all, and a cap so that the wait stays bounded.
- Clients that fail together retry together. Backoff alone moves the waves and keeps their height: 60 attempts in one tenth of a second at 10 s, and up to 90 by 20 s.
- Jitter makes each wait random. The busiest tenth of a second fell to 35 at 10 s and never rose above 38 by 20 s, while the totals stayed the same, 1,440 attempts without and 1,441 with.
- Full jitter waits from 0 to the delay and equal jitter from half the delay to the delay. A wait near zero can use up a request’s retries early.
- The C# example retries only transient failures of operations that are safe to repeat, waits 38 ms and 175 ms in its first run, and stops after 4 attempts.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.