Reliability: SLOs, Retries & Circuit Breakers

System Design · Production in the Cloud · Advanced · 15 min

Keep systems up when parts fail, and measure 'up' properly.

A fuse box for software. Calls flow while the fuse is in (closed). After too many failures in a row the fuse trips (open) and calls fail instantly instead of hammering a sick service. After a pause, one test call is let through (half-open) to see if it is safe to switch back on.

The idea in everyday words

A house's electrical fuse: when one appliance shorts, the fuse trips so the whole house doesn't burn. A circuit breaker in software does the same: when a service keeps failing, stop calling it for a while instead of piling on.

Measuring reliability

  • SLI: what you measure (e.g. % of requests < 300ms and not 5xx)
  • SLO: your target (99.9%)
  • SLA: the promise to customers, with penalties
for nines in ["99", "99.9", "99.99", "99.999"]:
    down = (100 - float(nines)) / 100 * 365 * 24 * 60
    print(nines + "%", "uptime =", round(down), "minutes of downtime per year")

Retries with exponential backoff + jitter

Retry transient failures, but wait longer each time (1s, 2s, 4s…) and add randomness so 10,000 clients don't retry at the same instant (a thundering herd).

import random
base = 0.1
for attempt in range(5):
    wait = min(5, base * 2 ** attempt) * random.uniform(0.5, 1.0)
    print("attempt", attempt + 1, "wait", round(wait, 2), "s")

Circuit breaker states

  • Closed: calls flow normally; count failures.
  • Open: too many failures → fail fast for a cool-down period.
  • Half-open: let a test call through; success → closed, failure → open again.

Also: timeouts on every call, bulkheads (separate pools), graceful degradation (show cached results), redundancy across availability zones/regions.

Go deeper (senior / staff)

Correlated failure is what really takes systems down. Single servers dying is easy. The hard cases are a bad deploy to every AZ at once, a config push, a shared dependency failing for everyone (DNS, identity, one database), or an expired certificate. Defences: deploy one AZ or one cell at a time with automatic rollback on alarms, and design for static stability: the system keeps working in its degraded state without new control-plane calls. For example, keep enough capacity running in each AZ instead of counting on Auto Scaling to launch instances during an AZ event.

Retry budgets. Retries multiply across layers: 3 retries (4 attempts) at each of 3 layers is up to 64 attempts at the bottom. Retry at one layer, use a retry budget (the AWS SDKs' standard retry mode uses a token bucket for this) and pass deadlines down.

Multi-region choices. Active-passive (pilot light, warm standby) is cheaper, but failover is a risky event of its own, so rehearse it. Active-active recovers fastest but forces you to handle write conflicts (DynamoDB global tables resolve them with last-writer-wins) and doubles always-on cost. Prefer data-plane failover, such as Route 53 health checks or Application Recovery Controller routing controls, over runbooks that call control-plane APIs mid-incident.

Cells and shuffle sharding. Split customers into independent cells, each a full stack with its own data, so a bug or a poison request takes out one cell, not everyone. Shuffle sharding gives each customer a random subset of workers, so two customers rarely share all of theirs.

Cost reasoning. Each extra nine costs very roughly ten times the effort. Tie the SLO to real user impact, ship freely while the error budget is healthy, and freeze risky changes when it is spent.

Step by step

  1. Closed:

    payments OK… failure 1, 2, 3

  2. Open:

    threshold hit → fail fast for 30s, show 'try later'

  3. Half-open:

    after 30s, 1 test call → success

  4. Closed:

    traffic flows again, counters reset

Code example

class Breaker:
    def __init__(self, threshold):
        self.threshold, self.fails, self.state = threshold, 0, "closed"
    def record(self, ok):
        if ok:
            self.fails, self.state = 0, "closed"
        else:
            self.fails += 1
            if self.fails >= self.threshold:
                self.state = "open"
        return self.state

b = Breaker(3)
for ok in [True, False, False, False, True]:
    print("call ok" if ok else "call failed", "->", b.record(ok))

Interview tips

  • Talk about failure modes for every component you draw.

  • Know 'nines' maths quickly.

  • Mention multi-region active-active vs active-passive trade-offs.

Challenge: Simulate a Circuit Breaker

Given a threshold and a list of call results (True/true = success), return the breaker state after each call. Consecutive failures reaching the threshold → "open". Any success → "closed" and resets the counter.

Example: breaker(2, [True, False, False, True]) → ["closed", "closed", "open", "closed"]

Solve it in the browser with tests, hints and an AI coach.

Practice problems

More in Production in the Cloud

Previous: Event-Driven Architecture: SQS, SNS, EventBridge & Kinesis · Next: Observability: Logs, Metrics & Traces