Resilient Microservices in.NET: Combining Polly Circuit Breakers with Refit

One of the most frequent causes of failure while developing contemporary.NET microservices is out-of-process HTTP communication. High latency, service deployments, and network issues can quickly propagate across your system, resulting in thread hunger and extensive failures.

Polly v8, the standard resilience framework in.NET 8+, when used with Refit, a strongly-typed REST client library, provides a clean, production-grade way to use the Circuit Breaker pattern to safeguard your microservices against cascade failures.

The Problem: Cascading HTTP Failures

Imagine an Order Microservice calling a Payment Microservice via HTTP. If the Payment service goes down or experiences a database deadlock, incoming requests to the Order service will hang while waiting for responses.

Without a circuit breaker:

  1. Every new order request allocates a thread to call the failing Payment API.
  2. Threads quickly pile up, consuming CPU and RAM.
  3. The Order service eventually runs out of thread-pool resources, crashing entirely—even for endpoints that don’t depend on the Payment service.

The Solution: The Circuit Breaker Pattern

A Circuit Breaker monitors outgoing HTTP traffic and automatically short-circuits requests when a dependency fails repeatedly:

  • Closed State (Normal): Requests flow straight through to the downstream service.
  • Open State (Short-Circuited): When the failure threshold is breached (e.g., >50% failure rate), calls fail instantly locally with a BrokenCircuitException. No network calls are made, preserving local CPU resources and preventing thread starvation.
  • Half-Open State (Trial): After a cooldown period, trial requests are allowed through to test if the downstream service has recovered.

Step-by-step Implementation

1. Define the Refit Client Interface

Refit eliminates boilerplate HTTP code by mapping C# interfaces directly to HTTP endpoints:

C#

2. Configure Refit with Polly Resilience Pipelines

In .NET 8+, the Microsoft.Extensions.Http.Resilience package allows you to attach Polly v8 pipelines directly to Refit clients during Dependency Injection setup using AddResilienceHandler.

C#

3. Consume the Refit Client Safely with Fallback Handling

When the circuit is Open, calls to IPaymentApi throw a BrokenCircuitException. Handle this gracefully in your application logic to provide fallback behavior (e.g., queueing the order for processing later):

C#

Best Practices for Refit + Circuit Breakers

  1. Combine Retry with Circuit Breaker: Layer a Retry policy with exponential backoff and jitter inside the Circuit Breaker. Let short transient glitches self-heal via retries before blowing the breaker.
  2. Set Sensible Timeouts: Always configure HttpClient.Timeout or use Polly’s AttemptTimeout. Uncapped HTTP requests will hold connections open indefinitely, preventing the circuit breaker from evaluating metrics promptly.
  3. Log & Export Metrics: Wire up the OnOpened and OnClosed delegates to your logging framework (e.g., Serilog, OpenTelemetry) to trigger alerts when critical dependencies fail.

Recommendation for ASP.NET 10.0 Hosting

A solid base for developing online services and applications is ASP.NET. Before creating an ASP.NET web application, you must be proficient in JavaScript, HTML, CSS, and C#. There are thousands of web hosting providers offering ASP.NET hosting on the market. However, there are relatively few web hosting providers that offer top-notch ASP.NET hosting.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts

Skip to toolbar Log Out