Resilience budgets
Resilience budgets
Resilience engineering keeps failures bounded. Retry, timeout, circuit breaker and concurrency limits solve different problems; compose them around the business operation and its overall time budget.
A retry repeats an operation only if its failure is classified as transient and repetition is safe. HTTP GET is typically safe and idempotent by contract; a POST may need an idempotency key and server-side deduplication. Never blindly retry a side effect when outcome is unknown. A timeout limits how long the caller waits, not whether remote processing stopped. A total deadline bounds all attempts and backoff; per-attempt timeout must fit the remaining time. Use monotonic elapsed time (Stopwatch or TimeProvider timestamp), and honor caller cancellation even during delay. Retry budgets also constrain aggregate traffic; cap concurrent requests, avoid nested retry amplification, use jitter and honor Retry-After within the deadline. A circuit breaker opens after a measured failure threshold, rejects calls for a cooldown, then permits limited half-open probes; a successful probe can close it. Use minimum throughput and sampling windows in production, not a permanent global boolean. Distinguish cancellation by caller, timeout, overload, permanent error and transient failure in metrics. Cache fallback may return explicitly acceptable stale content, not fake successful writes. Microsoft.Extensions.Resilience provides configurable resilience pipelines; Microsoft.Extensions.Http.Resilience provides HTTP-specific handlers including AddStandardResilienceHandler. Its standard handler composes limiting, total timeout, retries, breaker and per-attempt timeout; verify exact selected package options before use. Configure unsafe HTTP method retries explicitly (DisableForUnsafeHttpMethods is one conservative option). Do not combine handlers blindly. The following finite simulations illustrate budgets and states, not substitute implementations for Polly or production libraries.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Models and isolated local integration fixtures are explicitly distinguished.
Keep attempts inside a total budget
Assume a synthetic cost of 80ms per attempt and a 40ms delay after failures. No sleeping or real clock is involved.
Use the archived Fixture.csproj with an approved .NET 10 SDK. Inspect dependency graph, restore only approved dependencies, build and run.
using System;int elapsed = 0, budget = 250, cost = 80, delay = 40;for (int attempt = 1; attempt <= 4; attempt++) { if (elapsed + cost > budget) { Console.WriteLine("stop: insufficient budget"); break; } elapsed += cost; Console.WriteLine($"attempt {attempt}: elapsed={elapsed}ms"); if (attempt == 4 || elapsed + delay + cost > budget) { Console.WriteLine("stop: insufficient budget"); break; } elapsed += delay;}Console.WriteLine($"total={elapsed}ms");Expected result (not executed):
attempt 1: elapsed=80msattempt 2: elapsed=200msstop: insufficient budgettotal=200msThe model reserves the next known cost before waiting. Production costs are not known in advance; allocate a per-attempt timeout from the remaining elapsed deadline.
Trace open and half-open breaker states
An explicit monotonic clock and deterministic failure outcomes. This simplified sequential breaker permits one probe after cooldown, without a sampling window.
Use the archived Fixture.csproj with an approved .NET 10 SDK. Inspect dependency graph, restore only approved dependencies, build and run.
using System;int consecutiveFailures = 0, openUntil = -1;void Call(int now, bool healthy) { if (openUntil >= 0 && now < openUntil) { Console.WriteLine($"{now}: rejected"); return; } bool probe = openUntil >= 0; if (healthy) { consecutiveFailures=0; openUntil=-1; Console.WriteLine($"{now}: {(probe ? "probe closed" : "success")}"); return; } consecutiveFailures++; if (probe || consecutiveFailures >= 2) { openUntil=now+5; Console.WriteLine($"{now}: opened"); } else Console.WriteLine($"{now}: failure");}Call(0,false); Call(1,false); Call(2,true); Call(6,true); Call(7,true);Expected result (not executed):
0: failure1: opened2: rejected6: probe closed7: successThe rejected call never reaches the dependency. At the cooldown boundary the probe succeeds. This model is not thread-safe and cannot coordinate multiple replicas.
Retry only a known safe operation
A fake dependency reports a transient fault on its first call and success on its second. No actual HTTP calls are performed.
Use the archived Fixture.csproj with an approved .NET 10 SDK. Inspect dependency graph, restore only approved dependencies, build and run.
using System;using System.Net.Http;foreach (HttpMethod method in new[] { HttpMethod.Get, HttpMethod.Post }) { bool retryAllowed = method == HttpMethod.Get; for (int attempt=1; attempt<=2; attempt++) { bool success = attempt == 2; if (success) { Console.WriteLine($"{method}: success on {attempt}"); break; } if (!retryAllowed || attempt==2) { Console.WriteLine($"{method}: stop after {attempt}"); break; } Console.WriteLine($"{method}: transient failure, retry"); }}Expected result (not executed):
GET: transient failure, retryGET: success on 2POST: stop after 1This deliberately conservative model distinguishes operation semantics rather than treating status codes as permission to retry. An idempotency contract must be enforced at the server, not merely a header added by a client.
Apply the concept
Design an HTTP policy for a stock read with a 300ms end-to-end budget and a purchase command with ambiguous timeout. Include open breaker, caller cancellation and overload.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Resilience budgets — knowledge check
Apply the lesson concepts to the following scenarios.
Which budget must include retry delays?
What should an open circuit breaker do?
What is half-open for?
Why avoid automatic retries of ambiguous POST outcomes?
What should happen when the caller cancels during backoff?