A brief flicker
When the toaster gets no power, it's often just a flicker. The sensible thing is to try again, but not frantically: wait a moment, then a little longer each time, and give up after a few tries.
Retries with backoff and jitter
Many failures in distributed systems are transient: a dropped connection, a brief overload, a leader election. Retrying helps, but naive retries make overload worse. Good retries:
- use exponential backoff (1s, 2s, 4sโฆ) with a maximum,
- add jitter (randomness), so thousands of clients don't retry at the same instant,
- are capped in number, with an overall timeout,
- are only used for idempotent operations, or ones carrying an idempotency key.
The fuse trips
But if the kettle is actually broken, retrying forever just keeps the kitchen waiting. A fuse box notices the repeated faults and trips, cutting that circuit off. Now anything that needs the kettle hears "no" instantly and can use a backup plan, and the rest of the house carries on.
The circuit breaker
When a dependency is down, every call waits for a timeout, holding a thread or connection. Callers pile up, run out of threads, and fail too: a cascading failure. A circuit breaker wraps the calls:
| State | Behaviour |
|---|---|
| Closed | Calls pass through; failures are counted |
| Open | Calls fail immediately, without touching the dependency |
| Half-open | A few trial calls test whether it has recovered |
Failing fast frees resources and gives the struggling service room to recover. Pair it with a fallback: cached data, a default, or queueing the work.
Testing before switching back
After a while the fuse box lets one careful test through. If it works, everything switches back on. If not, the fuse stays off a while longer.
Recovery, and in Java
After the open wait, the breaker goes half-open. Successful trials close it; a failure reopens it. With Resilience4j in Spring Boot:
1@CircuitBreaker(name = "payments", fallbackMethod = "queueForLater")2@Retry(name = "payments")3public Receipt charge(Order order) {4return paymentsClient.charge(order);5}
Thresholds, wait durations and backoff are set in application.yml under resilience4j.circuitbreaker and
resilience4j.retry.
When the fuse box gets it wrong
Fuses can go wrong too, in three ways:
- Sometimes every cook tries again at once, and the kitchen asks the cook next door, who asks the cook next door, until one kettle gets twenty-seven requests for one cup. So the kitchen makes only one cook retry.
- Sometimes the kettle is not broken, only very slow, and the fuse box is set to watch only for sparks, so it never trips while the cooks stand waiting. So the kitchen makes the fuse box watch for slowness as well.
- And when the fuse does trip, a cook who just hands out yesterday's tea can hide that the kettle is still broken. So the kitchen sets a sign on the tea saying how old it is.
What can go wrong
- Retry amplification. Each layer retries on its own: with 3 attempts at each of 3 layers, one user request can become 3 x 3 x 3 = 27 calls to the bottom service, just when it is struggling. The same happens when an inner timeout is longer than the caller's own: the caller gives up and retries while the first call is still running. Observe: dependency traffic several times the user traffic, rising during an incident. Fix: retry in one layer only, keep backoff with jitter, and give each inner call a shorter timeout than the one above it.
- Retrying a non-idempotent call. A timeout does not mean the call failed; the charge may have succeeded and only the reply was lost. Retrying creates a duplicate. Observe: double charges, duplicate rows. Fix: send an idempotency key so the server ignores the repeat, or only retry safe calls (see idempotency).
- A breaker that never opens. Resilience4j defaults:
minimumNumberOfCallsis 100, so a low-traffic service isn't judged until 100 calls have been recorded (and with a time-based window, maybe never);slowCallRateThresholdis 100 (percent), so slow calls only open it when every call is slow;slowCallDurationThresholdis 60 seconds. A dependency that answers in 8 seconds with no errors looks healthy. Observe: breaker stays CLOSED while threads block and latency climbs. Fix: lower the minimum calls, set a realisticslowCallDurationThresholdand aslowCallRateThresholdbelow 100, and add a timeout. - Flapping and a probe stampede. Half-open allows
permittedNumberOfCallsInHalfOpenStatetrial calls (default 10) and admits the first 10 calls that arrive, concurrently, so under load a just-recovering dependency gets a burst; if enough of them fail (failureRateThreshold, default 50%), the breaker reopens; repeat. Observe: state transitions OPEN, HALF_OPEN, OPEN in the metrics, with latency oscillating. Fix: fewer trial calls, a longer or growing open wait (waitIntervalFunctionInOpenState), and a larger sliding window. - A fallback that hides the outage. Returning cached or default data keeps pages loading, but nobody notices the dependency is down, and users may act on stale or wrong data (an old price, an empty cart). Observe: no errors, yet a breaker stuck open or fallback counters rising. Fix: alert on breaker state and fallback use, mark fallback data as stale, and only fall back where wrong data is harmless.
| Choice | You gain | You pay |
|---|---|---|
| More retries | Recovers more transient failures | More load on a struggling service, longer waits, duplicates if unsafe |
| Short timeouts | Threads freed fast, quick failure | Slow but valid calls get cut off, more retries |
| Fallback instead of fail fast | The page still works | Hidden outages, stale or wrong data |
| Per-instance breaker state | No shared store, no extra failure point | Each instance learns the outage alone, so the dependency still gets probes from all of them |
Resilience4j keeps breaker state inside each JVM. Calls rejected by an open breaker throw
CallNotPermittedException, which a retry should not keep retrying.
Timeouts first: without a timeout on every network call, neither retries nor breakers can help, because a call can hang forever.
Bulkheads limit how many concurrent calls go to each dependency, so one slow service can't take every thread.