Circuit Breaker Pattern - The Breaker in Distributed Calls

Circuit Breaker Pattern - The Breaker in Distributed Calls

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

Issue

A Design Pattern, also known as a Pattern, is a general solution or design to solve a specific problem. It is synthesized and used by many people to solve similar problems. When solving a problem, Patterns are often considered or recommended. Therefore, the more design patterns you know, the more ideas you have to solve the problem in the most appropriate way.

The Circuit Breaker Pattern is a design pattern inspired by a "circuit breaker". When a short circuit or overload occurs due to too many electrical devices consuming power, the circuit breaker will automatically cut off to ensure there is no risk of explosion. Similarly, in software systems, it acts as a "breaker" when there is a mass error in the processing flow.

In today's article, let's find out what Circuit Breaker is, when it is commonly used, and how to implement it in Node.js.

What is Circuit Breaker Pattern?

The Circuit Breaker Pattern is a software design pattern used to handle errors related to calls to services from different components in a distributed system. When a service is unavailable or faulty, the Circuit Breaker Pattern helps to increase the stability and reliability of the system by disconnecting from that service and using an alternative recovery mechanism.

Circuit Breaker is often used in cases where the logic is related to API calls or something similar to external services (distributed systems). Due to the nature of distributed systems, the risk of errors between services can occur at any time, so we always need a plan to handle errors or redirect to backup services. Circuit Breaker helps increase system stability by disconnecting from the faulty service and using a recovery mechanism such as switching to another service or quickly returning an error without waiting for a response. It also reduces the load on services when it detects that a service is overloaded. In summary, Circuit Breaker provides a mechanism to handle errors that occur between services in a distributed system.

The operation principle of Circuit Breaker is very simple. It is based on three states of a "circuit breaker": Closed, Open, and Half-Open.

  • In the normal state, the circuit is closed, meaning the circuit is in the Closed state, and requests are being processed freely and everything is functioning as expected. The Closed state will transition to Open if the number of requests being processed suddenly increases or an error occurs above the threshold set by the circuit.

  • In that case, the circuit will automatically open, and it will be in the Open state. Subsequent requests will not be able to continue making calls to the distributed service. Instead, the circuit will immediately return an error to notify us without waiting for the processing.

  • After a certain period of time, the circuit will attempt to partially open for a few requests to check the availability of the services. This state is called Half-Open. If everything returns to normal, the circuit will switch back to the Closed state, and our system will return to its original state. If not, the circuit will continue to be Open until the next check.

There are some issues to consider when implementing Circuit Breaker, such as determining the threshold for circuit cutting and the retry threshold for circuit reopening. Depending on the design of each system, we need to determine these thresholds appropriately and optimally.

After understanding the principles, we can implement our own circuit breaker.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...