Circuit Breakers: Stopping One Slow Dependency From Taking Down Everything

Retrying a failed request is usually good advice — right up until the dependency you're retrying is genuinely down, in which case every retry from every caller just adds more load to an already-struggling system, and a service that calls it synchronously can end up with all its own resources tied up waiting on a dependency that was never going to answer anyway.
The specific failure this solves
Imagine service A calls service B on every request it handles. B starts timing out — not immediately failing, which would at least fail fast, but hanging for its full timeout window before eventually erroring. Every request to A that touches B is now stuck waiting out that full timeout. If A has a limited pool of workers or connections, and enough concurrent requests are all waiting on the same slow B, A can run out of capacity entirely — not because A itself is broken, but because all of its capacity is tied up waiting on something else. This is how one slow dependency turns into an outage of a service that doesn't even own the actual problem, a pattern sometimes called a cascading failure.
What a circuit breaker actually does
A circuit breaker sits in front of calls to a dependency and tracks its recent success/failure rate. When failures cross a threshold, the breaker "opens": for a cooldown period, calls to that dependency fail immediately, without even attempting the network call, instead of hanging for a full timeout each time. This does two things at once — it stops piling more load onto an already-struggling dependency, and it frees up the calling service's own capacity immediately instead of tying it up in doomed, slow attempts.
After the cooldown, the breaker moves to a "half-open" state: it lets a small number of test requests through to see if the dependency has recovered. If they succeed, the breaker closes again and traffic resumes normally. If they still fail, it reopens and waits longer before trying again.
This is the same underlying idea as an electrical circuit breaker, which the pattern is named after: rather than let a fault keep drawing current until something burns out, it trips and cuts the circuit, and someone (or something) checks whether it's safe to reset before power flows again.
What actually needs to be tested
The behavior worth verifying isn't "does my circuit breaker library work" (that's the library's job to have tested) — it's "does my code correctly handle the breaker being open." Concretely:
- When the breaker opens, does the calling code have a sensible fallback (cached data, a degraded response, a clear error) instead of just propagating a generic failure?
- Does the breaker actually open under sustained failure, rather than staying closed and letting every request hang out its full timeout regardless?
- Does the half-open recovery actually work — does traffic resume once the dependency is healthy again, or does the breaker get stuck open?
All three of these need a dependency that can be made to fail at a controlled, sustained rate long enough to trip the breaker, then made to recover on command — which is exactly what a mock with a configurable, adjustable chaos injection rate provides, without needing to actually degrade a real service (your own or someone else's) to observe how your circuit breaker responds to it.
Xerat02
Building MockBase.
More from the blog

Three Ways to Fake a Dependency, and When Each One Lies to You
Mocking, service virtualization, and contract testing all answer "what do I do about a dependency I don't control" — they answer it for different problems, and using the wrong one gives you a different flavor of false confidence each time.

The Content-Type Header Your Download Button Ignores (Until It Doesn't)
A browser will render, download, or silently mangle a response differently depending on one header most endpoints get right by accident — and "it worked when I tested it" usually means you tested the one case where the accident didn't matter.