No history yet

Introduction to Circuit Breaker Pattern

Preventing Cascading Failures

In a distributed system, services often depend on each other. If one service fails, it can cause a chain reaction, bringing down other services that rely on it. This is called a cascading failure. The Circuit Breaker pattern is a design strategy that prevents this by isolating the failing service, giving it time to recover without overwhelming the entire system.

Think of it like an electrical circuit breaker in your home. When it detects an overload, it trips, cutting off the flow of electricity to protect the wiring and prevent a fire. The software pattern works on the same principle.

Lesson image

This pattern is a proxy for operations that might fail. The proxy monitors for failures, and if they reach a certain threshold, it 'trips' or opens the circuit. For a set amount of time, all calls to the service will fail immediately, without even trying to connect. This stops the application from repeatedly attempting an operation that's likely to fail, saving system resources.

The Circuit Breaker pattern improves the resilience and fault tolerance of microservices by detecting failures and temporarily halting access to affected services.

This is different from a simple retry mechanism. A retry strategy might keep hitting a failing service, making a bad situation worse. A timeout simply gives up on a single request after a certain duration. A circuit breaker, however, is stateful. It understands the health of the downstream service over time and makes an intelligent decision to block traffic altogether.

The Three States

The circuit breaker operates in one of three states, transitioning between them based on the success or failure of recent calls.

1. Closed

This is the default, healthy state. The circuit breaker is closed, and requests flow normally to the downstream service. The breaker maintains a count of recent failures. If a call succeeds, the count is reset. If it fails, the count increases. When the failure count exceeds a predefined threshold within a specific time period, the breaker trips and moves to the Open state.

2. Open

When the breaker is open, requests to the downstream service fail immediately without any attempt to execute them. The breaker returns an error or a fallback response directly to the caller. This protects the failing service from being hammered with requests while it's struggling, and it prevents the calling service from wasting resources waiting for a response. After a configured timeout period, the breaker transitions to the Half-Open state.

3. Half-Open

In this state, the breaker allows a limited number of trial requests to pass through to the downstream service. This is a test to see if the service has recovered.

  • If these trial requests succeed, the breaker concludes that the service is healthy again. It resets its failure count and transitions back to the Closed state.
  • If any trial request fails, the breaker assumes the service is still unavailable. It trips again and returns to the Open state, restarting the timeout period before it attempts another test.
StateRequest BehaviorTransition Trigger (to next state)
ClosedRequests pass through normally.Failure count exceeds threshold.
OpenRequests fail immediately.Timeout period expires.
Half-OpenA limited number of test requests pass through.Success moves it to Closed; failure moves it back to Open.

Now, let's test your understanding of these core concepts.

Quiz Questions 1/5

What is the primary problem the Circuit Breaker pattern is designed to solve in a distributed system?

Quiz Questions 2/5

When a circuit breaker is in the 'Closed' state, requests flow normally to the downstream service.

Understanding these states is the key to seeing how this pattern enhances system resilience and prevents small faults from turning into major outages.