Circuit Breaker Pattern 00:00:01 Closed, open and half-open Payments has stopped responding, but the storefront keeps spending workers on calls that are already unlikely to succeed. How can one caller decide when to stop asking and how will it discover that payments is back? Calls pass. A success sets the counter to zero. The service answers with an error. 1 of 3. The third failure. From now on calls are refused in microseconds, and a timer runs. The wait is over. A limited number of test calls may pass. If any test fails, it opens again at once and the wait begins anew. Two successes. Normal operation, counter at zero. Closed means ordinary calls pass. Open rejects or falls back quickly. After a cooling period a limited probe tests recovery without immediately admitting the entire accumulated load. 00:00:54 Choose evidence that fits the traffic Two rules for opening, three series of results. Five good calls. Both rules are quiet. Five failures in a row open the counter. 5 of 10 is 50% and opens the rate. Half of the users get errors. The counter never passes 1. The rate opens after 10 calls. One of two failed. 50%, but two calls prove nothing. The minimum keeps it closed. Consecutive failures react differently from a windowed failure ratio. Give ratios enough observations and classify errors carefully so one invalid customer request does not make a healthy provider look unavailable. 00:01:34 A response after the deadline A call from the time before comes back while the breaker is testing. At 0 seconds, in generation 1. It will hang for 40 seconds, because it has no timeout. B fails and reaches the threshold. The breaker opens: generation 2. Half-open will be generation 3. At 36 seconds, half-open, test call C in generation 3 is fine. At 40 seconds A fails. Its ticket says 1, now is 3: ignored. Otherwise a healthy service would be cut off again. A local timeout belongs to the caller's history. A late remote success may still have moved money. Keep the operation identity and reconciliation logic independent of the breaker's state. 00:02:17 Scope the failure A third of the keys cannot be read. The overall error rate is 33%. It opens. Now nobody can read anything. A partial failure became a full one. It stays closed. The callers of B wait for each timeout. Only B's breaker opens. A and C work, and callers of B get their fallback at once. Keep independent destinations from sharing one global breaker when failures are isolated. Conversely, dividing a shared dependency too finely can hide the evidence needed to protect its actual capacity. 00:02:51 Payments resumes under control 100 orders a second, a timeout of 1 second, 200 threads. A payment takes 0.2 seconds: 100 x 0.2 equals 20 threads. Payments hangs. Every order waits a second: 100 threads, and every customer waits a second for an error. After the first failures calls are refused. Orders are stored as pending. Three tests succeed. The breaker closes and the pending orders are charged slowly, each with its key. Probe with limited traffic and reconcile pending orders using stable payment identifiers. Opening a breaker early should preserve resources without inventing a new result for any order already in flight. 00:03:36 The dependency starts flapping The service manages 60% of the normal load, not more. Full load. The service fails. Three calls are no problem. It closes, the full load returns, it fails. Every 30 seconds. 25%, then 50%. At 75% errors return, so it stays at 50% and the rest gets the fallback until capacity is repaired. Increase traffic in measured increments and observe the workload each newly admitted fraction creates. Reaching half-open is evidence for an experiment, not a certificate of full recovery. A circuit breaker is a local admission decision based on observed outcomes. It does not roll back an earlier charge, prove a remote payment failed, or fix the dependency that opened it.