Circuit Breaker Pattern - The Breaker in Distributed Calls

Circuit Breaker Pattern - The Breaker in Distributed Calls

Daily short news for you
  • For over a week now, I haven't posted anything, not because I have nothing to write about, but because I'm looking for ways to distribute more valuable content in this rapidly exploding AI era.

    As I shared earlier this year, the number of visitors to my blog is gradually declining. When I looked at the statistics, the number of users in the first six months of 2025 has dropped by 30% compared to the same period last year, and by 15% compared to the last six months of 2024. This indicates a reality that users are gradually leaving. What is the reason for this?

    I think the biggest reason is that user habits have changed. They primarily discover the blog through search engines, with Google being the largest. Almost half of the users return to the blog without going through the search step. This is a positive signal, but it's still not enough to increase the number of new users. Not to mention that now, Google has launched the AI Search Labs feature, which means AI displays summarized content when users search, further reducing the likelihood of users accessing the website. Interestingly, when Search Labs was introduced, English articles have taken over the rankings for the most accessed content.

    My articles are usually very long, sometimes reaching up to 2000 words. Writing such an article takes a lot of time. It's normal for many articles to go unread. I know and accept this because not everyone encounters the issues being discussed. For me, writing is a way to cultivate patience and thoughtfulness. Being able to help someone through my writing is a wonderful thing.

    Therefore, I am thinking of focusing on shorter and medium-length content to be able to write more. Long content will only be used when I want to write in detail or delve deeply into a particular topic. So, I am looking for ways to redesign the blog. Everyone, please stay tuned! 😄

    » Read more
  • CloudFlare has introduced the pay per crawl feature to charge for each time AI "crawls" data from your website. What does that mean 🤔?

    The purpose of SEO is to help search engines see the website. When users search for relevant content, your website appears in the search results. This is almost a win-win situation where Google helps more people discover your site, and in return, Google gets more users.

    Now, the game with AI Agents is different. AI Agents have to actively seek out information sources and conveniently "crawl" your data, then mix it up or do something with it that we can't even know. So this is almost a game that benefits only one side 🤔!?

    CloudFlare's move is to make AI Agents pay for each time they retrieve data from your website. If they don’t pay, then I won’t let them read my data. Something like that. Let’s wait a bit longer and see 🤓.

    » Read more
  • Continuing to update on the lawsuit between the Deno group and Oracle over the name JavaScript: It seems that Deno is at a disadvantage as the court has dismissed the Deno group's complaint. However, in August, they (Oracle) must be held accountable for each reason, acknowledging or denying the allegations presented by the Deno group in the lawsuit.

    JavaScript™ Trademark Update

    » 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

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, 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...