The Circuit Breaker pattern is one of the most popular design patterns used in Microservices architecture. In our previous article, we discussed on Benefits of Microservices Architecture which include Scalability, Fault tolerance, Resilience, etc. If we look at the flip side, microservices can make the architecture brittle as each of the user actions invokes multiple remote service calls over the network, which works perfectly if all the services are up and running.
Consider if one or more services goes down or exhibits high latency issues or timeouts, it can result in cascading failures across the entire application. Usually in this case the retrying calls can solve the issue.
But if the faults are due to unanticipated events and without knowing the significance of the issue too many continuous retries may bring down the microservice entirely and also can exhaust the network resources causing bad performance. In these circumstances it is pointless to retry the operations continously that is unlikely to succeed.
What is the purpose of the Circuit Breaker Pattern?
The purpose of Circuit Breaker pattern is different from the Retry pattern as the Retry pattern enables application to retry an operation in the expectation that it will succeed. The Circuit Breaker design pattern is the solution in case of any catastrophic cascading failure across multiple services as it prevents an application from performing an operation that is going to fail. The circuit breakers build a fault tolerance and resilient system that can survive gracefully when services are unavailable or have higher latency issues.
How the Circuit Breaker Pattern works?
The basic idea behind the circuit breaker software pattern is very straightforward. A circuit breaker acts as a proxy and monitors the number of recent failures that have occured
Using this pattern, the client will invoke a remote call to the service through a proxy, and the proxy acts as a circuit breaker. So when the number of failures crosses the threshold provided, the circuit breakers trip for a certain time period and blocks the request. After the timeout, the circuit breaker allows limited calls to the service and checks if the requests are successful, if yes it resumes backs to normal operation. Otherwise in case of failures continue then it again blocks the request for a certain time period.
Different States of Circuit Breaker
Closed State
When everything is normal the circuit breakers remained in the closed state and all the request passes through to the services as shown below. If the number of failures increases beyond the threshold the circuit breaker trips and goes into an open state.
Open State
In this state circuit breaker returns an error immediately without even invoking the services. The Circuit breakers move into the half-open state after a timeout period elapses. Usually, it will have a monitoring system where the timeout can be configured
Half Open State
In this state, the circuit breaker allows a limited number of requests from the Microservice to passthrough and invoke the operation. If the requests are successful then the circuit breaker will go to the closed state. However, if the requests continue to fail then it goes back to Open state.
Popular Circuit Breakers for Java and .NET Core
- Netflix Hystrix is a popular latency and fault tolernace library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
- Polly is a .NET resilience and transient-fault-handling library. It 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.
- is a service mesh, a configurable infrastructure layer for a Microservices application. It makes communication between service instances flexible, reliable, and fast, and provides service discovery, load balancing, encryption, authentication and authorization, support for the circuit breaker pattern, and other capabilities.
Polly and Hystrix can be primarily classified as “Fault Tolerance” tools.
Some of the features offered by Polly are:
On the other hand, Netflix Hystrix provides the following key features:
- Latency and Fault Tolerance
- Realtime Operations
- Concurrency
Originally published at https://microservicesdev.com on August 23, 2021.