← Building blocks

Stop calling a failing dependency

Reliability & failure handlingsenior

A dead service makes every caller wait for a timeout, cascading the outage.

Say it like this

When a dependency is down, waiting for every call to time out is the thing that spreads the outage, so a breaker watches the error rate, opens after a threshold, fails fast with a fallback, and probes periodically to close again.

Basic solution

Track failures; after a threshold open the circuit and fail fast, probing occasionally to close it again.

Resilience4jHystrix (retired)Envoy outlier detection

Closed, open, half-open

Failures above the threshold open the breaker; calls then fail fast to the fallback. After the open interval a few probes decide whether to close it again.

How it works

  1. Closed is the normal state: calls pass through to the dependency. The breaker records each outcome, success or failure, in a sliding window such as the last 100 calls or the last 10 seconds.
  2. If the failure rate in the window passes the threshold (say 50%, and only once at least 20 calls have been seen, so a single failure at low traffic does not count), the breaker trips to Open.
  3. Open: every call fails immediately without touching the dependency, and the caller runs its fallback instead. A timer starts for the open duration, say 30 seconds, during which the dependency gets no traffic from this breaker.
  4. When the timer ends the breaker goes Half-open: it lets a small number of trial calls through (say 3). If they succeed the breaker closes and normal traffic resumes; if any fail it reopens and restarts the timer, often for longer than before.
  5. The breaker exports its state and every transition as metrics, so operators can see which dependencies are being skipped and for how long.

Scope the breaker correctly

  • One breaker per dependency per host, sometimes per endpoint. A breaker shared across unrelated endpoints trips on the wrong signal, because a bug in one endpoint opens the circuit for all of them; a breaker per request has no history and can never trip.

Worked example

Rate limiter in the API gateway calling Redis

  • 20K req/s through the gateway, each doing one Redis EVALSHA with a 5 ms timeout.
  • Redis becomes unreachable. Without a breaker: every request waits 5 ms for nothing, the gateway’s latency floor rises by 5 ms, and with 20K req/s the connection pool to Redis is pinned at its limit while useful requests queue behind dead ones.
  • With a breaker: window of 100 calls, threshold 50% errors, minimum 20 calls. It opens within 10 ms of the outage. Each request now costs a few microseconds for the open check and falls back to a local in-process token bucket sized at limit ÷ N gateways.
  • Half-open every 5 s with 3 probe calls. Redis is back after 90 s; the 19th probe batch succeeds and the breaker closes. Total extra Redis load during the outage: 54 probe calls.
  • Fail-closed exceptions: login and OTP endpoints are configured to reject while the breaker is open, because an approximate limit is not acceptable there.

Variants and when each wins

Count-based window

  • The breaker trips on the failure rate over the last N calls. It wins under high traffic because it reacts within a few hundred calls; at low traffic it can trip on a tiny sample unless you set a minimum call count.

Time-based window

  • The breaker trips on the failure rate over the last T seconds. It wins at low volume because it is steadier; at high volume it reacts more slowly than a count window would.

Slow-call breaker

  • Calls slower than a threshold count as failures, so the breaker opens when the dependency degrades in latency, before timeouts even start firing. It wins when a slow dependency hurts you as much as a dead one.

Outlier ejection in a proxy (Envoy, service mesh)

  • The mesh removes an unhealthy upstream host from the pool for a while, and the application code never sees a breaker. It wins for per-host failures; it is blind to logical errors such as a dependency that returns 200 with wrong data.

Use a count-based window with a minimum sample plus slow-call detection in code; let the mesh do per-host ejection underneath.

Failure modes

Breaker never opens

  • The outage cascades anyway. It happens because the failure threshold or the minimum call count is set higher than the real traffic ever reaches, or because timeouts are not counted as failures at all. Count timeouts as failures and size the window to a few seconds of real traffic.

Breaker flaps

  • The state toggles every few seconds and clients see success and fast-fail alternating. It happens because half-open lets too many probes through, or the open duration is too short to let the dependency recover. Allow only a few probes, grow the open duration exponentially, and require several consecutive successes before closing.

Wrong fallback

  • The breaker opens and users see a blank page or a 500. It happens because nobody defined what fail-fast should return at that call site. Define the fallback per call site, such as a cached value, a default, or a clear error, and test that path.

Shared breaker trips on unrelated errors

  • A bug in one endpoint takes every call to that service offline. It happens because one breaker covers the whole service, so errors from the broken endpoint open it for the healthy ones too. Use breakers per endpoint or per error class, and do not count 4xx responses as failures.

Fail-open where it should fail closed

  • Rate limits silently disappear during a Redis outage. It happens because a blanket fallback allows everything when the breaker is open. Choose fail-open or fail-closed per endpoint: approximate limits for ordinary traffic, rejection for login and OTP.

Interviewer follow-ups

Answer out loud before you open each one.

What is the difference between a breaker and a retry?

A retry tries harder for one request; a breaker stops trying for all requests. They work together: retries handle blips lasting milliseconds, the breaker handles outages lasting seconds or minutes and keeps retries from amplifying them.

How do you choose the threshold and window?

The window should hold a few seconds of traffic so the breaker reacts fast but not on noise; set a minimum call count so one failed call at 3 a.m. does not trip it. Fifty percent over 20 to 100 calls is a common starting point, then tune on real incidents.

Where do you put the breaker in a service mesh world?

Per-host ejection and connection limits belong in the mesh. The application still needs its own breaker for logical failures and for choosing the fallback, because the mesh cannot know that an empty recommendations list is an acceptable answer.

How do you test a breaker?

Fault injection: make the dependency return errors or sleep past the timeout in a staging environment, and assert the breaker opens, the fallback is served, latency drops, and it closes after recovery. Also assert the metrics and alerts fire; an invisible breaker is a debugging nightmare.

Connections

In study paths
Making retries safe
Pairs with
Never wait forever · Retry transient failures carefully · Decide what happens when a guard is down · Degrade instead of failing · Isolate failures
Used in
Distributed Rate Limiter · Notification System

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon ElastiCacheRedisMemorystoreAzure Managed RedisRedis, Valkey, Memcached