Reliability and Failure
How availability is measured, how redundancy and failover work, how to stop one failure from spreading, and what interviewers ask when a component dies.
On this page
Reliability is the work of keeping a system useful while parts of it fail. It combines redundancy, so that a failed component has a replacement, with protective patterns such as timeouts, circuit breakers and load shedding, which stop a failing component from dragging down its callers. Availability, the share of time that the system does its job, is the number used to set the target.
Without these measures, a single slow optional service makes its callers wait, the waiting callers use up their threads, and the callers of those callers start to fail as well.
Where redundancy and protections sit in a system
request and replyasynchronous message
Availability and the nines
Availability is usually written as a percentage and spoken as a number of nines. A figure of 99.9% is called "three nines".
| Availability | Downtime per 30-day month | Downtime per year |
|---|---|---|
| 99% | 7.2 hours | 3.65 days |
| 99.9% | 43 minutes | 8.8 hours |
| 99.99% | 4.3 minutes | 53 minutes |
| 99.999% | 26 seconds | 5.3 minutes |
Each additional nine cuts downtime tenfold and costs more. At 99.99% the monthly budget is about four minutes, which is less time than it takes a person to be paged, log in and fix a problem, so recovery has to be automatic.
Components in series
When a request needs every component in a chain, the availability of the chain is the product of the availabilities. Three services that are each 99.9% available give 0.9993, about 99.7%, or roughly 130 minutes of downtime a month. Every dependency added to the critical path lowers availability.
Components in parallel
When any one of several copies can serve the request, the system is down only if all copies are down at once. Two copies that are each 99% available fail together 0.01 × 0.01 = 0.0001 of the time, so the pair is 99.99% available. This assumes independent failures. Two copies in one rack, or running the same bad release, do not fail independently.
Redundancy and failover
Redundancy means having a second component ready to do the work of one that fails. It can be used in two ways.
Active-passive
One component serves all traffic while a standby copy waits, kept up to date by replication. When the active one fails, a monitor notices, promotes the standby and redirects traffic to it. It is the normal design for a database with a single leader.
Downtime is the detection time plus the promotion time. For example, a check every 5 seconds that requires three misses takes 15 seconds to detect a failure. Checking more aggressively risks replacing a leader that is only slow while it is still running, and then two nodes both believe they are the leader, which is called split brain (see consensus and leader election). With asynchronous replication, writes that had not reached the standby are lost at failover.
Active-active
All copies serve traffic at the same time. Stateless servers behind a load balancer work this way, and when one fails the others simply take its share. There is no promotion step, so there is almost no failover time.
The remaining copies must be able to carry the extra load. For stateful systems, active-active means more than one node accepts writes, so concurrent writes to one record can conflict and need a resolution rule or a single owner per record.
Cascading failure
A cascading failure is one in which a failure in a single component increases the load or the waiting time of other components until they fail too. It usually begins in one of three ways:
- A dependency becomes slow rather than failing outright. Each waiting call holds a thread.
- Callers retry failed requests, so a struggling service receives several times its normal load.
- A server fails and its traffic overloads the remaining servers, which fail in turn.
The first case is the most common. In the trace below, a product page calls a catalog service and an optional recommendations service. Recommendations becomes slow, and the page has no protection. The later steps add the protections described below.
- views/s
- 1000
- recs latency
- 50 ms
1Normally the page serves 1,000 views a second, calling catalog (20 ms) and recommendations (50 ms) in parallel.
- views/s
- 1000
- recs latency
- 5 s
2Recommendations slows to 5 seconds. Calls in flight equal rate times wait, 1,000 × 5 = 5,000, but the page servers have only 2,000 threads.
- views/s
- 1000
- recs latency
- 5 s
3Every thread is stuck, so the whole page fails although catalog is healthy. This is the cascade.
- views/s
- 1000
- in flight to recs
- 300
4A 300 ms timeout makes calls give up early, so calls in flight drop to 1,000 × 0.3 = 300.
- views/s
- 1000
- in flight to recs
- 300
5A bulkhead gives recommendations its own 400 threads. If they fill, further calls fail at once and the other 1,600 threads stay free.
- views/s
- 1000
- in flight to recs
- 0
6A circuit breaker sees more than half of recent calls fail and opens, so the page stops calling recommendations.
- views/s
- 1000
- in flight to recs
- 0
7With graceful degradation, the page shows a cached list of popular items, shoppers can still buy, and recommendations recovers without load.
Protections against failure
Timeouts
A timeout limits how long a caller waits for a response before it gives up. Every network call needs one, because a dependency that hangs otherwise holds the caller's thread forever. Set it a little above the dependency's 99th percentile latency. A timeout that is too long does not protect anything, and one that is too short turns ordinary slow responses into errors and extra retries.
Retries
A retry repeats a failed request, which hides short failures such as a dropped connection or a server that restarted. Retries are safe only for operations that can be repeated, so a payment needs an idempotency key first (see idempotency and retries). Wait between attempts with exponential backoff, doubling the delay each time, and add random jitter so that clients do not retry together.
Retries multiply load when they are done at every layer. If a web tier, a service and a database client each make three attempts, one user request can produce 3 × 3 × 3 = 27 queries at the database. Retry at one layer only, cap retries at a small fraction of normal traffic (a retry budget), and do not retry errors that will fail again, such as a 400.
Circuit breakers
A circuit breaker wraps calls to a dependency and counts failures. While it is closed, calls pass through. When the failure rate in a recent window passes a threshold, the breaker opens, and calls fail immediately or use a fallback without reaching the dependency. After a cool-down it lets a few trial calls through (half-open); if they succeed it closes, and if not it opens again.
A breaker protects the caller, because failing at once costs nothing, and the dependency, which receives no traffic while it recovers.
Bulkheads
A bulkhead gives each dependency its own limited pool of threads or connections, the way a ship's compartments contain a leak. If recommendations becomes slow, it can use up only its own pool. The cost is that pools must be sized, and idle capacity in one pool cannot be borrowed by another.
Graceful degradation
Graceful degradation means that when a part fails, the system keeps working with reduced features instead of failing completely. A product page can show popular items when recommendations are down, and a feed can load without ads. Decide for each dependency whether it is critical or optional.
Load shedding
Load shedding means that a server that is over capacity rejects some requests immediately, usually with a 503 response, instead of accepting all of them and timing out on every one. Shed the least valuable work first, such as browsing before checkout. Shedding responds to the server's own overload, unlike rate limiting, which gives each client a fair share.
Backpressure
Backpressure is the way a slow consumer tells its producer to slow down, so that work does not pile up without limit. The usual form is a bounded queue: when it is full, the producer is blocked or receives an error. Without a bound, the queue grows until memory runs out. A consumer that fetches only as many messages as it can process is a common example (see message queues and streams).
Zones, regions, RPO and RTO
A zone is an isolated group of data centers with its own power and network, and a region is a group of zones in one area. Copies in separate zones survive a building failure, and synchronous replication between zones is practical because they are close. Copies in separate regions survive losing a whole region, at higher cost.
Two objectives decide how much protection is needed. The recovery point objective (RPO) is how much recent data you can afford to lose, measured as a length of time. The recovery time objective (RTO) is how long the service may stay down before it must be working again. Synchronous replication gives an RPO of zero but makes every write wait for the second copy, while asynchronous replication loses whatever had not yet been copied.
One region, several zones
Copies in three zones survive a zone failure with automatic failover. A region-wide outage still takes the system down, which is acceptable for most systems.
Active-passive regions
A second region holds an asynchronous copy and takes over if the first is lost. Failover takes minutes, and the RPO is the replication lag. Use it when a region outage must cost only minutes of downtime and a few seconds of lost writes are acceptable.
Active-active regions
Every region serves live traffic, so users reach a nearby region and there is no untested standby. Writes to one record in two regions can conflict, so records usually have a home region. It suits users spread across continents.
Failure modes of failover and health checks
Failover that sends all load to the survivors
When one zone fails, its traffic moves to the others. If two zones each run at 70% of capacity, the survivor would need 140%. Keep enough headroom to lose the largest failure domain, which for three zones means running each at about 66% or less.
Health checks that are too shallow or too deep
A check that only tests whether the process runs misses a server that cannot reach its database. A check that queries the database makes every server unhealthy when the database slows down, and the balancer removes all of them. Most balancers can be set to keep sending traffic to the servers anyway when all of them fail the check, so that a shared dependency problem does not remove the whole fleet.
In the interview
Interviewers usually point at a box in your design and ask what happens when it dies. For each component, say what the user sees, how the failure is detected and what takes over, and state the target as downtime: "99.9% allows about 43 minutes a month, so failover must be automatic". Use active-passive for single-writer state and active-active for stateless tiers.
Questions to expect:
- "What happens if the database leader dies?" A monitor detects the missed health checks within seconds and promotes the standby. With asynchronous replication, writes not yet copied are lost, so say which RPO the data allows.
- "What happens if the load balancer dies?" A single balancer would be a single point of failure, so there are at least two, either as an active-passive pair that takes over a shared address or as several balancers that share traffic through DNS or anycast. See load balancing.
- "What happens if the cache dies?" Every read falls through to the database, which may not be able to carry that load. The protections are a replicated cache cluster and load shedding while the cache refills. See caching.
- "What happens if one app server dies?" The load balancer stops sending it traffic within seconds, and the other servers take its share. Requests in flight on it fail and are retried, which is safe only if they are idempotent.
- "A dependency is slow. What happens to your service?" Without protection, calls pile up and use every thread. With a timeout, a bulkhead and a circuit breaker, calls to it fail quickly and the rest keeps working, and optional dependencies get a fallback.
- "What is the difference between load shedding and backpressure?" Shedding rejects excess requests immediately, so the client sees an error. Backpressure makes the producer slow down, so the work is delayed instead of refused. Shedding suits request traffic from many clients, and backpressure suits a pipeline with a bounded queue between stages.
- "How would you survive a region failure?" First ask whether the requirement exists, because multi-region is expensive. Active-passive recovers in minutes with a small data loss, and active-active gives local latency but needs conflict handling.
Your feed service calls an ads service. The ads service starts taking 10 seconds to respond. What happens?
The feed calls ads with a 200 ms timeout and from its own small connection pool, so slow ads cannot use the threads that load the feed. If more than half of the ad calls fail within a 10-second window, a circuit breaker opens and the feed is served without ads until trial calls succeed again.
A circuit breaker is open. What does the caller do with a new request?
Two zones each run at 70% of capacity behind a load balancer. One zone fails. What happens?
What to practise next
Failures can only be handled once they are detected, so the next lesson is observability. To see failures component by component, work through Design a Key-Value Store.