Skip to content
ADevGuide Logo ADevGuide
Go back

Circuit Breaker Pattern: States, Failure Handling and Trade-offs

By Pratik Bhuite | 11 min read

Hub: Java / Interview Fundamentals

Series: Backend Interview Mastery

Last verified: Sep 19, 2026

Part 42 of 88 in the Backend Interview Mastery

Key Takeaways

On this page
Reading Comfort:

Circuit breaker pattern showing closed, open, and half-open states protecting a downstream service

When a payment provider slows down, a healthy API can spend every worker waiting for a dependency that is already failing. New requests add load to the provider and consume local connections, turning a partial outage into a cascading failure. The circuit breaker pattern fails fast after evidence of trouble and gives the dependency time to recover.

This guide explains breaker states, thresholds, fallbacks, retries, distributed instances, and the trade-off between protecting the caller and hiding a real outage. Pair it with timeouts, retries with backoff, and service discovery.

Table of Contents

Open Table of Contents

What a Circuit Breaker Does

A breaker wraps an outbound operation and tracks recent outcomes. While healthy, calls flow normally. Once failures exceed a policy, the breaker opens and rejects calls locally without touching the dependency. After a cool-down, it allows a small probe set in half-open state; success closes the circuit, while failure reopens it.

flowchart TD
    A[Caller] --> B{Circuit state}
    B -->|Closed| C[Call dependency]
    C -->|Success| D[Record success]
    C -->|Timeout or eligible failure| E[Record failure]
    E --> F{Threshold reached?}
    F -->|No| B
    F -->|Yes| G[Open circuit]
    G --> H[Fail fast or fallback]
    G --> I[Cool-down timer]
    I --> J[Half-open probe]
    J -->|Success| K[Close circuit]
    J -->|Failure| G

The breaker is a load-shedding mechanism, not a health oracle. An open circuit may protect the caller while the dependency remains broken. Emit state changes, rejected-call counts, and fallback outcomes so operators see the degraded mode instead of a mysteriously “healthy” API.

The Three States

StateBehaviorTransition
ClosedCalls are allowed; outcomes update the rolling policyOpens after a failure threshold
OpenCalls fail fast or use a bounded fallbackHalf-open after cool-down
Half-openA limited probe set tests recoveryCloses on success; opens on failure

Use a rolling window or consecutive-failure threshold with a minimum request volume. Opening after one unlucky timeout creates flapping; waiting for thousands of failures wastes resources. Choose the window from the dependency’s latency and error SLO, not a copied default.

Choosing Failure Signals

Count timeouts, connection failures, 5xx responses, and explicit overload signals such as 429 when they indicate the dependency cannot serve the operation. Do not count ordinary 4xx validation errors as dependency health failures; they are usually caller mistakes. Separate slow-call rate from error rate because a dependency can be returning successful but unusably slow responses.

Breakers should usually be scoped to a meaningful dependency boundary: host, operation, tenant, or shard. One global breaker for an entire downstream fleet can make a single bad tenant take down healthy traffic. Too many tiny breakers can hide an outage and produce noisy state management, so choose a scope aligned with failure isolation.

Fallbacks and Retry Interaction

Fallbacks should preserve correctness, not merely return a cheerful response. A catalog read may use stale cached data; a payment authorization usually cannot be faked and should return a pending or retryable state. A fallback must be observable and have an expiration policy.

Retries and breakers need one owner. If a caller performs three retries and every retry passes through a closed breaker, the breaker should record the underlying failures without multiplying attempts across every stack layer. Open breakers should normally stop retries immediately. Use exponential backoff and jitter for the small number of operations that remain eligible.

Distributed and Operational Concerns

An in-process breaker protects each application instance independently. That is often desirable because one instance’s network path can fail while others remain healthy. A shared Redis breaker can coordinate a fleet but adds another dependency and consistency problem; a stale shared state can either send traffic to a broken service or reject healthy traffic.

Instrument:

  • Current state and state-transition reason.
  • Allowed, rejected, timed-out, and successful calls.
  • Half-open probe count and recovery duration.
  • Fallback count and freshness or business outcome.
  • Dependency latency and errors by operation and zone.

Test half-open recovery, dependency flapping, process restarts, clock changes, and a breaker store outage. An open circuit should not hold threads or connections while waiting for the cool-down timer.

Implementation Sketch

function callWithBreaker<T>(operation: () => Promise<T>): Promise<T> {
  if (breaker.isOpen()) {
    metrics.increment("dependency.rejected");
    return Promise.reject(new DependencyUnavailableError());
  }

  return operation()
    .then(value => {
      breaker.recordSuccess();
      return value;
    })
    .catch(error => {
      if (isDependencyFailure(error)) breaker.recordFailure();
      throw error;
    });
}

The real implementation must enforce a timeout around operation, limit concurrent half-open probes, and make the state transitions atomic within the chosen scope. Do not let a slow call remain in flight forever after the circuit opens.

Interview Questions

1. What problem does a circuit breaker solve?

It prevents repeated calls to a dependency that is likely to fail, reducing resource consumption and limiting cascading failure. The caller can fail fast or use a bounded fallback while the dependency recovers. It does not repair the dependency or guarantee that the fallback is correct.

2. How do closed, open, and half-open differ?

Closed allows calls and records outcomes. Open rejects calls locally for a cool-down period. Half-open allows a small probe set to test recovery; success closes the circuit and failure returns it to open.

3. Should a 404 open the breaker?

Usually not. A 404 often means the requested resource does not exist and says nothing about dependency health. Timeouts, connection failures, and selected 5xx or 429 responses are more appropriate signals, but the policy should follow the dependency contract.

4. How should a breaker interact with retries?

The breaker should stop retries once it is open, and retry logic should live at one deliberate layer. Otherwise several layers multiply attempts and overload the failing dependency. Each retry needs a deadline, an idempotency policy, and a shared attempt budget.

5. Should breaker state be stored in Redis?

Only when coordinated fleet-wide behavior is more valuable than the extra dependency and consistency complexity. Local state is simpler and often provides better failure isolation. A shared store needs TTLs, atomic updates, a store-outage policy, and metrics for stale or conflicting state.

Conclusion

The circuit breaker pattern protects resources by turning repeated downstream failures into fast, observable decisions. Define failure signals, state thresholds, cool-downs, probe limits, and fallbacks around the real SLO. Combine breakers with timeouts, bounded retries, idempotency, and alerts; a breaker without those contracts only moves uncertainty to a different layer.

References

  1. Microsoft Azure Architecture Center: Circuit Breaker Pattern
  2. AWS Builders’ Library: Timeout, retries, and backoff with jitter
  3. Resilience4j: CircuitBreaker

YouTube Videos

  1. “Circuit Breaker Pattern Explained: Timeouts, Retries and Fallbacks” - Software Architecture [https://www.youtube.com/watch?v=rvKKj7JU92g]

Share this post on:

Next in Series

Continue through the [object Object] with the next recommended article.

Related Posts

Keep Learning with New Posts

Subscribe through RSS and follow the project to get new series updates.

Was this guide helpful?

Share detailed feedback

Previous Post
Exponential Backoff with Jitter: Retry Budgets and Safe Retries
Next Post
Service Discovery: DNS, Registries and Kubernetes Services