Circuit Breaker Pattern
1 min read
Pronunciation
[sur-kit bray-ker pat-ern]
Analogy
Like an electrical circuit breaker that trips under overload, a smart contract circuit breaker trips when abnormal conditions occur.
Definition
A variant of the Emergency Stop Pattern that automatically disables contract functions when predefined thresholds or error rates are exceeded.
Key Points Intro
Circuit Breaker Pattern adds automated safeguards via:
Key Points
Error threshold: Tracks failure count or rate of exceptions.
Automatic trip: Switches to ‘open’ state when threshold crossed.
Cool‑down period: After a timeout, allows limited test calls in ‘half‑open’ state.
Recovery logic: Resets counters and resumes normal operation if tests succeed.
Example
A DeFi lending pool counts failed collateral checks; if failures > 10 in an hour, it trips and only allows repayments until recovery.
Technical Deep Dive
Maintain a struct `{ failures, lastWindowStart, state }`. On each operation, increment failures on error and check against `maxFailures`. Transition state: CLOSED→OPEN when threshold hit, record `openedTimestamp`. In OPEN state, reject calls until `openedTimestamp + timeout`. In HALF‑OPEN, allow a limited number of calls to test; on success reset to CLOSED.
Security Warning
Incorrect threshold or timeout settings can cause unnecessary downtime or fail to protect under attack; tune parameters based on realistic metrics.
Caveat
Adds complexity and on‑chain storage/logic costs; ensure gas overhead is acceptable.
Circuit Breaker Pattern - Related Articles
No related articles for this term.