- Architectural Design Patterns 1 – Layered (or Tiered) Pattern
- Architectural Design Patterns 2 – Model-View-Controller (MVC)
- Architectural Design Patterns 3 – Microservices
- Architectural Design Patterns 4 – Event-Driven Architecture
- Architectural Design Patterns 5 – Monolithic
- Architectural Design Patterns 6 – Service-Oriented Architecture
- Architectural Design Patterns 7 – Domain-Driven Design
- Architectural Design Patterns 8 – Serverless (Function as a Service – FaaS)
- Architectural Design Patterns 9 – Circuit Breaker
When designing systems, especially distributed systems, the flow of data and service requests must remain continuous. This brings us to an architectural design pattern called the Circuit Breaker. It comes from electronics. Simply, it is designed to stop the flow when something goes wrong. This prevents potential cascading failures in a system.
Understanding the Circuit Breaker Pattern
Imagine a scenario where a service relies heavily on another third-party service. However, that third-party service becomes slow or starts to fail at some point. Without any preventive measures, our service will keep making requests, waiting for timeouts, even getting stuck. This is where the Circuit Breaker provides an advantage. It monitors the number of failed requests and once the threshold is reached, for a certain period no further requests are made to the failing service. During this state, the third-party service might have the time and resources to recover.
A real-world scenario can be online booking systems. If a payment gateway is experiencing delays, the booking service can use the circuit breaker pattern to stop requests temporarily. Then it can provide a better response to users rather than crashing or delaying responses.
Implementation Insights
A typical circuit breaker keeps track of requests and their success or failure rates. Depending on the rate of failure, it moves between three states: Closed, Open, and Half-Open. In the closed state, requests flow through normally. However, if failures cross a threshold, the breaker moves to an open state and blocks all the requests. After a certain time, it moves to a half-open state, allowing a limited number of requests to pass. If those requests succeed then the breaker closes, else it remains open.
Pros and Cons
A primary advantage of the circuit breaker pattern is its ability to prevent system failures from wide-spreading. Instead of continuously sending requests to a failing service, it gives it a room for recover. Additionally, by using this pattern, systems can fail properly and gives meaningful feedback to the users or calling services.
However, a misconfigured circuit breaker can harm the system. If it’s too sensitive, it’s state might change too often even for minor issues. This can cause services being unavailable when they don’t need to be. On the contrary, if it’s not sensitive enough, it might not change the state even though it is required.
Circuit breaker is great for handling faults but it doesn’t address the root causes. It’s a preventive mechanism, a beta blocker. It is not a solution to the underlying problem. So, while they bring resilience, they shouldn’t be seen as a replacement to a proper error handling mechanism.
Suleyman Cabir Ataman, PhD