29. Failures, Retries, Circuit Breakers
Practice: five levels
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Which pattern? One line each.
- The payment gateway is down; checkout should stop waiting on it immediately.
- Slow recommendations are using up all of checkout's threads.
- One request in 200 to inventory fails randomly and succeeds on a second try.
- A dependency sometimes hangs for minutes.
- Reviews are down; the book page should still load.
B. True or false? Explain in one line.
- Two database replicas on the same rack remove the single point of failure.
- Retries always improve availability.
- A circuit breaker should count 404 responses as failures.
- A readiness check should fail if an optional dependency is down.
- An untested backup is as good as a tested one.
Level 2: Calculate
- How many minutes of downtime per 30-day month does 99.95% allow? Per year for 99.99%?
- Checkout needs payment (99.9%), inventory (99.9%) and itself (99.95%). What's checkout's availability?
- Two independent load balancers, each 99.5%, either able to do the job. Combined availability?
- Three layers each make up to 3 attempts. How many calls can one user action cause at the bottom layer?
- A service handles 1,000 requests a second; recommendations take 5 s during an incident and get 100 requests a second. How many threads would they occupy?
- Async replication lags 2 s; the store takes 50 orders a second. Roughly how many orders could be lost in a regional failure?
Level 3: Build
In Python:
downtime_minutes(availability, days),serial(*parts)andparallel(a, copies).percentile(samples, p)and use it to pick a timeout from simulated latencies.simulate(...)showing a retry storm after a short outage, and recovery with a retry budget.- A
CircuitBreakerwith closed, open and half-open states. - A bulkhead check: one shared pool vs per-dependency pools when one dependency slows down.
Level 4: Debug
Late one night, real customers couldn't check out for 30 seconds at a time, repeatedly. Logs show a search-engine crawler requesting thousands of non-existent book IDs, which the catalogue answers with 404. Checkout calls the catalogue to confirm prices. The breaker:
- Explain how a crawler's 404s blocked real customers.
- Fix it, and name two other improvements.
Level 5: Design
Make the bookstore's checkout resilient. It calls: payment gateway (critical, external, sometimes slow), inventory (critical, internal), pricing (critical, internal), recommendations (optional), and sends a confirmation email (optional). Peak: 1,000 checkouts a minute during the Big Book Sale. Target: 99.95% availability for checkout.
Deliver:
- Timeouts and retries for each dependency, with reasoning.
- Where circuit breakers and bulkheads go.
- What degrades, and how, when each dependency fails.
- How the system avoids retry storms during a payment gateway outage.
- Health checks, failover and a DR plan (RPO and RTO) for the order database.