Search code examples
microservicescircuit-breaker

Microservice Circuit Breaker and Discovery Service patterns


I'm new to microservices and have this doubt that google hasn't really helped me out. I know that a microservice has to be independent, so even if one of its counterpart goes offline, one should keep working normally.

Having that in mind, I can't really understand circuit breaker or even service discovery, like where should do I put it? Since every call I make to any microservice goes through the circuit breaker, let's say my Circuit Breaker service's server goes offline, so my whole application is doomed until I fix it. How to go around that?

Most importantly, WHERE should I put the Circuit Breaker, in a microservice as well?


Solution

  • You should use the Circuit breaker pattern whenever you have remote calls.

    If you don't use it, then in some circumstances (i.e. when some microservices are down) your system would act as it is under a self DOS attack. This situation manifests itself when you have chained synchronous calls. For example, if you have the following: A -> B -> C (A calls B which calls C). If C is not responding and A keeps calling then B could be overwhelmed with managing waiting calls from A and could not respond to legitimate calls from other services that would normally succeed.

    The most common place to use the Circuit breaker is in the API Gateway, where most of the remote calls are made (this is it's primary responsibility). You could use the pattern also in clients, to force them stop continuously and repeatedly calling a dead microservice.

    Although microservices are independent with regards to resilience (they could function even when other fail), this does not mean that they don't communicate with one another. They may communicate but in an asynchronous manner, i.e. when one microservices wants to update its own local cache with data from another microservice in a background process.