In this tutorial, I let's build a resilient Web API application using Polly as its Circuit-Breakers. This approach is a method of minimizing exponential failures in a distributed system or in a microservices architecture.
Introduction
In a distributed system where application sub-systems are hosted in multiple host server with its own autonomy, it is essential to implement resiliency in your transport layer by creating a policy to retry certain http request to its dependencies or implement a circuit breaker to re-target http request to an alternative endpoint.
You can implement try catch handler but if your application has a number of dependency, your application will tend to be messy and difficult to maintain.
Here I want to introduce Polly, What is Polly Anyway, According to its site, Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
With the example posted in github, I created has two demo web api application, First is the FailingAPI application, where the default Get endpoint is designed to inconsistently return an exception. Second is the ConsumerAPI application that implements Polly and has dependency to the FailingAPI application.
Prerequisite
- Microsoft.Extensions.Http.Polly
- Newtonsoft.Json