Microservices Patterns: The Circuit Breaker Pattern
Microservices Patterns Series — Part 02
The Circuit Breaker pattern is a basic pattern used especially in microservices based architectures.
The Problem
When one service synchronously invokes another, there is always a possibility that the other service is unavailable or unusable. This scenario could exhaust precious resources such on the caller side while waiting for the service to respond. This failure could cascade to other services throughout the application.
The Solution
The service client should invoke the remote service via a proxy that functions similar to to an electronic circuit breaker. It maintains a time out to minimize the client request load and handle service level errors efficiently.
- When the number of consecutive failures crosses a threshold, the circuit breaker trips and stops responding client requests.
- For the duration of a timeout period all attempts to invoke the remote service will fail immediately.
- After the timeout expires the circuit breaker allows a limited number of test requests to pass through. If those requests succeed the circuit breaker will resume its normal operation.
- Otherwise, if there is a failure the timeout period begins again.
When there is a service composition connected to the client proxy, it calls all services and aggregates their responses and sends the response to the client. The code that implement the client proxy endpoint should have a logic to handle the failure of each service it calls (Figure 05).
In a typical microservices deployment, containers are dynamic and has the tendency to go down at any given point. Therefore, this pattern implementation helps to prevent from sending unnecessary loads of requests to a failed service, by providing a delay to recover from errors.
References
- Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018
- Circuit Breaker Pattern: https://microservices.io/patterns/reliability/circuit-breaker.html
- Spring Cloud Hystrix: https://cloud.spring.io/spring-cloud-netflix/multi/multi__circuit_breaker_hystrix_clients.html
- Hystrix Circuit Breaker Pattern: https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/