Circuit breakers

A circuit breaker is a term borrowed from electronic circuits. Whenever a circuit gets in a faulty situation, the role of a circuit breaker is to cut it off from the supply to make sure it does not impact other areas. In other words, the problem is restricted to one part and cannot propagate. Using the same idea, we are talking about a technique to develop over Microservices in such a way that a problem in one Microservice should not propagate to other services, and that the impact of a failure is restricted to one area.

Let's take an example. Say we have a service that is supposed to return data about an employee's tax details. This service fetches some tax rules from another service and then works on the salary details provided. After calculating the tax details, the service returns the tax to be applied on the salary.

The following diagram highlights how tax and salary services interact:

Now, let's say the Tax Rules Service stops responding; it might be dealing with a huge load or some code error. This would mean that our Tax Calculator Service would call the Tax Rules Service but will keep on waiting until it errors out, so we can see that a faulty service is actually impacting other services.

Both the calls will timeout, resulting in overall failure, as shown here:

A circuit breaker can help in this case by creating a protective layer across the services. This will help to break the connection between the calling service and the faulty service:

A circuit breaker would help us to isolate the faulty service.

The circuit breaker can be in three states:

  • Closed: When everything is working normally. At this time, the circuit breaker is in the closed state, all the calls are being made successfully to the service, and we are getting proper responses. We can add a threshold number for errors before the circuit breaker gets into the open state.
  • Open: As the name suggests, the circuit is open or we are in an error state, so none of the calls will be made to the service being called.
  • Half-open: Once our circuit goes into the open state, the circuit breaker will keep a check on the service being called to make sure things work normally once the service is healthy again. To do so, the circuit breaker gets into the half-open state after staying for a predefined period of time in the open state. In the half-open state, the circuit breaker will try calling the end service again; if the call succeeds, the circuit will go back to the closed state; otherwise, it will go into the open state again.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.57.172