05. Reliability: cheatsheets

4 lessons, one page each when printed.

System Design · 05. Reliability · 10 min · Intermediate

Redundancy & Failover

Keep spare copies, and switch to them fast when a part dies.

In short

Remove every single point of failure: two of each part, health checks to find the dead one, and copies in separate zones (and, if worth it, regions). One chain at 97.03% became 100% on paper.

Failover costs seconds, and copies can fail together, so test failovers on purpose before a real failure does it for you.

Key ideas

  • Do not ask "will it fail?" Ask "what happens to users when it does?" (step 1)
  • A good health check tests what matters (can it reach its database?), not only that the process is running. (step 5)
  • Choose the target from the business need. Each extra nine costs far more than the one before. (step 9)

Good to know

  • Active-passive is simple and safe for data that must have one writer, such as a database. (step 3)
  • Spreading over zones is cheap with cloud providers and is the usual first step for high uptime. (step 7)

Watch out

  • Look for SPOFs by asking of each box: if this one dies right now, does the whole system stop? (step 2)
  • Two servers at 70% load each cannot survive a failure: the one left would need 140%. Keep each below 50%, or add a third. (step 4)
  • Faster checks find failures sooner but remove healthy servers by mistake. Tune the speed against false alarms. (step 6)
  • Most systems stop at two zones. Add regions only when an outage would cost more than running a second region. (step 8)
copyUsersLoad balancerLoad balancer 2App serverApp server 2DatabaseStandby databasestandby

Key numbers

Uptime
99.9996%
Downtime / year
2.0 min

Terms

Single point of failure
A part with no spare, whose failure stops the whole system.
Redundancy
Keeping more than one copy of a part so one can fail without an outage.
Active-passive
One copy serves traffic while a standby waits to be promoted if it dies.
Active-active
All copies serve traffic at the same time, and the survivors take over when one fails.
Health check
A regular probe, such as GET /health, that tells a load balancer whether a server can take traffic.
Failover
Switching traffic from a failed part to its spare, which takes seconds.
Availability zone
A separate building in a region with its own power and network.
Region
A group of zones in one area; a second region protects against a whole-region outage.
Nines of uptime
Uptime written as 99.9%, 99.99% and so on; each extra nine cuts allowed downtime by ten times.

System Design · 05. Reliability · 10 min · Intermediate

Rate Limiting

Say no to some requests so everyone else can get a yes.

In short

A rate limiter turns "everyone shares the server" into "everyone gets a fair share". Fixed windows are simple but have an edge burst; sliding windows fix it; a token bucket allows bursts; a leaky bucket smooths them.

Put it at the gateway, count in a shared store, and answer rejected requests with 429 and Retry-After.

Key ideas

  • A rate limiter protects a shared service from any one caller, by accident or on purpose. (step 1)
  • Two numbers set the behaviour: the refill rate is the long-run speed, the bucket size is the biggest burst. Many APIs use this. (step 5)

Good to know

  • In Redis this is two commands: INCR the key, and EXPIRE it after 60 seconds. (step 2)
  • The counter version is a close estimate that uses almost no memory. Most big systems use it. (step 4)
  • Limits can stack: a loose limit for the whole API, and a tight one for login (say 5 tries a minute) to slow password guessing. (step 7)
  • A good client reads Retry-After and waits. A bad one retries at once, which is where the next lesson, retries and backoff, comes in. (step 9)

Watch out

  • Any fixed window can be gamed at its edge. If bursts matter, use one of the next three ideas. (step 3)
  • Token bucket allows bursts; leaky bucket smooths them. Use leaky when the service behind it cannot take spikes, at the cost of added waiting. (step 6)
  • Most public APIs fail open, because blocking every user is worse than a short period without limits. Login limits often fail closed. (step 8)
ClientNormal usersRate limiter50/s per keyApp server

Key numbers

Allowed
450/s
Rejected
1,950/s

Terms

Rate limiting
Capping how many requests a client may send in a period, so one caller cannot overload a shared service.
Fixed window
A counter per client that resets at the start of every minute (or other period).
Boundary burst
A client sends a full limit at the end of one window and again at the start of the next, doubling the limit.
Sliding window
Counting requests over the last N seconds from now, so there is no reset edge to exploit.
Token bucket
A bucket that refills at a steady rate; each request spends a token, and a full bucket allows a burst.
Leaky bucket
A queue that drains at a fixed speed, turning bursts into a smooth stream and dropping the overflow.
API gateway
The front door that checks limits before requests reach the app.
Shared counter
A limit counter kept in one store, such as Redis, so all gateways count together.
429 Too Many Requests
The HTTP status for a rejected request, sent with Retry-After to say when to try again.

