A slow dependency can hold connections and request workers until unrelated requests start failing. A circuit breaker reduces repeated calls after failures accumulate. Graceful degradation defines what the application can still return while that dependency is unavailable.
Both need limits around them. Calls already in flight still need deadlines, and the fallback needs enough capacity to survive the outage. Start with one optional dependency and prove its failure behavior before extending the pattern across your application.
Decide what a degraded response means
Consider a hypothetical shop whose product page fetches recommendations from a separate service. The product details can still load if recommendations fail. Showing a small, locally stored list might be acceptable; blocking the whole page until recommendations recover would defeat that choice.
Write down the response contract first. For this example, it could be:
| Condition | Page behavior |
|---|---|
| Service responds | Show the returned items. |
| Call fails or is blocked | Use the cached list if its age is allowed. |
| Cached list is too old | Omit suggestions. Keep product details. |
| Product details fail | Show an explicit failure for the page. |
This is a proposed design, not a measured result or a policy every shop should adopt. Choose the allowed cache age with the person responsible for the data. A stock count, account balance or authorization decision has different consequences from a list of suggested products.
Keep the fallback cheaper than the normal request. Sending every cache miss to the same failing database adds load during the outage. Google's overload guidance describes reducing work and serving less expensive responses. Exercise your fallback during routine testing, including when its cache is empty.
What each breaker state permits
In the closed state, the breaker admits calls and records their outcomes. Once its configured failure rule is met, it opens and rejects new calls through that breaker. The application must handle that rejection, usually with a fallback or an explicit error.
After a waiting period, a half-open state admits limited trial traffic. Successful trials can restore normal admission; failed trials can reopen the breaker. Exact transition rules depend on the library. Microsoft's circuit-breaker pattern explains this cycle and the need to handle rejected calls.
Avoid implementing only a failure counter and an open flag. That leaves unanswered how calls resume, how concurrent trials are limited, and when old failures expire. Use the state machine supplied by your library, then test its behavior with your client and error types.
A breaker also needs a useful lifetime. Recreating it for each request discards the history that makes it work. Scope it to the dependency or operation that should share a failure decision. A single global breaker can make an optional recommendation failure block unrelated calls.
Sampling changes when a breaker opens
A failure percentage needs a denominator. Resilience4j supports count-based and time-based windows, plus a minimum number of recorded calls before evaluating failure or slow-call rates. Its slow-call threshold records duration; it does not terminate the call. The Resilience4j documentation also makes clear that the window size does not limit concurrent requests.
For a hypothetical count-based policy, suppose the window holds ten completed calls, the minimum sample is ten, and the breaker opens at a failure rate of at least 50%. Nine failed calls are still below the minimum sample. If the tenth succeeds, nine of ten have failed, so the rule opens the breaker. These numbers demonstrate the rule; they are not recommended production settings.
Time-based sampling has a different low-traffic problem. If too few requests arrive within the sampling period, the minimum may never be reached. Polly documents this behavior: below its minimum throughput, even an entirely failing sample does not open the circuit. Check request volume per breaker instance, including quiet periods, before choosing a threshold.
Classify outcomes explicitly. A rejected business operation, such as an invalid product identifier, need not indicate dependency failure. Decide which exceptions and HTTP results count, and how cancellations are treated. Polly exposes a predicate for this choice. Its breaker rethrows failures and does not perform retries itself.
Bound time, concurrency and retries separately
Set client timeouts that fit inside the caller's total deadline. Include the work before and after the dependency call, plus any retry delay. Check whether the client timeout covers connection setup, DNS and TLS. A timeout means the caller stopped waiting; it does not prove the remote operation had no effect. AWS explains these limits in Timeouts, retries, and backoff with jitter.
Use a bulkhead to cap concurrent work for this dependency. A bulkhead separates capacity, such as a connection pool or a fixed number of execution slots, so one failing service cannot consume every slot available to other work. Put a bound on queued work too. Microsoft's bulkhead guidance covers isolating resource pools and testing those boundaries under failure.
If retries are appropriate, give one layer responsibility for them, limit the attempts, and use backoff with jitter. Retry only operations whose effects can safely be repeated. Stop retrying when the breaker rejects admission. Otherwise, retries in several layers can multiply traffic or waste the remaining request budget on immediate rejections. AWS discusses retry amplification and retry limits; Microsoft's breaker guidance calls out handling non-transient breaker rejections.
Record where the retry logic sits relative to the breaker. A breaker around the entire retry operation sees its final outcome. A breaker around each attempt sees individual attempts. This follows from what each wrapper observes, and it changes how quickly its sample fills. Verify the arrangement your client stack actually uses.
Fit attempts inside a total deadline
Give the optional recommendation lookup a budget within the page request. In this hypothetical design, the page has 1,000 ms from application entry to its response deadline. Reserve 200 ms for other page work and 100 ms for fallback and response handling. That leaves at most 700 ms for the lookup, including waiting for an execution slot, network setup, attempts and retry delay.
The table shows one sequential allocation. These are chosen limits for the example, not measured latencies or recommended defaults.
| Work | Budget |
|---|---|
| Other page work | 200 ms |
| First attempt | 300 ms |
| Retry delay | Up to 100 ms |
| Second attempt | Up to 300 ms |
| Finish or fallback | 100 ms |
The allocation totals 1,000 ms. It leaves no spare allowance for an uncounted queue, slow cleanup or scheduler delay. Use it to find missing work in the budget before implementing the policy. Measure the complete request under load and leave headroom in the deployed settings.
Recompute the remaining budget before admitting another attempt. Suppose the first attempt and its cleanup consume 450 ms after the initial 200 ms of page work. A 100 ms retry delay then leaves only 150 ms before the reserved final 100 ms. A new 300 ms timeout would exceed the deadline. Shorten that attempt if a useful response is still plausible, or serve the agreed fallback immediately. Skip it if the caller has cancelled or the breaker is open.
A per-attempt timeout needs an outer limit that covers the whole operation. Otherwise, each retry can receive a fresh timeout even after the request has spent its budget. Treat queue waits and retry delays as part of elapsed time. Test the client stack's actual timeout coverage, especially connection establishment.
Cancellation must reach the work
An elapsed timeout does not forcibly stop every task. Polly's timeout strategy uses cooperative cancellation: the callback must honor the cancellation token passed to it. A callback that ignores that token can keep executing after cancellation is requested. Pass it through to the HTTP call and other cancellable work.
Measure when the caller gets its response and when the dependency work releases its resources. In a test where a stub ignores cancellation, a fast fallback can hide growing work in the background. Opening the breaker prevents later admissions; already admitted calls still need their own termination behavior. Record active calls and queue depth through the entire failure test, including after the public request has ended.
Retrying a write after an uncertain result
A timed-out checkout write may already have committed. A retry must follow the API's idempotency contract. For one logical operation, retain its request identifier across attempts. A deliberate second order needs a different identifier. Check the API's retention period, caller scope and behavior when the same identifier arrives with changed parameters. AWS explains these choices in Making retries safe with idempotent APIs.
For an API you own, coordinate the duplicate-request record with the business write so a crash cannot leave the effect committed without its record. Define how concurrent duplicates and late retries are answered. A key in a header only helps if the receiving service enforces this contract.
Test by committing a disposable order in staging, then dropping its response. Retry with the same identifier and verify one business effect. Try changed parameters and a retry beyond the documented retention period too. If the result remains uncertain, expose that state and reconcile it before inviting another submission. A cached recommendation is a safe fallback in this example; an invented order confirmation is not.
Recovery across several application instances
An in-memory breaker normally tracks one process's experience. Resilience4j's registry is in memory, so separate processes can disagree about the same dependency. That can be useful when only one caller has a broken network path, but it also means one process opening its breaker does not stop traffic from the others.
Account for aggregate trial traffic. In a hypothetical deployment with twenty independent instances, allowing two simultaneous trials per instance can admit forty trials across the fleet. A local limit alone cannot establish a fleet-wide limit. Include the number of callers when load-testing recovery.
A timer expiring only makes recovery eligible to be tested. Check whether your library waits for the next call or performs an automatic transition. Do not manually close every breaker merely because a public health endpoint turns green. That endpoint may use a different route, credential or operation from the failed request.
Run a failure drill and keep the evidence
Use an isolated dependency stub or a staging service where you can inject delays and failures without affecting customers. Record the configured policy, request count at the dependency, breaker transitions and caller-visible result. The following are acceptance cases to run, not results from a lab performed for this article.
| Fault | Evidence to collect |
|---|---|
| Dependency stalls | Response time and active work stay within the tested limits. |
| Callback ignores cancellation | Work that outlives the caller is visible and bounded. |
| Retry budget runs out | No attempt receives time beyond the remaining budget. |
| Write response is lost | Same operation ID produces one business effect. |
| Failures reach the rule | New calls stop reaching the dependency while open. |
| Traffic stays below the sample minimum | Observed state matches the library's rule. |
| Fallback cache is empty | The page follows the agreed degraded contract. |
| Trial calls fail | The breaker returns to open. |
| Dependency recovers | Trials and resumed traffic stay within its capacity. |
Record the client and resilience-library versions with the policy. Also test a rejected business request and a cancelled caller. Check that neither silently enters the wrong failure category. Repeat recovery with several application instances, since a single-process test misses their combined load.
Track fallback use separately from HTTP success. A page can return 200 while an optional feature remains unavailable for hours. Record dependency errors, rejected calls and time spent open, alongside the age of data served from the fallback.
External checks add a view of the public response. Internal metrics explain the breaker and fallback decisions behind it. Google's monitoring guidance distinguishes these black-box and white-box signals. For the shop example, check that product details still render, then use application telemetry to see whether recommendations are live, cached or omitted. A successful public check alone cannot establish that the dependency recovered.