Circuit Breaker Pattern
- In Turkish
- Circuit Breaker Deseni
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
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
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
- MicroservicesSoftware Architecture, p. 27Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
- Service MeshDevOps & Cloud, p. 48A service mesh is an infrastructure layer that manages traffic between microservices, adding encryption, retries, routing, and monitoring without code changes.
- API GatewayBackend & APIs, p. 3An API gateway is a server that sits in front of a group of backend services and acts as the single entry point that receives, checks, and routes API requests.
- Rate LimitingBackend & APIs, p. 37Rate limiting is a technique that caps how many requests a client can make to a server or API within a time window, protecting it from abuse and overload.
- ScalabilitySoftware Architecture, p. 36Scalability is a system's ability to handle growing amounts of work, such as more users or data, by adding resources without a drop in performance.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
Sources
Spotted a mistake or something missing on this page?Suggest an edit