Design Patterns | Circuit Breaker Part 1
Problem Statement: While working in the Microservices world, it's quite common for a service to breakdown or unable to respond within expected time. But do you know this failure can really hamper the calling service? It can lead to all threads getting consumed and if not handled/written properly, the calling service may stop accepting more requests and eventually go down or the negative effect may even cascade to other services
What! Are you really kidding?
Microservices' world is about calling one service from another and we're saying one service can go down because of another. Then isn't it a major drawback of Microservices?
Let me explain the problem statement in more detail.
Firstly, here it's not just about the service being called to be down or not responding, it's possible that culprit is network which is either choked or slow or there are high timeouts set for the API(s) being called.
Secondly, the other service's API call made has to be synchronous. Because if it's an async call, at max, it can lead to the exhaustion of the available threads in the async pool. Therefore, at-least it won't cause the calling service to go down. But still our critical resources such as memory, database connections, threads etc. will remain blocked till that time.
Clearly, it's a problem to solve!
Hopefully below image helps you to understand the problem and solution better!
What if there is something which continuously monitors the failures, and doesn't allow the call to actually reach the other service but rather returns it immediately because it knows the call is going to fail.
That something is "Circuit Breaker" It sits as a proxy in between and prevents the scenario mentioned in the problem statement to occur!
How? Let's understand in more detail in next article which will be part 2. Hope below image helps you to understand the context and problem better!
I did actually create a video recently and posted at YouTube regarding Circuit Breaker Pattern to explain it in easy way. Do watch!
BTW, I do teach Java and related stuff online. Do get in touch with me if you're willing to become a better Software Engineer or leverage my expertise to clear your interview or grow in your career!
Till then, Happy Learning!