Circuit Breaker Pattern in Microservices
The circuit breaker pattern is a design pattern that falls under the sustainable design patterns category. It allows developers to prevent cascading failures in microservices architecture by invoking remote services through a proxy.
Microservices architecture has become the new norm for large-scale applications. Because it has more advantages compared to traditional monolithic architecture. However, microservices also come with several challenges. One such challenge is preventing cascading failures. For example, network or service failure in one microservice can quickly cascade into other services and cause a system-wide failure.
Why We Need Circuit Breaker Pattern?
In a microservices architecture, services have to communicate with each other. Sometimes, there can be service failures, or it takes significant time to respond to requests due to network connectivity issues
For example, assume that there are 2 microservices named catalog and orders. When the catalog service needs to communicate with the orders service, it creates a new thread and sends a request to the orders service. However, if there are any network issues or timeout failures from the orders service, the catalog service will not get an instant response. Also, there is no way to inform the catalog service about the failure so it will wait for a response. As a result, the catalog service will send continuous requests to the orders service until its resources are exhausted, resulting in catalog service failure.
Understanding and implementing the circuit breaker pattern is pretty easy. It has three states: Closed, Open, and Half Open
- Open
- Half-Open
- Helps to prevent cascading failures.
- Handles errors gracefully and provides better under experience.
- Reduces the application downtimes.
- Suitable for handling asynchronous communications.
- State changes of the circuit breaker can be used for error monitoring
Comments
Post a Comment