6 Strategies to Build Resilience in ASP.NET Core

6 Strategies to Build Resilience in ASP.NET Core

Last Updated on May 25, 2025 by Aram

In the evolving world of cloud-native and distributed systems, resilience is no longer optional, it’s essential.

This article explores how developers can leverage the power of Polly via the new extensions to build resilience in ASP.NET Core

Starting from .NET 8 and built on top of Polly (v8), Microsoft.Extensions.Resilience and Microsoft.Extensions.Http.Resilience packages were added to allow you easily implement resilience mechanisms on HttpClient.

Check this document to learn more about Polly and the 6 strategies to build resilience in ASP.NET Core apps.

Introduction to Polly

Polly is a distinguished library that helps you build robust applications by enabling you to handle the transient faults

Transient faults are temporary issues or problems that arise due to network loss, service unavailability.

Such faults would normally resolve on their own after some time and won’t remain for a permanent period.

Recently, there have been 2 major releases of Polly: v7 and v8

Both are being supported in parallel

v8 is a major redesign of the Polly API with lots of enhancements and changes in the structure

API Design Changes from Polly v7 to v8

Here is a list that highlights the key changes that occurred to Polly going from v7 to v8:

  • It is async-first by default
  • Use of resilience strategy instead of resilience policy
  • ResiliencePipelineBuilder was introduced to align with the resilience strategy
  • Can easily combine multiple strategies using FluentBuilder
  • Better customization and readability with PredicateBuilder to define when a strategy applies
  • Easily configure the behavior with Strategy-specific options (like RetryStrategyOptions, TimeoutStrategyOptions)
  • Seamless integration with HttpClient and HttpClientFactory
  • Works seamlessly with the new resilience extensions within the Microsoft Library

Polly and the Resilience Extensions

Following the success of Polly, the recent version, v8, was re-architected to integrate with Microsoft.Extensions.Resilience and Microsoft.Extensions.Http.Resilience

These extensions provide an integrated set of revised and simplified APIs to help you leverage resilience strategies powered by Polly

Using Polly through Resilience Extensions

Add the resilience extensions NuGet Packages:

Then, inside the app builder services collection, define resilience strategy under a named HttpClient:

Now whenever you use the named HttpClient “TestApi” it will apply the resilience strategy that we defined earlier:

And of course when need to have the interface ready for the previous code to compile

6 Strategies for Resilience in .NET

Polly offers 6 strategies for resilience in .NET, listed under 2 main categories: Reactive and Proactive

Reactive Strategies

1. Retry

Retries a failed operation multiple times before giving up.

Useful when errors are temporary and a quick retry could succeed.

In retry you can set the max number of attempts, the delay before the first attempt, and what’s the interval of delay between each retry: constant, exponential, or linear

Best used with:

  • Transient network errors (e.g., HTTP 500, timeouts)
  • Temporary unavailability of a downstream service
  • Flaky third-party APIs

2. Circuit Breaker

Monitors failures and stops sending requests once failure rate crosses a threshold.

Gives the system time to recover.

Best used to:

  • Prevent hammering a failing system
  • Avoid exhausting resources on repeated errors
  • Enable graceful recovery and health checks

3. Fallback

When all else fails (retry, timeout, etc.), returns a safe default response instead of crashing.

Best when used to:

  • Gracefully handle complete failure
  • Return cached/stubbed data instead of throwing errors
  • Maintain user experience even when services are down

4. Timeout

Defines a maximum allowed time for an operation. If it takes too long, it’s forcefully canceled.

Best used to:

  • Protect against long-hanging requests
  • Prevent a system from freezing due to stuck dependencies
  • Maintain snappy UX and system responsiveness

5. Hedging

Sends backup/parallel requests after a short delay if the primary request is taking too long.

Improves response speed for unreliable services.

Best used with:

  • High-latency or unreliable endpoints
  • Mission-critical systems where waiting too long is costly
  • Redundant endpoints or mirror services

6. Rate Limiter

Controls the number of allowed calls per time window.

Helps protect services from being overwhelmed by traffic.

Other modes include:

  • Concurrency(n)
  • TokenBucket(…)
  • FixedWindow(…)

Strong Resilience Pipeline Setup

This is how you can change the strategies all together:

Here’s an example combining:

  1. Timeout – Fail fast if too slow.
  2. Retry – Retry on transient errors.
  3. Circuit Breaker – Stop calling a broken service.
  4. Fallback – Provide default or cached response if all else fails.

Summary

In this article, we explored how to build resilient .NET applications using Polly v8 through the Microsoft.Extensions.Resilience and Microsoft.Extensions.Http.Resilience packages. We discussed each of the core resilience strategies—Retry, Circuit Breaker, Fallback, Timeout, Hedging, and Rate Limiter, and demonstrated how to configure them using the new pipeline-based builder syntax of the resilience extensions.

Through practical examples, we showed how to:

  • Automatically retry transient HTTP failures
  • Protect systems under failure using circuit breakers
  • Return controlled fallback responses
  • Prevent long-hanging requests with timeouts
  • Improve response times with hedged calls
  • Throttle request flow via rate limiting

By integrating these strategies using the new built-in extensions (introduced in .NET 8), developers can create robust, fault-tolerant, and cloud-ready systems without manually managing Polly’s complexity.

The new approach encourages composability, observability, and strong alignment with modern .NET development practices.

References

You can always learn more by reading the official docs and checking other articles and tutorials:

Polly Official Docs

Introduction to resilient app development

Build resilient HTTP apps: Key development patterns

Check my latest article: Your Quick Guide to JWT

Bonus

Here is a recommended listen for a beautiful rendition for Chopin’s 5 Most Popular Pieces

Leave a Reply