# Circuit Breaker Pattern

The Circuit Breaker pattern is a design pattern used in distributed systems to detect failures and encapsulate the logic of preventing a failure from constantly recurring during maintenance, temporary external system failure, or unexpected system difficulties.

## 1. The Problem: Cascading Failures

In a microservices architecture, if Service A calls Service B, and Service B is down or slow:
1.  Service A waits for a timeout (e.g., 30 seconds).
2.  Service A retries the request.
3.  Service A's resources (threads, database connections) get tied up waiting for Service B.
4.  If traffic is high, Service A runs out of resources and crashes.
5.  **Result:** A single failing service brings down the entire system (Cascading Failure).

## 2. The Solution

Wrap the dangerous function call (e.g., HTTP request) in a **Circuit Breaker** object. This object monitors for failures. Once the failures reach a certain threshold, the circuit breaker "trips", and all further calls return an error immediately without making the network call.

## 3. The Three States

### Closed (Normal Operation)
The circuit is closed. Requests are allowed to pass through to the supplier.
*   If the call succeeds, everything is fine.
*   If the call fails, a failure counter is incremented.
*   If the failure counter exceeds a threshold (e.g., 5 failures in 10 seconds), the circuit trips to **Open**.

### Open (Failing)
The circuit is open. Requests are **blocked** immediately.
*   The circuit breaker returns an error or a fallback response instantly (Fail Fast).
*   This prevents resource exhaustion and gives the failing service time to recover.
*   After a timeout period (e.g., 30 seconds), the circuit switches to **Half-Open**.

### Half-Open (Testing)
The circuit allows a limited number of requests (e.g., 1) to pass through to test if the underlying problem is fixed.
*   If this request succeeds, the circuit switches back to **Closed** (Reset).
*   If this request fails, the circuit switches back to **Open** and restarts the timeout timer.

## 4. Benefits

*   **Fail Fast:** Prevents the application from waiting for TCP timeouts.
*   **Resource Protection:** Prevents exhaustion of network connections or thread pools.
*   **Recovery:** Reduces load on the downstream service, allowing it to recover faster.
*   **User Experience:** Allows returning a cached fallback or a friendly error message instantly instead of a spinner that eventually times out.

## 5. Implementation

Libraries like **Hystrix** (Java), **Resilience4j** (Java), or **Polly** (.NET) are commonly used. In modern cloud-native stacks, this logic is often offloaded to a **Service Mesh** (Istio/Linkerd).

```javascript
// Conceptual Usage
circuitBreaker.fire(request)
  .then(response => console.log(response))
  .catch(err => console.log("Service unavailable, showing cached data"));
```

[[programming/microservices-architecture]]
[[programming/service-mesh]]
[[programming/system-design]]