AlgoViz

Resilience in distributed systems

medium 28 min

A 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

Our system50 a secondUsersShop200 threadsOrdershealthyReviews0.2 s
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

Our systemUsersLoad balancerZone A33%Zone B33%Zone C33%
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

    critical

    One slow answer holds a thread for minutes. At 50 a second they are all gone in seconds.

    Wrong
    requests.get(url)
    Right
    requests.get(url, timeout=(0.3, 0.5))   # connect, read
  • Retrying a write without a key

    critical

    The first attempt may have succeeded and only the answer was lost. The second charges again.

    Wrong
    for _ in range(3): pay(order)
    Right
    key = order.payment_key           # the same every timefor _ in range(3): pay(order, idempotency_key=key)
  • Retrying a refusal

    major

    If the breaker is open or the bulkhead is full, trying again only fights your own protection.

    Wrong
    except Exception: retry()
    Right
    except Refused: return fallback()except Temporary: retry_with_backoff()
  • A half-open breaker that never reopens

    major

    If the failure count is cleared on half-open and the threshold stays 5, one failed test does not open it.

    Wrong
    state, failures = HALF_OPEN, 0if failures >= 5: state = OPEN
    Right
    if 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?