Circuit Breaker

Stop calling a dependency that keeps failing, fail fast with a fallback, and probe now and then to see if it has recovered.

Description

A circuit breaker sits between your service and a remote dependency, such as a payment provider or another team's API. It counts failures. While calls succeed it is closed and lets everything through.

When failures cross a threshold it opens: calls fail immediately, without waiting on timeouts, and your code falls back to a cached answer, a queue or a friendly error. After a cool-down it goes half-open and lets a few trial calls through. If they succeed it closes again; if not, it stays open.

The point is to protect both sides. Your threads and connection pools aren't tied up waiting on a dead service, and the struggling dependency gets breathing room to recover instead of a retry storm.

System design

Order serviceplace order1charge()2call3timeouts, 5xx4open: fail fast5pending6Checkout appOrder handlerCircuit breakerclosed · open · half-openPay-later queuefallbackPayment provider

Circuit Breaker system design diagram

  1. Checkout app (client).
  2. Order handler (service).
  3. Circuit breaker (gateway): closed · open · half-open.
  4. Pay-later queue (queue): fallback.
  5. Payment provider (external service).
  6. Step 1: Checkout app → Order handler: place order.
  7. Step 2: Order handler → Circuit breaker: charge().
  8. Step 3: Circuit breaker → Payment provider: call.
  9. Step 4: Payment provider → Circuit breaker: timeouts, 5xx (failure path).
  10. Step 5: Circuit breaker → Pay-later queue: open: fail fast.
  11. Step 6: Order handler → Checkout app: pending.
  1. The checkout app sends an order to the order service.
  2. The order handler charges the customer through the circuit breaker, never directly.
  3. While the circuit is closed, the breaker passes the call on to the payment provider.
  4. Timeouts and 5xx errors are counted. When the failure rate crosses the threshold, the circuit opens.
  5. While open, calls fail immediately and the order goes to a pay-later queue. After a cool-down, a few trial calls test whether the provider is back.
  6. The customer gets a fast answer ("payment pending") instead of a spinner and a timeout.
AWS
App Mesh or Envoy outlier detection; in code, libraries such as opossum (Node) or resilience4j.
Azure
Azure API Management circuit breaker policy; Polly in .NET.
Google Cloud
Cloud Service Mesh (Istio) outlier detection and connection-pool limits.

When to use it

Use it when

  • You call a remote service that can be slow or unavailable, and waiting on it would exhaust your threads, connections or users' patience.
  • A failing dependency should degrade one feature, not take down your whole service.
  • You have a sensible fallback: cached data, a queue for later, a default, or a clear message.
  • Retries alone are making outages worse by piling load onto a struggling service.

Avoid it when

  • The call is local and in-process, such as reading from memory. There is nothing to protect.
  • The operation must succeed or fail as a unit with no fallback. Fail clearly instead of pretending.
  • Traffic is so low that a few failures would open the circuit by chance. Use timeouts and alerting instead, or a time-based failure rate.

Pitfalls

  • Thresholds tuned by guesswork: too sensitive and it opens on noise, too lax and it never helps. Base them on error rate over a window, not a raw count.
  • Counting the wrong failures. A 404 or a validation error is not the dependency being down; only timeouts, connection errors and 5xx should trip it.
  • One breaker for many endpoints, so one bad endpoint blocks healthy ones. Scope a breaker per dependency, and per endpoint when needed.
  • No visibility. Emit state changes as metrics and alerts; an open circuit is an incident someone should know about.
  • Combining it badly with retries: retry inside the breaker, a few times with backoff, not around it.

Alternatives

Retry with backoff

Choose it when failures are brief and transient. Often used together: retry a few times, and let the breaker stop retries when the outage is real.

Bulkhead

Choose it when you want to cap how much of your capacity one dependency can use, even while it's still answering slowly.

Queue-Based Load Leveling

Choose it when the work can happen later. Put it on a queue and let a worker call the dependency at its own pace.

Timeouts only

Choose it when the dependency rarely fails and the cost of waiting is low. Always set timeouts, even with a breaker.