Resilience in distributed systems
medium 28 minA resilient system keeps doing its most important work while parts of it fail, and returns to normal by itself.
See it run
One slow or broken part, and the five patterns that keep it a small problem.
Operations
Timeout against a cascade
A reviews service becomes slow. The shop needs it for nothing important, and still falls.
Step 1 of 5
Healthy
- Threads busy
- 10 of 200
- Requests failing
- 0%
50 requests a second, 0.2 seconds each in reviews: 10 threads busy.
Step 1: Healthy
# no timeout: 50 a second x 30 s = 1,500 threadsreviews = get("/reviews")# with one: 50 a second x 0.5 s = 25 threadstry: reviews = get("/reviews", timeout=0.5)except Timeout: reviews = None- Protects
- Your threads
- Costs
- Some wrong cuts
- Wait forever
- everything stops
Busy threads = requests a second x seconds each. Do this multiplication for every call.
In the real world
The same resilience doing real jobs. Pick one and step through it.
Three zones, sized so that two can carry everything.
Step 1 of 4
Three zones
- CPU of the working zones
- 60%
Each carries a third at 60% CPU.
Step 1: Three zones
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A call without a timeout
criticalOne slow answer holds a thread for minutes. At 50 a second they are all gone in seconds.
Wrongrequests.get(url)Rightrequests.get(url, timeout=(0.3, 0.5)) # connect, readRetrying a write without a key
criticalThe first attempt may have succeeded and only the answer was lost. The second charges again.
Wrongfor _ in range(3): pay(order)Rightkey = order.payment_key # the same every timefor _ in range(3): pay(order, idempotency_key=key)Retrying a refusal
majorIf the breaker is open or the bulkhead is full, trying again only fights your own protection.
Wrongexcept Exception: retry()Rightexcept Refused: return fallback()except Temporary: retry_with_backoff()A half-open breaker that never reopens
majorIf the failure count is cleared on half-open and the threshold stays 5, one failed test does not open it.
Wrongstate, failures = HALF_OPEN, 0if failures >= 5: state = OPENRightif state == HALF_OPEN or failures >= 5: state = OPEN
Interview tips
How to explain it
- Failures happen. Keep them small and short.
- Timeout, retry, breaker, bulkhead, fallback.
- Prove it with controlled failures.
Memory trick: TRIBE
- Timeout, Retry.
- Isolate, Breaker.
- Errors handled: the fallback.
Before you retry
- Is it temporary?
- Is a repeat safe?
- Is there time and budget left?
For each dependency
- How long do we wait, at most?
- How many calls at once, at most?
- What does the user get without it?