The circuit breaker pattern is a design pattern that helps to prevent cascading failure in an application. It is based on the idea of breaking the connection between two components when there is a problem to reduce the impact of the failure.
I’ll give an example of a real situation on the gateway. you can use this pattern in something like this situation whenever you want.
There are 1 million clients, some clients make requests to service A, some clients make requests to service B, some for C. All of these requests pass through the gateway.
What if some function on service C has something wrong and causes timeout in 30 seconds and we have lots of clients make requests on service C.
lets you think, the gateway holds a lot of resources for requests. when the gateway reaches the limit resource. gateway down and all of the services can’t use.
you might think, oh we have some technology like Kubernetes, we can scale-up gateway to make other services work, but it has more cost of resources.
Let’s me show you an example solution
When we get timeout response, we count timeout response if exceed threshold, during 5 mins, gateway return timeout response immediately without send any request to the service. after 5 mins, gateway give a chance to send a request to the service if timeout response again, countdown another 5 mins. if success, reset timeout count stack to zero.
Now, let’s go of the logic.
circuit breaker terms come from electrical engineering. we have to know some words.
close-circuit: wire connected so program can continue running
open-circuit: wire disconnected so program can’t continue running
Let’s take a look at the diagram
closed-circuit state: request can send to service.
opened-circuit state: request will be blocked immediately, doesn’t send to the service.
half-opened circuit state: let’s user try to send a request if successful turn to close circuit state else turn to open circuit state.
There have to set 4 values in this logic
- timeoutResThreshold (to set limit number of timeout response to send to the service)
- timeoutResCount (to count number of timeout response)
- breakTime (to stop sending the request to the service)
- isBreakTimeExpired (to check break time status)
Here is full logic for circuit breaker pattern.
Thank you for reading & enjoy your coding 🙂