Skip to main content
Abstract Algorithms Logo
Abstract Algorithms
โ† All Problemsยทdesign patternMedium~25 min

Design a Circuit Breaker for Microservices

Design a resilient Circuit Breaker pattern to protect distributed microservices from cascading failures when downstream dependencies degrade or fail. The breaker must support Closed, Open, and Half-Open states with automatic recovery testing.

Requirements

  • โœ“Track failure rates and slow call rates over a sliding evaluation window.
  • โœ“Transition from Closed to Open when failure threshold is exceeded, failing fast immediately without calling downstream.
  • โœ“Transition from Open to Half-Open after a configured sleep window, allowing a trial percentage of probe calls.
  • โœ“Transition from Half-Open to Closed if probes succeed, or back to Open if probes fail.
  • โœ“Provide fallback responses when circuit is Open.

Constraints & Scale

  • โ€”Minimal overhead on the critical request path (< 0.1 ms latency added).
  • โ€”Thread-safe state transitions without blocking concurrent in-flight requests.
Understand
Guided mode

What is cascading failure in microservices? Why does an upstream service calling a slow downstream service exhaust thread pools?

Context & Constraints โ€” Human Reviewed

When Service B slows down, Service A's worker threads block waiting for responses. Soon Service A runs out of threads and fails to serve its own clients, cascading the failure through the entire system. Circuit Breakers fail fast immediately, preventing thread pool exhaustion.

Trade-offs

  • โš–Fail-Fast vs Persistent Retries: Retrying a struggling service creates a retry storm that worsens the outage. Circuit breaker stops calls completely to allow downstream recovery.

Concepts used in this stage