MongoDB

Circuit Breaker Pattern

Understand the circuit breaker pattern and how it prevents cascading failures in distributed microservices systems.

S

srikanthtelkalapally888@gmail.com

Circuit Breaker Pattern

The circuit breaker prevents an application from repeatedly calling a failing service, allowing it time to recover.

The Problem

Order Service → Payment Service (down)
             → Retry → Retry → Timeout
             → Thread pool exhausted
             → Order Service also fails

This is a cascading failure.

Circuit Breaker States

Closed (Normal)

Requests pass through. Failures are counted.

Open (Tripped)

Requests fail immediately. No downstream calls. After timeout, move to Half-Open.

Half-Open (Testing)

Allow limited requests. If successful, close circuit. If fail, reopen.

Closed → (threshold exceeded) → Open
Open → (timeout) → Half-Open
Half-Open → (success) → Closed
Half-Open → (failure) → Open

Implementation

// Hystrix / Resilience4j style
@CircuitBreaker(name = "paymentService",
                fallbackMethod = "fallback")
public Order processPayment(Order order) {
  return paymentClient.pay(order);
}

public Order fallback(Order order, Exception e) {
  // Queue for retry or return cached response
}

Configuration

  • Failure threshold: 50% in last 20 requests → Open
  • Timeout: 30 seconds before Half-Open
  • Slow call threshold: >2s counted as failure

Tools

  • Netflix Hystrix (deprecated)
  • Resilience4j (Java)
  • Polly (.NET)
  • Istio (service mesh level)

Conclusion

Circuit breakers are essential for resilient microservices. Combine with retries, timeouts, and bulkheads for comprehensive fault tolerance.

Share this article