Chapter 18: Fault Tolerance — Failover, Circuit Breakers, Retries

Retry with Exponential Backoff

// Don't hammer a failing service. Back off exponentially:
attempt 1: wait 100ms
attempt 2: wait 200ms
attempt 3: wait 400ms
attempt 4: wait 800ms + random jitter
// Jitter prevents thundering herd (all clients retrying simultaneously)

Circuit Breaker Pattern

States: CLOSED ──(failures exceed threshold)──► OPEN ▲ │ │ (timeout) │ ▼ └──(success)──── HALF-OPEN ◄────────────┘ CLOSED: requests flow normally, count failures OPEN: reject immediately (don't call failing service), return fallback HALF-OPEN: allow one test request through to check if service recovered

Bulkhead Pattern

Isolate failures. Don't let one slow dependency consume all your threads/connections.

// Without bulkhead: Service B is slow → all threads waiting → entire system hangs
// With bulkhead: allocate max 10 threads to Service B calls
//   If all 10 busy → fail fast for B, other services unaffected

Timeout

Always set timeouts. A missing timeout = a thread/connection held forever if remote service hangs.

Failover Strategies

StrategyHowTrade-off
Active-PassiveStandby takes over on failureSimple but wastes standby resources
Active-ActiveAll nodes serve trafficEfficient but complex (data sync)
Multi-regionEntire region fails over to anotherSurvives datacenter failure, complex
Key Takeaways