Posted on

Polly with NET 6, Part 1 Dependency Injection of a Policy into a Controller

There are a number of other features that exist in Polly to help build resilient microservices, and we have only touched on a few common ones. For both Circuit Breakers, when the circuit breaks, all requests to the resource are rejected immediately and a BrokenCircuitException is thrown. After the defined period, the Circuit Breaker will allow one request through. If the test request succeeds, the circuit returns to normal and all requests are allowed. But if the test request fails, the circuit remains open for another defined period before again transitioning to the test state. RetryForever does not actually retry forever; it will retry up to int.MaxValue times.

This is the first post in a series where we will discuss various policies that Polly provides and how they can be used to make your system even more resilient. Once again, thank you for taking your time to read and stay safe if you’re reading this during the 2020 Covid-19 pandemic. The FallbackPolicy object uses the delegate to execute the required HTTP call to customer service in the Execute() delegate. If the HTTP call throws an exception that is being handled by the fallback policy then provide a substitute value in the event of failure.

It also allows you to specify how many concurrent requests can execute and how many requests can be queued for execution so that once concurrent & queue slots are full then new requests are failed immediately. In this article, I will not cover how to build a microservice in ASP.NET Core as I have already covered that in detail in my other article on Microservices with ASP.NET Core. Here we will see how to implement fault tolerance in Microservices using Polly in ASP.NET Core. The purpose of the circuit breaker is different from the retry pattern.

polly net

The circuit breaker policy object uses the delegate to execute the required HTTP call to customer service in the Execute() delegate. If the HTTP call throws an exception that is being handled by the catch block to provide an alternate value for the customer name. The above code example will create a fallback policy that will substitute the response value as ‘Customer Name Not Available – Please retry later’ if an HTTP service call fails with an exception handled by the Policy. A fallback policy with datatype string is being used as customer service action return a string as a response. The TimeoutPolicy object uses the delegate to execute the required HTTP call to customer service in the Execute() delegate. This is for demo purposes only in practice you will return an error to the user and at the same time inform the admin about the error so that this can be fixed.

The Timeout policy lets you decide how long any request should take. If the request takes longer than specified, the policy will terminate the request and cleanup resources via the usage of a cancellation token. In this example, the fallbackPolicy wraps the retryPolicy which wraps the timeoutPolicy. You might be wondering how this will help you if the error you get back is an authorization error. That’s where onRetry delegate really comes in handy, by letting you execute any code prior to performing the retry. Accompanying the project is a .NET Microservices Architecture ebook with an extensive section on using Polly for resilience, to which Dylan Reisenberger contributed.

Before we start adding resilient microservices, it’s worth spending a moment understanding what resiliency is. The Bulkhead Isolation policy limits the amount of resources any part of your application can consume. Let’s say you have a web service that consumes multiple other web services. If one of those upstream services is unavailable, requests will start to back up on your service.

Post navigation

The Polly policies can be combined in any order using a PolicyWrap. However, we should consider the ordering points that are described in the Polly documentation. In the following example, we combined all the studied policy strategies based on the typical policy ordering. We have registered the IProvider2Integration with Transient lifetime in the Dependency Injection . You can decide how to register your services depending on your project requirements. For a better understanding of Dependency Injection and Lifetime, read the .NET Nakama article.

polly net

For using Polly with HttpClient factory from ASP.NET Core 2.1, see our detailed wiki page, then come back here or explore the wiki to learn more about the operation of each policy.

Polly helps us to implement this retry policy with a limit on the maximum number of retries from order service to product service. Now in this case, if the order service receives a failure response from the product service then retrying Onion Architecture the request might fetch the results from the product service. However, to ‘simulate’ a network & dependency failure, we can simply “stop” the authors and books microservices, which will result in the API Gateway returning errors.

Microservices Framework and DevOps Development: Get the Best of Both Technologies

As per the definition of name this policy suggests that you need to terminate the request in case of no response from another service within the set time limit. Along with Order details, this Order service also returns the customer name. To get this customer name order service makes a call to customer service get method.

polly net

Next, we will add a new action method in order service which will make use of the Circuit Breaker Policy object to make an HTTP request to the action method of customer service which is returning an error. Circuit Breaker policy is being used to handle failures from customer service by not making any calls to the customer service for 1 minute after it has failed for 2 consecutive times. Next, we will add a new action method in order service which will make use of the FallbackPolicy object to make an HTTP request to the new action method of customer service which is returning an error. The fallback policy is being used to handle failures from customer service by providing fallback or substitute value for response in case of failure. Next, we will add a new action method in order service which will make use of the RetryPolicy object to make an HTTP request to the new action method of customer service which is returning an error on a random basis.

