Circuit Breaker Pattern: Fail Fast, Not Hard
A circuit breaker wraps network calls to prevent cascading failures. It monitors for errors, and if a service seems down, it 'trips' to fail requests instantly without hitting the network. This gives the failing service time to recover.
Why it exists
In distributed systems, services depend on each other. If one service fails or becomes slow, its callers can get stuck waiting, consuming resources like threads and memory. This can cause the calling service to fail, which in turn causes its callers to fail, creating a cascading failure. The circuit breaker pattern exists to contain the initial failure and prevent it from spreading across the system.
The mental model
It's an electrical circuit breaker in your house's fuse box, but for software. Normally, the circuit is closed, and electricity (requests) flows freely. If there's a fault (too many failed requests), the breaker trips and opens the circuit, stopping the flow entirely. This protects the system from further damage. After a cool-down period, it cautiously tests the connection before closing the circuit again.
How it works
The pattern is a state machine that wraps a protected function call. It has three states. First, CLOSED: Requests are passed through to the downstream service. The breaker monitors for failures. If the failure count exceeds a configured threshold, the breaker trips and moves to the OPEN state. Second, OPEN: For a configured duration, all calls to the protected function fail immediately without being executed. This gives the downstream service time to recover. After the timeout expires, the breaker moves to HALF-OPEN. Third, HALF-OPEN: The breaker allows a single 'probe' request to pass through. If this request succeeds, the breaker assumes the service has recovered and moves back to the CLOSED state. If the probe fails, it returns to the OPEN state.
When to use it
Use this pattern when calling remote services over a network, especially in a microservices architecture. It's ideal for protecting your application from failures in services that are either third-party dependencies or other internal services that could be unreliable or slow. It improves system resilience by providing a fast failure response instead of a long wait.
When not to use it
It's generally overkill for in-process communication or calls to a local database. These operations typically have different, more immediate failure modes that don't benefit from this pattern's stateful, time-based logic. Also avoid it if the calling application has no reasonable fallback behavior; if failing fast just shows a user a hard error page, the benefit is reduced.
One canonical example
An e-commerce frontend calls a recommendation service. The service becomes overloaded and starts timing out. The circuit breaker, wrapping the call, notes several timeouts and trips to the OPEN state. For the next 30 seconds, any call to the recommendation service fails instantly. The frontend catches this fast failure and renders the page without recommendations, preserving the core user experience instead of making the entire site hang.
Interview question
What is the primary function of the HALF-OPEN state in the Circuit Breaker pattern?
- a.To gradually reintroduce traffic to the service, starting with a low request volume.
- b.To block all requests for a short period, giving the service time to stabilize before fully reopening.
- c.To allow a single probe request to determine if the downstream service has recovered.Correct
- d.To collect more failure metrics before deciding whether to transition back to the CLOSED state.
Why? this is the answer
The HALF-OPEN state is specifically designed to allow a single 'probe' request to pass through, testing if the downstream service has recovered. If this probe succeeds, the circuit closes. Option B describes the OPEN state, where all requests are blocked.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #resilience
- #distributed systems
- #microservices
- #design patterns
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on resilience — each one lists the topics its interview covers.
See open roles