System Design · 05. Reliability · 10 min · Intermediate

Timeouts & Retries

Give up on slow calls, try again the safe way, and stop a failure from spreading.

In short

Always set a timeout. Retry only what is safe and likely to work, with growing, random waits, and an idempotency key for anything that changes data.

Add a circuit breaker to stop calling what is down, and bulkheads so one slow service cannot use every thread. Failures then stay small instead of spreading.

Key ideas

  • A slow dependency is often worse than a dead one: a dead one fails fast, a slow one holds your threads. (step 1)
  • Backoff spreads one client's tries in time. Jitter spreads different clients apart. Use both. (step 6)
  • Reads and PUTs are safe to repeat. Creating things (POST) needs a key. Keep saved keys for a day or so. (step 7)

Good to know

  • Retry only errors that may pass by themselves (timeouts, 503). Never retry 400 or 404: the answer will not change. (step 3)
  • Cap the wait (for example at 30 s) and the number of tries, and give up when the user's own deadline has passed. (step 5)
  • Pair it with a fallback, such as a popular-items list, so users see something useful instead of an error. (step 8)
  • Timeouts, retries with backoff, breakers and bulkheads work as a set: each covers a gap the others leave. (step 9)

Watch out

  • Set the timeout from real data, a little above the slowest normal reply (the p99), and give the whole request a deadline too. (step 2)
  • Retry at one layer only, and give a retry a budget (for example, at most 10% extra calls). (step 4)
timeout 1 sUsersApp serverCircuit breakerclosedRecommendations

Key numbers

Calls in flight
1
Error %
0%

Terms

Timeout
The longest a caller waits for a reply before it gives up.
Retry
Sending a failed call again, in the hope that the failure was only a blip.
Retry storm
Retries at several layers multiply the load on a struggling service until it cannot recover.
Exponential backoff
Waiting longer after each failed try, such as 1, 2, 4, 8 seconds.
Jitter
Random variation added to a wait so many clients do not retry at the same moment.
Idempotency key
A unique id sent with a request so the server can recognise a repeat and not do the work twice.
Circuit breaker
A guard that stops calls to a failing service for a while, then tests it with one call.
Bulkhead
A separate pool of threads or connections per dependency, so one slow service cannot use them all.
Fallback
A cheaper answer, such as a default list, used when a call fails.

System Design · 05. Reliability · 8 min · Beginner

Observability

See inside a running system with logs, metrics and traces.

In short

Logs tell what happened, metrics tell that something changed and where, and traces show the path of one request. Dashboards and the four golden signals show health at a glance.

An SLO turns "fast enough" into a number, the error budget says how much failure is allowed, and alerts fire on user pain so the team finds problems before the users do.

Key ideas

  • Observability means you can ask new questions about a running system and answer them from the data it already produces. (step 1)
  • Latency, traffic and errors show what users feel. Saturation shows what is about to hurt them. (step 6)

Good to know

  • Pass the trace id along in a header (W3C traceparent), so every service adds its spans to the same trace. (step 4)
  • Keep one overview dashboard for the whole service and link each panel to a more detailed one. (step 5)
  • 100% is the wrong target: it costs far too much and users cannot tell the difference after about 99.99%. (step 7)

Watch out

  • Never log passwords, card numbers or tokens. Logs are copied around and kept for months. (step 2)
  • Do not put unbounded values such as user_id in metric labels. Each value makes a new series, and a million of them break the store. (step 3)
  • Too many alerts and people start to ignore them. If a page needs no action, delete it or turn it into a ticket. (step 8)
UsersGatewayOrdersLog storePaymentsDatabaseMetrics storeDashboard

Key numbers

Time to find cause
2 min
Error budget left
98%

Terms

Observability
How well you can understand what a running system is doing from the data it produces.
Structured log
A log line written as fields (often JSON), including a request id, so it can be searched.
Metric
A number sampled over time, such as requests per second, cheap to store and graph.
Trace and span
A trace follows one request through the services; each timed step is a span.
Percentile (p99)
The value below which that share of requests falls; p99 is the time that 99 of 100 requests beat.
Golden signals
Latency, traffic, errors and saturation: the four things to watch for every service.
SLI
A measured number of how well the service is doing for users, such as the share of fast, successful requests.
SLO
The target for an SLI over a period, such as 99.9% over 30 days.
Error budget
The amount of failure the SLO allows; when it is spent, reliability work comes first.
Burn-rate alert
An alert that fires when the error budget is being used up too quickly.