
More Retries Can Mean a Bigger Outage: Backoff, Jitter and Retry Budgets
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
Retries are selfish
Layers multiply
int dbCalls = 0;bool Database() { dbCalls++; return false; } // overloaded: every call fails// A layer calls the one below it up to `attempts` timesFunc<bool> Layer(Func<bool> below, int attempts) => () => Enumerable.Range(0, attempts).Any(_ => below());// Gateway -> Orders -> Inventory -> Database, 3 attempts per hopLayer(Layer(Layer(Database, 3), 3), 3)();Console.WriteLine($"Every layer retries: {dbCalls} database calls");dbCalls = 0;Layer(Layer(Layer(Database, 3), 1), 1)(); // only Inventory retriesConsole.WriteLine($"One layer retries: {dbCalls} database calls");Retries that wait the same time arrive together
A per-request limit isn't a system limit
The fix: one layer, jitter and a budget
// Orders service. Gateway and Inventory have the same looppublic sealed class InventoryClient(HttpClient http){ public async Task<Stock> GetStockAsync(string sku, CancellationToken ct) { for (int attempt = 1; ; attempt++) { try { return (await http.GetFromJsonAsync<Stock>($"/stock/{sku}", ct))!; } catch (Exception) when (attempt < 3) // retries 404s too { await Task.Delay(1000, ct); // every client wakes up together } } }}using System.Net;// Only Orders retries Inventory; Gateway and Inventory fail fastpublic sealed class InventoryClient(HttpClient http, RetryBudget budget){ public async Task<Stock> GetStockAsync(string sku, CancellationToken ct) { budget.OnRequest(); for (int attempt = 1; ; attempt++) { try { return (await http.GetFromJsonAsync<Stock>($"/stock/{sku}", ct))!; } catch (HttpRequestException e) when (attempt < 3 && e.StatusCode is null or >= HttpStatusCode.InternalServerError && budget.TrySpend()) // false when the budget is spent { // Full jitter: a random wait up to the capped exponential delay double capMs = Math.Min(2_000, 100 * Math.Pow(2, attempt)); var waitMs = Random.Shared.NextDouble() * capMs; await Task.Delay(TimeSpan.FromMilliseconds(waitMs), ct); } } }}RetryBudget is one token bucket shared by every call to Inventory: each request adds a tenth of a token, each retry spends a whole one, so retries stay near 10% of traffic and stop when an outage drains the bucket. The call is a read, so it's safe to repeat; retrying writes needs the idempotency covered in the article on idempotent consumers.
How to apply it in your project
Count the retry layers For one user action, list every layer that retries: your loops, HTTP handlers, SDKs, database drivers and message redelivery. Multiply their attempts. Keep one layer Retry right above the dependency that fails, and make the others fail fast or degrade, such as showing a page without recommendations. Back off with jitter, within a deadline Use capped exponential backoff with full jitter, a small attempt limit, and a total time that fits what the user is waiting for. Add a budget and watch it Cap retries per dependency, for example to a tenth of requests, and alert when the retry rate climbs.
Key takeaways
Retries help with rare, independent failures and hurt when failures come from overload. Retries at several layers multiply: three layers of three attempts send 27 calls to the bottom. Backoff without jitter still produces synchronized waves; full jitter spreads them out. A per-request limit doesn't cap total load; a retry budget per dependency does. Retry at one layer, only errors that can succeed, and check what your libraries already retry.
📚 المصادر
Timeouts, retries, and backoff with jitter — Marc Brooker, Amazon Builders' Library (2019) Exponential Backoff And Jitter — Marc Brooker, AWS Architecture Blog (2015) Addressing Cascading Failures — Site Reliability Engineering, Google Handling Overload — Site Reliability Engineering, Google Build resilient HTTP apps: Key development patterns — .NET, Microsoft Learn
التعليقات (0)