@reisenberger – Enable extensibility by custom policies hosted external to Polly. @awarrenlove – Add ability to calculate cache Ttl based on item to cache. @jiimaho and @Extremo75 – Provide public factory methods for PolicyResult, to support testing. @reisenberger – Fix CircuitBreaker HalfOpen state and cases when breakDuration is shorter than typical call timeout. Thanks to @vgouw and @kharos for the reports and insightful thinking. @SteveCote – Added overloads to WaitAndRetry and WaitAndRetryAsync methods that accept an onRetry delegate which includes the attempt count.

Circuit-breaker

But now since few consecutive calls to customer service has failed so for some time you don’t want to waste time in calling customer service instead assume that it will return an error and use the alternate response for processing the request to order service. To simulate random failures from customer service add the below action method to customer service. To handle temporary failures you want to add the logic to retry the request to customer service a minimum of 2 more times to ensure that temporary failures from customer service are handled using retries.

Sometimes a request is going to fail no matter how many times you retry. The Fallback policy lets you return some default or perform an action like paging an admin, scaling a system or restarting a service. It’s time to apply the policy in the code that communicates with the third-party provider. We used the circuit breaker policy in the following examples to execute the ThirdPartyProviderCommunication() function.

This object contains the information that is needed from the policies. In this step, we define the policies with their thresholds and how we will combine them. The following code samples show how we can define policies based on the policyBuilder of Step 1. However, there are cases, such as the TimeoutPolicy, in which we should use the Polly static methods. To learn more about the different contractors of each policy, see the Polly documentation.

  • The timeout policy is being used to handle delays from customer service.
  • If you do not design and implement techniques to ensure fault tolerance, even partial failures can be amplified.
  • Specify a substitute value or func, calling an action if the fallback is invoked.
  • When order service calls product service continuously with success then we say that circuit is closed .
  • As per the definition of name this policy suggests that you need to retry the request in case of failure of the request during 1st attempt.
  • Create another .Net 6 WebAPI called CircuitBreaker.Demo and install Polly and Newtonsoft.json Nuget packages.

On above screen select action /api/Order/GetOrderByCustomerWithCircuitBreaker/ it should expand and then click on Try it out button. After that, you should see the below screen where you need Command Line Commands CLI Tutorial to enter a value for customer code and click on execute button. On above screen select action /api/Order/GetOrderByCustomerWithFallback/ it should expand and then click on Try it out button.

The Tutorial Project

It could be difficult to reproduce a network failure, so to simulate this scenario, let’s modify our Authors service to fail on the first request, then succeed on the second. No matter what happens to our downstream microservices, we can now gracefully handle the error and show something more readable to our consumers. The first and most simple way to handle failures with Polly is to capture any Exception, and handle them accordingly.

We are reusing the source code of HttpClientFactory’s typed client implementation. In this article, we have shown how Polly can help prepare us for these inevitable problems, and therefore increase our resiliency, which in turn increases uptime, saves resources, and improves customer satisfaction. To download the source code for this article, you can visit our Resilient Microservices in .NET with Polly repository. You specify how many concurrent requests of a given type can execute and how many can be queued for execution. If the execution and queue slots are in use, no more requests of that type can be processed. @reisenberger – Allowed policies to handle returned results; added strongly-typed policies Policy;.

Bryan Hogan of the NoDogmaBlog has authored a PluralSight course on Polly. The course takes you through all the major features of Polly, with an additional module added in the fall of 2018 on Http Client Factory. The course examples are based around using Polly for fault tolerance when calling remote web services, but the principles How to Run a Successful 1-on-1 Meeting with a Developer and techniques are applicable to any context in which Polly may be used. PolicyResult.FaultType – was the final fault handled an exception or a result handled by the policy? (quickstart;deep)When a process faults, multiple failing calls can stack up and can easily swamp resource (threads/ CPU/ memory) in a host.

Please be sure to branch from the head of the default branch when developing contributions. Polly-Samples contains practical examples for using various implementations of Polly. Please feel free to contribute to the Polly-Samples repository in order to assist others who are either learning Polly for the first time, or are seeking advanced examples and novel approaches provided by our generous community. @simluk – Fix continueOnCaptureContext not being honored in async retry implementation (bug in v7.1.0 only).