Resilience
Core Principles
- Polly v8 resilience pipelines, not v7 policies — Polly v8 replaced
PolicywithResiliencePipeline. Never usePolicyBuilder,Policy.Handle<>(), orISyncPolicy. The new API is composable, type-safe, and integrates natively withIHttpClientFactory. - Configure via
AddResilienceHandler, not manual wrapping — For HTTP calls, useMicrosoft.Extensions.Http.Resiliencewhich adds pipelines directly toHttpClientvia DI. No manualExecuteAsyncwrapping. - Compose strategies, don't nest them — A single
ResiliencePipelinecan chain retry + circuit breaker + timeout. Strategies execute outer-to-inner (first added = outermost). No need for nested try/catch or manual orchestration. - Always set timeouts — Every external call needs a timeout. Use Polly's
AddTimeout()as the innermost strategy so it applies per-attempt, and optionally an outer timeout for total elapsed time. - Instrument everything — Polly v8 emits
Meteringevents and supportsTelemetryOptionsfor OpenTelemetry. Use them to monitor retry rates, circuit breaker state, and timeout frequency.
Patterns
HTTP Client Resilience (Recommended Default)
Why: AddStandardResilienceHandler() from Microsoft.Extensions.Http.Resilience applies production-ready defaults. Override only when you need different thresholds.
Custom HTTP Resilience Configuration
Why: Named resilience handlers let you tune per-service. The order matters: total timeout > retry > circuit breaker > attempt timeout.
Non-HTTP Resilience Pipeline
Why: AddResiliencePipeline registers a named pipeline in DI. Inject with [FromKeyedServices] for clean, testable code.
Typed Resilience Pipeline
Why: Typed pipelines let you add fallback strategies that return a default value when all retries are exhausted — critical for graceful degradation.
Hedging (Parallel Requests)
Why: Hedging sends a parallel request if the first hasn't responded within the delay. Use for latency-sensitive reads where you can tolerate duplicate work.
Telemetry Integration
Rate Limiting (.NET Built-in)
.NET provides built-in rate limiting middleware via AddRateLimiter() — no external packages needed. Algorithms: AddFixedWindowLimiter, AddSlidingWindowLimiter, AddTokenBucketLimiter, AddConcurrencyLimiter.


