Highlights

Contents

Part V
05

Timeouts, circuit breakers, bulkheads

We prevent a failed dependency from consuming every resource in its callers. A bounded wait and a bounded amount of waiting solve different problems. We will trace deadlines, open a breaker, recover through probes, and isolate resource pools.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Availability and SPOFs2Redundancy,active-active, passive3Graceful degradation andfault tolerance4Retries, exponentialbackoff, and jitter5Timeouts, circuitbreakers, bulkheads6Health checks andfailover7Disaster recovery, RTO,and RPO8Case study: one incident

#Section 17Timeouts and end-to-end deadlines

A viewer’s request has an illustrative total budget of 500 milliseconds. Heron reserves 40 for network transit and 60 for assembling and returning the reply, leaving 400 for application work. After 120 has been spent, a new downstream call has only 280 useful milliseconds.

A timeout bounds a particular wait. A deadline bounds the completion time of the whole operation. Pool acquisition, connection setup, and response reading can each have timeouts, but those waits still sit inside the same outer deadline.

500 MS: 40 + 60 RESERVED, 400 USEFUL; 120 SPENT, 280 LEFT FOR THE REST500 MS: 40 + 60 RESERVED, 400 USEFUL; 120 SPENT, 280 LEFT FOR THE RESTnetspent 120remaining 280reply0100200300400500 msthe next call inherits 280 ms, not a fresh budget
Figure 11.19Illustrative serial budget. A downstream request inherits the remaining time rather than restarting the clock.

Propagate a remaining duration or a carefully interpreted deadline across each boundary. Check it again when a queued request begins execution. A request that already expired should not start expensive optional work. Picking values requires latency measurements and false-timeout analysis, rather than copying an arbitrary timer from another service.

#Section 18A timeout does not roll back an effect

The application abandons a database wait, but the database continues executing. It may commit just after the timeout. The caller has stopped observing; that is not the same as the callee undoing work.

Propagate cancellation where supported, release caller resources, and record an uncertain outcome where necessary. Cancellation can itself race with completion. For a write, recover through the original command identity and authoritative state. For a read, a later attempt can be simpler, but it still consumes capacity and may see a newer version.

THE CALLER'S DEADLINE FIRED, THEN THE COMMIT FINISHED: BOTH ARE TRUETHE CALLER'S DEADLINE FIRED, THEN THE COMMIT FINISHED: BOTH ARE TRUEcaller waitsdeadlinedatabase write Kcommitlater: look up K, never assume the timeout rolled it back
Figure 11.20An uncertain-outcome trace. A timeout stops waiting; it does not prove rollback.

The cost of this design is retained outcome evidence and a recovery API or idempotency contract. If a downstream service lacks such a contract, the upstream cannot invent certainty. It must expose uncertainty or reconcile before issuing another effect. That limitation belongs in the interview answer and in the user-facing status.

#Section 19Circuit breaker states and recovery probes

A recommendations dependency repeatedly times out. Even bounded waits occupy slots for calls that are unlikely to help. A circuit breaker stops selected calls temporarily after evidence of failure. Its purpose is to protect resources and permit recovery, not to establish that the peer is certainly dead.

In the closed state, calls flow and the breaker collects outcomes. When its configured failure rule triggers, the open state rejects or uses a permitted fallback without calling the peer. After a cooldown, the half-open state admits a small controlled probe set. Successful probes can close it; renewed failure reopens it.

A CIRCUIT BREAKER OPENS THE CALL PATH UNTIL BOUNDED PROBES SUCCEEDA CIRCUIT BREAKER OPENS THE CALL PATH UNTIL BOUNDED PROBES SUCCEEDclosedcalls admittedopencalls blockedhalf-openprobe only
Figure 11.21Breaker states. Failure thresholds and probe capacity are explicit policy choices.
OPEN, HALF-OPEN WITH A FEW PROBES, THEN CLOSED; A FAILED PROBE REOPENSOPEN, HALF-OPEN WITH A FEW PROBES, THEN CLOSED; A FAILED PROBE REOPENSopenreject calls,cool downhalf-opena boundednumber of probesclosedadmit, ramp uptimerprobes passprobe failsone passing probe is not full capacity; raise traffic gradually
Figure 11.22Recovery states. A working probe does not establish full recovered capacity.

For a concrete illustrative policy, keep the last 20 eligible dependency outcomes and open when at least 10 failed. A completed window with 12 failures has failure fraction 12 divided by 20, or 0.6, so it opens. If that observation occurs at time 5 seconds, a 30-second cooldown permits recovery probes at time 35. The half-open state admits at most 2 probes; other ordinary calls still use the rejected or fallback path.

The state transition matters. Closed state collects the window, open state clears ordinary downstream admission, and half-open reserves the probe slots atomically so a burst of callers cannot all become probes. Under this example both successful probes close the breaker and reset its rolling history; any failed probe reopens it and starts another cooldown. This is a declared teaching policy, not a universal library default. A production rule needs measurements for its window, threshold, and recovery ramp.

12 OF 20 CALLS FAIL (0.6): OPEN AT 5 S, COOL 30 S, PROBE TWICE AT 35 S12 OF 20 CALLS FAIL (0.6): OPEN AT 5 S, COOL 30 S, PROBE TWICE AT 35 Swindow of 20: 12 failures ≥ threshold 10 (illustrative pattern)closedopen: reject, cool down 30 s2 probes0510152025303540 sprobe slots are reserved atomically so a burst cannot all become probes
Figure 11.23Illustrative breaker policy and state times. All counts and times are policy assumptions checked by the chapter ledger.

Failures caused by invalid client requests should not poison a dependency breaker. Nor should a breaker make permission checks optional. Local breakers at different callers may disagree because they see different routes and histories. This is acceptable if their role is local resource protection rather than global leadership.

#Section 20Bulkheads and concurrency isolation

A slow clip service occupies every shared application worker. Score reads now fail even though the score database is healthy. A bulkhead gives a dependency or workload its own bounded resource allocation so its failure cannot consume everything available to unrelated work.

For an illustrative worker budget of 100 slots, allow at most 20 clip-service calls in flight. If all clip calls stall, 80 slots remain available for other work. Separate connection pools can provide a similar boundary. The exact division must reflect measured costs, priorities, and the behavior under overload.

A STALLED CLIP POOL CANNOT CONSUME THE RESERVED SLOTSA STALLED CLIP POOL CANNOT CONSUME THE RESERVED SLOTSclip maximum: 20 slotsother work: 80 slots20 + 80 = 100 slots in the total resource budgetthe boundary reserves capacity for work that must remain useful
Figure 11.24Illustrative slots. Isolation preserves capacity only if other shared resources are also bounded.

A shared database, CPU, memory allocator, or logging path can still defeat this partition. Trace all constrained resources, not just thread count. Bulkheads may waste idle reserved capacity, while shared pools use resources more flexibly. A bounded borrowing policy can trade utilization for isolation, but it needs an explicit failure test.