Circuit Breaker Pattern - The Breaker in Distributed Calls

Circuit Breaker Pattern - The Breaker in Distributed Calls

Daily short news for you
  • Rust is the time of its coming... It's "redefining" a whole bunch of things. Even the coreutils package containing basic Linux commands has been rewritten by it, and it runs on multiple platforms too 🫣

    uutils.github.io

    » Read more
  • Attention everyone 🔥, Gemini 2.0 is the latest and most powerful model from Google, currently available for free trial, and the API is also free (But it seems they are imposing more restrictions as there are many users).

    » Read more
  • I just discovered a very simple way to public localhost on the Internet using VSCode. First, open the Panel using the shortcut Command + J (or Ctrl + J), click on the PORTS tab, click on the "Forward a Port" button, then enter the port you want to public and hit Enter, and that's it. VSCode will create a URL mapped to the port on your local machine 🤓

    » 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.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)

Leave a comment...