Circuit Breaker Mechanism: Detection, Algorithm, Time Window, Duration, Manual Trigger, Global Switch, and Monitoring
This article explains a project's circuit breaker implementation, covering detection steps, the algorithm based on request count and failure rate, time‑window statistics, recovery duration, manual activation, a global enable switch, and how to monitor its current state.
In a project we need a circuit breaker to prevent excessive pressure on third‑party API calls; the following sections describe the mechanism used.
0x01. Circuit Breaker Detection Mechanism
(1) After a request reaches the backend, first check whether the circuit‑breaker switch is on. (2) If the switch is on, the request is rejected. (3) If the switch is off, check whether the time window for error‑rate statistics is full. (4) If the window is not full, increment the request count in the request bucket (Redis). (5) If the response contains an error, increment the failure count in the failure bucket (Redis); otherwise increment the success count. (6) If the window is full, evaluate whether to trigger a circuit break.
0x02. Circuit Breaker Algorithm
Necessary conditions: (1) Total request count > X (a predefined threshold). (2) Failure rate > Y (a predefined threshold). The total request count is obtained from the request bucket in Redis. Failure rate = (failure count ÷ request count) × 100%. When both conditions are met, the API is considered to have too many failures and the circuit breaker is triggered.
0x03. Statistics Time Window
(1) For each request, the system checks whether the time window (e.g., 5 minutes) is full; if full, the window resets and request/success/failure counters are cleared. (2) The start time of the first window defaults to the current time.
0x04. Circuit Breaker Duration
(1) When a problem occurs, all request paths can be broken; the default recovery time is 1 minute, adjustable per environment. (2) The recovery time can be dynamically increased if the environment (e.g., network) remains poor.
0x05. Manual Circuit Break
Because the circuit breaker relies on failure‑rate statistics, sometimes a rapid cut‑off is needed—for example, when a surge of recharge requests leads to many refunds, manually breaking the package‑retrieval API prevents further recharges.
0x06. Global Circuit Breaker Switch
In scenarios where circuit‑breaker detection is not required, a global switch can enable or disable the entire detection logic.
0x07. Monitoring Current Circuit Breaker State
After enabling detection, you can verify its operation by checking specific monitoring parameters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
