Circuit Breakers: Stop Retry Storms Against Failing Dependencies
A circuit breaker temporarily blocks calls after recent failures to a remote service or shared resource cross a threshold. It prevents more requests from piling up when they are likely to fail. Retries assume a brief failure may soon recover; a circuit breaker decides that calls should pause. This protects both the dependency’s recovery time and the caller’s resources.
A circuit breaker temporarily blocks calls after recent failures to a remote service or shared resource cross a threshold. It prevents more requests from piling up when they are likely to fail. Retries assume a brief failure may soon recover; a circuit breaker decides that calls should pause. This protects both the dependency’s recovery time and the caller’s resources.
Closed, open, and half-open states
A circuit breaker usually works as a state machine:
**Closed:** Requests pass through while recent failures are observed. When failures reach a threshold within a time window, the breaker opens.
**Open:** Calls are not sent to the dependency. They fail quickly or return a meaningful cache, default, or degraded response. After a wait period, the breaker moves to half-open.
**Half-open:** A limited number of trial calls pass through to check recovery. Success returns the breaker to closed; failure opens it again and restarts the wait.
Half-open prevents all waiting requests from flooding a service that is still recovering. State transitions can use a failure count, observation window, timeout signals, consecutive successes, and an open duration. Choose values to match dependency latency, recovery behavior, and the application’s tolerated error rate.
Distinguish retries from a circuit breaker
Retries make a limited number of attempts for failures that may be temporary, such as a network interruption or brief throttling. A circuit breaker rejects calls quickly when repeated failures persist, limiting failure propagation. The patterns can work together, but repeatedly retrying because the breaker is open defeats its purpose. Retry only transient failures for a bounded time, and have retry logic respect fast rejection from an open breaker.
Set an appropriate timeout for each call first. A timeout that is too long can tie up threads, connections, and memory during an outage; one that is too short can misclassify slow but healthy requests. Include timeouts and retry delays within the caller’s overall deadline. A circuit breaker does not fix an excessively long timeout.
Decide which failures count
Counting every failure alike can open the breaker for the wrong reason. Connection refusals, timeouts, persistent server errors, or explicit overload responses may indicate a failing dependency. Invalid input, authorization failures, and missing records usually describe an individual request, not a dependency outage. Consider a minimum sample count or observation window so a few failures during very low traffic do not dominate the rate.
Match the breaker’s scope to the failure domain. If independent regions, shards, or routes share one failure counter, a problem in one can block healthy paths. If every application instance has an isolated breaker, many instances may continue to hit the same failing dependency. Observe latency, errors, and breaker state, then decide whether shared distributed state is worth its coordination cost.
Choose what to return while open
Decide per feature what callers should see. For data where freshness is less important, a cached result may work. A noncritical feature can be hidden temporarily or use a default. For essential payments or order processing, do not disguise failure as success: return a clear temporary failure and let the client or job queue retry safely later. If a fallback exists, verify it has equivalent data meaning and sufficient capacity. Do not record a default as a successful dependency result.
In message processing, immediate failure may cause rapid redelivery. Coordinate with broker-delayed retries, backoff, and dead-letter handling; consider whether work can wait while the breaker is open. A circuit breaker does not replace business error handling or message recovery policy.
Set thresholds and verify recovery
For each dependency, define and record the failure threshold, observation window, timeout, open duration, half-open trial volume, and required consecutive successes. A threshold that is too low can block normal service during a brief wobble; one that is too high may not stop resource exhaustion or cascading failures. A very short open period repeatedly loads a recovering service; a very long one keeps functionality closed after recovery.
Track state changes, short-circuited calls, fast failures, fallback rate, half-open trial outcomes, and dependency latency and errors. In staging or fault-injection exercises, reproduce dependency timeouts, consecutive failures, recovery, and half-open failure to confirm calls are isolated and state returns as expected. If operators can reset a breaker manually, define authorization, audit logging, and recovery steps. A circuit breaker is not a substitute for exception handling in business logic or local in-memory access, and it can add needless complexity when retries or message-queue recovery are already sufficient.
Reference
[Microsoft Learn: Circuit Breaker pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker)