Circuit Breaker Pattern

When your application calls external services, a lot can go wrong. Services can be slow, unresponsive, or completely down. Without proper handling, these failures can cascade through your system, making everything slow or unusable. The Circuit Breaker pattern prevents this by detecting failures and avoiding unnecessary calls to failing services.

Let’s say you’re calling an external service that's experiencing issues. Each call might take 30 seconds to timeout, and your application makes hundreds of such calls. Your users would face long delays, and your system's resources would be tied up waiting for responses that will never come.

The Circuit Breaker works like an electrical circuit breaker. It monitors for failures. When failures reach a threshold, the circuit "trips" and prevents further calls to the failing service. Instead, it fails fast by returning an error immediately. After a set time, it allows a few test calls to check if the service has recovered.

The Circuit Breaker has three states:

  1. Closed: Normal operation, calls pass through

  2. Open: Calls fail fast without attempting to execute

  3. Half-Open: Allows a limited number of test calls to check recovery

Implementation is straightforward. You track the number of recent failures. When failures exceed a threshold (say 5 failures in 10 seconds), you open the circuit. After a timeout period (maybe 1 minute), you switch to half-open and allow test calls. If these succeed, close the circuit; if they fail, reopen it.

Here's how you might use a Circuit Breaker:

// 5 failures, 1 minute timeout
CircuitBreaker breaker = new CircuitBreaker(5, 60000); 

try {
    breaker.execute(() -> externalService.call());
} catch (CircuitBreakerOpenException e) {
    // Handle failure fast scenario
}

The Circuit Breaker pattern pairs well with fallbacks. When the circuit is open, you can return cached data, default values, or use alternative services instead of failing completely.

© 2024 DrawSystem Design. All rights reserved.