In ASP.NET Core, which five strategies does AddStandardResilienceHandler stack, in what order from the outermost, and why does that order matter?
answer
- outermost first, like middleware
- rate limiter, total timeout, retry
- breaker inside retry sees every attempt
- attempt timeout innermost, 10 s
- BrokenCircuitException is not retried
basics
~20 sFrom the outside in: rate limiter, total request timeout (30 s), retry, circuit breaker, attempt timeout (10 s). The total timeout bounds every retry, the breaker counts each attempt, and an open breaker fails fast because its rejection is not retried.
solid answer
~40 sThe builder adds, outermost first: a **rate limiter** (a concurrency limiter, 1,000 permits, no queue), a **total request timeout** (30 s), a **retry** (3 retries, exponential with jitter), a **circuit breaker** (10% failure ratio, 100 calls minimum, 30 s sampling, 5 s break) and an **attempt timeout** (10 s). Order decides behaviour. The total timeout sits outside the retry, so all attempts and backoff delays together fit in 30 seconds. The attempt timeout sits inside the breaker, so a hung attempt is cut at 10 seconds, counted as a failure by the breaker and retried. The breaker sits inside the retry, so it sees every attempt, and once open it throws `BrokenCircuitException`, which the retry's predicate does not handle, so callers fail fast. The rate limiter is outermost, so a rejection is never retried.
code
csharp · 9 linesbuilder.Services.AddHttpClient<PaymentGatewayClient>(c => c.BaseAddress = new Uri("https://gateway.example.com/"))
.AddStandardResilienceHandler()
.Configure(o =>
{
o.Retry.MaxRetryAttempts = 2; // retry (3rd layer)
o.CircuitBreaker.MinimumThroughput = 20; // breaker (4th layer)
o.AttemptTimeout.Timeout = TimeSpan.FromSeconds(5); // innermost
o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(20);
});go deeper
Recall the five strategies by name and the two headline timeouts, 30 seconds total and 10 seconds per attempt.
Explain outermost-first nesting and what each position means for retries, timeouts and the breaker's failure count.
Predict what a caller sees during a blip, a hang and an outage, and tune values without fighting the fixed order.
Decide when the fixed standard order is acceptable for a dependency and when a team should own a custom pipeline.
## Pipelines nest like middleware A Polly 8 pipeline is a stack of strategies. The first one added is the **outermost**: it sees the call first and the result last, and everything added after it runs inside it, once per time it decides to run its inner part. `AddStandardResilienceHandler()` builds exactly this stack for an `HttpClient`, in this order: 1. **Rate limiter**: a concurrency limiter from System.Threading.RateLimiting, 1,000 permits and a queue of 0. When full, it rejects at once with `RateLimiterRejectedException`. 2. **Total request timeout**: 30 seconds for the whole operation. Exceeding it throws `TimeoutRejectedException`. 3. **Retry**: 3 retries after the first attempt, exponential backoff from a 2-second base with jitter, honouring `Retry-After`. 4. **Circuit breaker**: opens when at least 10% of at least 100 calls in a 30-second window fail; stays open for 5 seconds before testing again. 5. **Attempt timeout**: 10 seconds per individual attempt, also throwing `TimeoutRejectedException`. One trap when reading the source: the remarks on `HttpStandardResilienceOptions` list the chain as "Bulkhead -> Total Request Timeout -> Retry -> Circuit Breaker -> Attempt Timeout" and then number a slightly different walk-through. The extension method's builder code, which adds the rate limiter first, is what actually runs. ## Why each position matters | Pair | Effect of the order | |---|---| | Total timeout outside retry | all attempts **plus** the backoff waits share one 30-second budget | | Attempt timeout inside retry | a hung attempt is abandoned at 10 s and, being transient, retried | | Breaker inside retry | each attempt is a sample for the breaker, so retries feed it data | | Attempt timeout inside breaker | slow attempts count as breaker failures, not just errors | | Rate limiter outermost | a rejected call never enters the retry, so overload is not amplified | ## What a caller observes In a shop that polls a flaky card-payment gateway for payment status, the order produces these outcomes: - **A brief 503 blip**: the first attempt gets 503, the retry waits about two seconds with jitter (or the `Retry-After` value), the second attempt succeeds. The caller sees 200. - **A hung gateway**: each attempt is cut at 10 s. With backoff waits added, the 30-second total timeout expires before all three retries run, and the caller gets a `TimeoutRejectedException`. - **A gateway outage**: failures across many calls trip the breaker. From then on the breaker throws `BrokenCircuitException` immediately. The retry's predicate handles only 5xx, 408, 429, `HttpRequestException` and `TimeoutRejectedException`, so it lets the breaker's rejection through and the caller fails within milliseconds instead of waiting. - **A burst of 1,500 simultaneous calls**: the first 1,000 go in; the rest are rejected by the limiter without touching the gateway. ## How to change it - Tune values, not order, with `.Configure(o => { o.Retry.MaxRetryAttempts = 2; o.AttemptTimeout.Timeout = TimeSpan.FromSeconds(5); })` on the builder the method returns. The options object exposes `RateLimiter`, `TotalRequestTimeout`, `Retry`, `CircuitBreaker` and `AttemptTimeout`. - To change the order or the set of strategies, build your own pipeline with `AddResilienceHandler`; the standard handler's order is fixed. - Remember that the breaker is per pipeline instance: one client means one breaker, unless the pipeline is selected per authority. ## Common misreadings - Thinking the 10-second attempt timeout is the overall limit, when three retries could otherwise run for far longer than 10 seconds. - Expecting the retry to keep trying while the breaker is open; it gives up at once because the breaker's exception is not transient. - Assuming the rate limiter throttles requests per second; the default is a concurrency limit, not a rate.
- Why is a TimeoutRejectedException from the attempt timeout retried, while one from the total timeout is not?The attempt timeout sits inside the retry, so its exception reaches the retry's predicate, which treats `TimeoutRejectedException` as transient. The total timeout sits outside the retry and cancels the whole operation, so nothing above it is left to retry.
- Why is a rejection from the standard handler's rate limiter never retried?The rate limiter is the outermost strategy, so its `RateLimiterRejectedException` is thrown before the retry is even entered; nothing outside it can retry. The exception is also outside the retry's transient set, which covers error statuses, `HttpRequestException` and timeouts. The caller sees the rejection at once, so an overload is not amplified by extra attempts.
- Says the attempt timeout is the overall time limit for the call
- Expects the retry to keep retrying while the circuit breaker is open
- Places the circuit breaker outside the retry in the standard handler
- Thinks the default rate limiter caps requests per second
- Believes the strategy order can be changed through HttpStandardResilienceOptions