If we are using microservices Architecture then we are working with lots of APIs these APIs are communicating with each other in sync or async way. If any one of the APIs is not responding this will lead to cascading failure which will bring down the entire system.
One such challenge is preventing cascading failures.
To overcome this issue we are going to use Circuit Breaker Pattern.
What is a Circuit Breaker?
Circuit Breaker Pattern works similarly to an electrical circuit breaker. when some electric fluctuation happens it trips and there is no further connection established to prevent damage.
Similarly when some service is down Circuit Breaker will not call any further service it will return an error immediately, as the timeout period is reached the Circuit Breaker switches to half-open State and tries to connect with the service that was down if it is up, and responding then state of Circuit Breaker will turn to Closed State. If the service is down is still not responding then it will return to Open State.
There are 3 states
- Closed
- Open
- HalfOpen
Closed
When the sender sends the request and gets a response the circuit breaker remains in the closed state.
Open
The circuit breaker returns an error for the sent request without executing the further service.
Half-Open
We configure the timeout period as it is reached then the Circuit breaker switches to a half-open state in this state, it will again try to connect to the down service if it is able to connect and get a successful response then it will switch to Closed state if it gets an Error then it will again switch to Open State.
Entire Circuit breaker Flow
Why Circuit Breaker is Needed?
In Microservices architecture where each API communicates with Other APIs in this process sometimes we get errors due to the service being down or the service being unable to respond.
Example:-
Let’s assume we have 3 services that communicate with each other.
Report Service sends a request to Upload Document Service and Upload Document Service sends a Request to Notification Service.
In this process, we got the request to generate the report and it has generated the report successfully but now it is trying to call Upload Document Service (Uses S3 Storage) which is using third-party API is not responding. in this part, it will not respond to the Report service will not get any response.
In this process Reporting service will keep calling the Document Service this both services (Reporting and Document) will get down. Meanwhile, both services will also affect the Notification service because it will no longer get any notification requests.
The failure of single microservices is able to bring down 2 other services. But in the real world where we have 100 of APIS if any 2 services are down then what will be the effect of it on the entire system?
