What is the circuit breaker pattern?

The circuit breaker pattern wraps calls to a remote service and stops making them once failures cross a threshold. It fails fast or returns a fallback for a while, then tests whether the service has recovered.

Also called circuit breaker design patternUpdated

When a service calls a dependency that is failing or very slow, each call waits for a timeout. Threads and connections pile up, the caller slows down, and then its own callers start timing out. One bad dependency becomes an outage across several services. A circuit breaker prevents this by failing fast: after enough failures it stops calling the dependency for a while and returns an error or a fallback straight away.

Michael Nygard popularised the pattern in his book Release It!, and Martin Fowler's write-up is the one most developers learn from. The name comes from the electrical switch that trips to protect a circuit.

The three states

  • Closed. Calls go through. The breaker records successes, failures and slow calls over a sliding window.
  • Open. The failure rate has crossed the threshold. Calls are rejected immediately without touching the dependency. After a wait period, the breaker moves to half-open.
  • Half-open. A small number of trial calls go through. If they succeed, the breaker closes. If they fail, it opens again.

Example: a payment status check in Spring Boot

A checkout service calls a payment gateway's status API, protected with Resilience4j:

resilience4j.circuitbreaker:
  instances:
    paymentGateway:
      slidingWindowType: COUNT_BASED
      slidingWindowSize: 20
      minimumNumberOfCalls: 10
      failureRateThreshold: 50
      slowCallDurationThreshold: 2s
      slowCallRateThreshold: 50
      waitDurationInOpenState: 30s
      permittedNumberOfCallsInHalfOpenState: 3
@CircuitBreaker(name = "paymentGateway", fallbackMethod = "statusPending")
public PaymentStatus fetchStatus(String paymentId) {
    return gatewayClient.getStatus(paymentId);
}

private PaymentStatus statusPending(String paymentId, Throwable t) {
    return PaymentStatus.PENDING; // settle later from the gateway's webhook
}

The gateway starts timing out, and 12 of the last 20 calls fail. That is a 60% failure rate, above the 50% threshold, so the breaker opens. For the next 30 seconds fetchStatus returns PENDING at once, and the order screen shows "payment processing" instead of hanging. Then three trial calls go through. If they succeed, normal traffic resumes. The final status still arrives through the gateway's webhook, so nothing is lost.

Resilience4j's defaults are larger: a window of 100 calls, a 50% failure threshold and 60 seconds in the open state. Tune them for low-traffic endpoints, where 100 calls may take hours to collect.

Netflix Hystrix made the pattern common in Java. It is now in maintenance mode, and Netflix points new projects to Resilience4j. Polly offers the same pattern in .NET.

When to use it, and what to pair it with

Use a breaker on remote calls that can fail independently of your service: third-party APIs (payments, KYC, SMS), other internal services and slow databases. Pair it with:

  • Timeouts, or a hanging call never counts as a failure.
  • Retries with backoff, only for idempotent operations, and with a limit.
  • Bulkheads that cap concurrent calls per dependency, because a breaker doesn't limit concurrency.
  • Fallbacks that make sense for the business: cached data, a queued request or a clear "try again later".

Common mistakes

  • One breaker for a whole host when only one endpoint is failing.
  • Counting 4xx client errors as failures, so bad input from users trips the breaker.
  • No alert when a breaker opens. An open breaker is a symptom someone should look at, so show breaker state in your observability dashboards.

Our monolith vs microservices for startups guide explains what a move to microservices demands, including retries, circuit breakers and tracing.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call