Skip to main content

Circuit Breaker Pattern

Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/circuit-breaker

In short

The circuit breaker pattern protects a system by stopping calls to a failing dependency for a while and failing fast instead of waiting on timeouts.

What is the circuit breaker pattern?

In distributed systems, one service often depends on others over the network. When a dependency becomes slow or unavailable, callers that keep sending requests waste threads and connections waiting for timeouts, and the problem can spread until the whole system slows down, which is called a cascading failure. The circuit breaker pattern wraps these remote calls in a guard that notices repeated failures and stops calling for a while.

A circuit breaker has three states. In the closed state, requests pass through normally while the breaker counts failures. When failures cross a threshold, for example 5 in a row or half of recent calls, it trips to open, and every call fails immediately or returns a fallback, such as cached data, without touching the struggling service. After a cooldown period it moves to half-open and lets a few trial requests through: if they succeed it closes again, and if they fail it reopens.

The name comes from the electrical circuit breaker in your home: when too much current flows, it trips and cuts power to protect the wiring, and you reset it once the problem is fixed. Software circuit breakers are common in microservices, API clients, and service meshes, and they are usually provided by resilience libraries or proxies rather than written from scratch.

Circuit breakers are often confused with retries and rate limiting. Retries repeat a failed call hoping it succeeds, which can make an overloaded service worse, while a circuit breaker deliberately stops calling; the two work best together, with retries for brief glitches and the breaker for longer outages. Rate limiting, by contrast, protects a service from too many incoming requests, whereas a circuit breaker protects the caller from a failing dependency.

At a glance

A circuit breaker has three states. Closed: requests go through and failures are counted. When failures reach a threshold it opens: calls fail at once without reaching the struggling service. After a timeout it turns half-open and lets one test request through; if that succeeds it closes again, if it fails it opens again.Closedrequests go throughOpencalls fail at onceHalf-openone test requestfailures reach a thresholdafter a timeouttest failstest succeeds
Like a fuse in a house: when a service keeps failing, the breaker stops sending it traffic, gives it time to recover, then tries again carefully.

Key takeaways

  • A circuit breaker wraps remote calls and stops them after repeated failures.
  • Closed passes calls through, open fails fast, and half-open tests whether the service recovered.
  • Failing fast prevents cascading failures and frees up threads and connections.
  • Fallbacks, such as cached data or a default response, keep the user experience usable.
  • It complements retries and timeouts rather than replacing them.

Example

A minimal circuit breaker in TypeScripttypescript
let failures = 0;
let openedAt = 0; // when the circuit last tripped open

async function withBreaker<T>(fn: () => Promise<T>): Promise<T> {
  // Open: after 5 failures, fail fast for 30 seconds without calling the service
  if (failures >= 5 && Date.now() - openedAt < 30_000) throw new Error("Circuit open");
  try {
    const result = await fn(); // closed, or a half-open trial after the cooldown
    failures = 0; // success closes the circuit
    return result;
  } catch (err) {
    if (++failures >= 5) openedAt = Date.now(); // trip (or re-trip) to open
    throw err;
  }
}

Readers ask

What are the three states of a circuit breaker?

Closed means calls flow normally while failures are counted. Open means calls fail immediately without reaching the service. Half-open means a few trial calls are allowed through after a cooldown to check whether the service has recovered.

What is the difference between a circuit breaker and a retry?

A retry repeats a failed call in case the problem was temporary. A circuit breaker stops making calls after repeated failures so a struggling service has time to recover; retries usually run inside the breaker, with a growing delay between attempts.

What happens to requests while the circuit is open?

They fail fast, typically with an error the caller can handle, or they return a fallback such as cached data, a default value, or a friendly message. This keeps the caller responsive instead of hanging on timeouts.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings