Microservice resilience - Circuit Breaker using polly in .Net Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to dotnet core central the channel where I discuss different topics of dotnet core and today my focus is going to be discussing circuit breaker with poly for asp.net core micro-services the things that I'm going to cover today are number one what is the circuit breaker next I'm going to cover why do we need it for creating resilient micro-services and third is how do you implement it using poly so let's start one by one it's fast what is circuit breaker circuit breaker is a concept for creating resilient micro-service in a micro-services environment we have multiple services talking to each other in some cases they are talking in reactive fashion where they're using some sort of cue or even worse for communicating with each other which is extremely decoupled and that's easier to manage in a sense that if the event is not received or you didn't get a message from a cue you are just not reacting to it whereas when you are talking to a service through an HTTP there is always a possibility of failure I mean even queues and even bus can have connectivity issues and I guess you can implement circuit breaker there as well but for coupled call which is like HTTP call it's way more relevant where you know HTTP calls might fail the services might get a 500 error and it's important to implement resiliency there what is circuit breaker help circuit breaker essentially is a concept where you try a service for a predetermined number of time and if it fails that many number of time you just open the circuit which means the consecutive calls do not even go to the service it immediately returns until the wait period that is configured now the circuit breaker can be implemented on your own you can have try/catch block and keep the configured number and then you have to obviously keep it as a static or some sort of in-memory cache so that you know for a particular service how many times you are calling and beyond that what you have to do so this entire framework you have to build on your own or you can use something like poly which provides this entire infrastructure out of box which is really cool so to do that what I'm going to do is I'm going to open up a solution which I created we just throws an exception and by the way I have covered retries with poly in my previous video you can see the link up where I have a link to my previous video where I have covered the retries with poly retry is essentially just retrying a service for a particular number of time if it fails and then giving up it retry the difference between retry and circuit breaker is retry will happen for the single call so when your service or for a single transaction so when your service is walking through a workflow and at some point in time it's making an external call to try we'll try this external call during that session for the configure number of time where the circuit breaker is across multiple call so first time your service gets a call you are calling out to another service B it fails the circuit breaker will come back as the circuit breaker will not be doing anything here if the configured number of tries is 3 it will just fail and your service will come back and it will didn't matter your service get called second time it will work it will go the other service service B will throw an error it will come back and the third time what will happen is that's when circuit breaker will kick in it'll say ok the last two try was a failure and I have been configured to try two times so now I am not even going to make the service call because the Excel service is dead so there is no point in making a call so it will just throw a circuit breaker error saying you know the circuit is open right now so you cannot even go there that's how a circuit breaker works and that's the difference between circuit breaker and retries I want to make it very clear that retry is for the same session multiple tries where a circuit breaker is across the session figuring out how many times is failed and then opening the circuit so that the call doesn't even go outside okay having said that now I'm going to open visual studio and show you the solution which I created in my last video which just throws an exception so this is considered this an external service error API all it does is it throws an error right so I'm going to just start it okay so this service is started now what I'm going to do is I'll just close this solution this is not important because it's not doing anything I'm going to create a new project and we'll do create a s periodic or wave application and I'm going to name it a circuit breaker tour demo I'm going to select API and here I'm going to select dotnet code to wrote one because Paulie currently works well with dotnet code 2.1 so i'm just going to go with dotnet code 2.1 okay so once the project is created what I'm going to do is I'm going to go here and the idea is in this function call or in this Web API method all I'm going to do is called the other service which is running in Port 5,000 with the error API and call the cat method on the error API which will return error so to do that what I'm going to do is I'm going to go and start up and then I'm going to configure the HTTP client okay so we have the HTTP client configure so now I can go here in the controller and inject it in the constructor the client factory and create a client out of it and I'll provide the same name that I have given during configuration and just at the Newton soft okay so right now the code is ready so what's going to happen is we'll try to access will create their APA klein try to call values we should throw a 500 error and doesn't matter how many times recall we should be getting the same error and you know continuously fail and will continuously make call out to the other application so as you can see it's feeling desolation it's just because the the other service is returning 500 right and we can put a breakpoint here to show you and I can come back and do a refresh again and we can see here the response is 500 right and and doesn't matter how many times i refresh i am going to get the same error rate just that it's calling the Excel service and we are getting 500 okay now I'm going to add Polly to the implementation and as I mentioned if you want to see my previous video where I started with Polly and explained about Polly you can click the link below okay so next thing what I'm going to do is I'm going to add the new get package for Polly but I'm going to add the Microsoft extension for that so I'm going to add the Microsoft or extension start HTTP tour Polly and I'm going to add the two dot one dot one or zero one of these for the Theodore doesn't work because it relies on three dot oh I think this one should work okay now that Polly is added what I'm going to do is I'm going to go into startup and Here I am going to add the circuit breaker for the HTTP client so after I added the namespace poly I can see the extension method circuit breaker and in the circuit breaker I'm going to say how many times it should try to connect to external service during failure I'm going to say two and then after that I have to specify how many minutes it should wait and not even go out and make an external call and keep the circuit open so for that I'm going to say time span dot add from minutes and I'm going to say you know wait for two minutes because if s external service is failing there's no point in making call for two minutes so that's how I have configured it so now as I explained in my previous video also the beauty is all the configuration for circuit breaker is right here in your dependency injection configuration section it does not even impact your implementation of the actual code so you can see here absolutely nothing I have not done anything everything happened here so it is configured now now next thing what I'm going to do is I'm going to again run my application and we should see that after couple of free tries from the third retry we should start getting circuit open message and in the code we should be able to handle that application specifically and manage it in our code maybe to you know add cache data or something again even if the circuit breaker we can we have couple of actions as re functions I have a problem I have to use the circuit caressing okay so this should work yeah as I was telling is in case of a circuit breaker error you know depending on our application need we can do a lot of thing we can catch the last valid data and keep surfacing that or you know some fallback so you can see first time it's getting a 500 error so we're getting similar error second time also it'll get it but third time onward see it is saying the circuit is now opened and not allowing cause so circuit breaker has kicked in and for next two minutes it's going to do the same thing and then after two minutes we can see it will again call out to see if the circuit is if the Excel call is happening so here in circuit breaker we can have a couple of actions which are basically callback from the circuit breaker on break and on reset and obviously we can use this method to do some plumbing in terms of how do you want to react what are the message if you want to log something or handle the request somehow so that's that's energy store circuit breaker is if you want me to cover more about circuit breaker and advance circuit breaker please leave me a comment and I will pick it up in my next video but for the stator circuit breaker that's pretty much the implementation is and I can sure it's the circuit is still open because the service is failing and it'll continue to happen for a couple of minutes and as you can see after 2 minute I refreshed and it is giving me the old 500 error I'll do it again and the circuit is open once again because it after two minutes it tried the service is still broken so it will again fall back saying okay your service is not up so I'm not going to open it again so this is how we implement circuit breaker with poly for making our micro services more resilient thank you so much for watching this video if you have not subscribed to my channel please subscribe and if you liked the video please give me a thumbs up
Info
Channel: DotNet Core Central
Views: 4,004
Rating: undefined out of 5
Keywords: .net core, polly, circuit breaker, asp.net core, microservice, .net core 2.0, c#, micro-service, microservices, micro-services, circuit breaker pattern, circuit breaker design pattern, micro-service resiliency, microservice resiliency
Id: ScpydLKqQuQ
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Thu Oct 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.