Microservices Patterns: The Circuit Breaker Pattern

Crishantha Nanayakkara
2 min readJun 10, 2023

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.

  1. When the number of consecutive failures crosses a threshold, the circuit breaker trips and stops responding client requests.
  2. For the duration of a timeout period all attempts to invoke the remote service will fail immediately.
  3. 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.
  4. Otherwise, if there is a failure the timeout period begins again.
Figure 04 — Circuit Breaker Pattern

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).

Figure 05 — Circuit Breaker Pattern with Proxy level service orchestration

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

  1. Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018
  2. Circuit Breaker Pattern: https://microservices.io/patterns/reliability/circuit-breaker.html
  3. Spring Cloud Hystrix: https://cloud.spring.io/spring-cloud-netflix/multi/multi__circuit_breaker_hystrix_clients.html
  4. Hystrix Circuit Breaker Pattern: https://howtodoinjava.com/spring-cloud/spring-hystrix-circuit-breaker-tutorial/

--

--

Crishantha Nanayakkara

Enterprise Architect, Consultant @ FAO (UN), Former CTO, ICTA Sri Lanka