Circuit Breaker Pattern - The Breaker in Distributed Calls

Circuit Breaker Pattern - The Breaker in Distributed Calls

Daily short news for you
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more
  • I just found a quite interesting website talking about the memorable milestones in the history of the global Internet: Internet Artifacts

    Just from 1977 - when the Internet was still in the lab - look how much the Internet has developed now 🫣

    » Read more
  • Just thinking that a server "hiding" behind Cloudflare is safe, but that’s not necessarily true; nothing is absolutely safe in this Internet world. I invite you to read the article CloudFlair: Bypassing Cloudflare using Internet-wide scan data to see how the author discovered the IP address of the server that used Cloudflare.

    It's quite impressive, really; no matter what, there will always be those who strive for security and, conversely, those who specialize in exploiting vulnerabilities and... blogging 🤓

    » 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

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click 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...