Microservice Circuit Breaker Pattern

Introduction

The article explains one of the Cross-Cutting Patterns in Microservices architecture known as Circuit Breaker Pattern. Microservice architecture is a distributed architecture that comprises some of the distributed components like Kafka as well, architecture can be polyglottic in nature. There can be any number of services in the architecture which depends on the kind of problem is being solved, it’s important that when services communicate to each other all the services must be alive, in the article we are going understand

  1. What is Circuit Breaker Pattern and its importance?
  2. States of Circuit breakers.

Circuit Breaker

A circuit breaker is a device whose purpose is to protect the electrical circuits from damage in case of short-circuits or overload, it stops the electricity flow in case of a defect to protect the appliances and prevent the risk of fire. Similarly, the Circuit Breaker design pattern stops sending the request to the service which is not working or taking too long to respond, the pattern saves the architecture from such disastrous events and helps in building fault-tolerant and resilient systems.

Circuit Breaker Pattern

The circuit breaker sits between the request and response mechanism as a proxy, when the microservice experiences failures or slowness the Circuit Breaker trips for a particular duration, in that period request to a particular service will fail immediately. Once the duration is over the Circuit Breaker will allow a few requests to the service and keep the service under observation and if the services respond successfully then Circuit Breaker resumes the flow as it was before, if there are failures from the service then Circuit Breaker remains tripped until the problem is resolved.

States

Circuit Breaker Pattern has three states

  1. CLOSED
  2. OPEN
  3. HALF-OPEN

CLOSED

The CLOSED state is the Happy Path Scenario, the flow is working normally, the Microservices are up and running, in this state the Circuit allows the requests through the service.

Circuit Breaker Pattern

OPEN

When the Circuit Breaker Timeouts reaches a configured value in calling the Microservice, meaning the Microservice is either having slowness or not working as expected the Circuit Breaker trips and goes to an OPEN State. When Circuit is in an OPEN state the incoming requests will be returned with an Error and no microservice calls are executed. This behavior reduces the load further and allows the microservice to recover from the Failure.

Circuit Breaker Pattern

HALF-OPEN

After a duration, the Circuit goes to HALF-OPEN State, In the HALF-OPEN state, Circuit makes a trial request to the Service periodically to test whether the service is recovered from the Failure or not. If the service calls still Times out, the Circuit remains in the OPEN state, if the calls are successful the Circuit switches to a CLOSED state and the traffic will go as usual.  

Summary

It’s a very important and useful pattern for Microservices which makes the microservice architecture Fault Tolerant, as we have seen this pattern handles timeouts, slowness effectively which helps in the timely recovery of microservices. There are various Circuit Breaker open-source libraries available, some of them which are widely used are, Hystrix, Resilience4j.


Similar Articles