Skip to content

Failure scenarios

Once you have drawn a design, a good interviewer starts to break it. “What happens if Redis fails?” “What happens when traffic goes up 10 times?” “How would you handle the loss of a region?” “Where is your bottleneck?” “How do you handle duplicate messages?” They are not looking for a list of tools. They want to see whether you can think about a system that is not working, which is what a system spends part of its life doing.

The failure stage of the Arena is where you practise this, and the questions have the same shape each time. This lesson gives you a method for answering them, and then applies it to each of the five.

Mental model. A what-if answer has four parts, in this order: the failure, the arithmetic, the defence and the cost. Skip the arithmetic and the defence is a guess. Skip the cost and the defence sounds free, which no defence is.

Move Say Example for a failed cache
Name the failure What exactly stops, and what keeps working The cache stops answering. The application and the database are fine.
Do the arithmetic Where the work goes, and whether the component that receives it can take it Every read now reaches the database, which was sized for the misses.
State the defence One change that removes the cause, or limits the damage More database capacity, or a way to serve reads from somewhere else.
State the cost What the defence takes: money, complexity, a risk it creates Capacity that sits idle while the cache is healthy.

You will meet the same four moves in the failure engineering module, where each failure is worked in full. Here you make them short enough to say in two minutes.

The failure is that the cache stops answering. Nothing is wrong with the database or the application, and that is the point: a component that was only supposed to make things faster has become a load-bearing part of the design.

The arithmetic is one formula. The database sees only the misses, so with a hit ratio of h it receives 1 − h of the reads. When the cache is gone it receives all of them: 1 ÷ (1 − h) times more. At h = 0.95 that is 20 times more.

Check it in the Builder. This is the cached design of the last lesson: a load balancer, four API instances, a cache with a 0.95 hit ratio and a database with two instances, at 5,000 requests per second. Press Run at 5,000 RPS and read the database: it is asked for 250 requests per second out of 1,600 and is 16% busy. Nothing is overloaded.

Now open Advanced settings on the cache, set Hit ratio (0 to 1) to 0 and run again. A cache that answers nothing sends every read on. The database is asked for all 5,000 requests per second, which is 20 times more than before and 313% of its 1,600. It is now the bottleneck, and the design carries about 32% of the target load: 1,600 out of 5,000, so about 68% of the requests fail. The API is still at 16%, and adding API instances would change nothing, because the work that is failing is not in the API.

Try other values of the hit ratio to see the whole curve. The database receives 5,000 × (1 − h).

Hit ratio Database is asked for Against its 1,600
0.99 50 requests per second 3%
0.95 250 16%
0.9 500 31%
0.5 2,500 156%
0 5,000 313%

The row to say aloud is the last: a cache that makes a database 20 times smaller makes the system 20 times more fragile in one specific way, and it must be possible to say what happens in that case.

Now the defence and its cost. There is no setting that makes this free, and the order in which you name the options shows what you understand.

  • More database capacity, enough for all the reads: 5,000 ÷ 800 is 6.25, so 7 instances instead of 2, which would run at 89% busy. It removes the cause. The cost is money, and capacity that sits idle while the cache is healthy.
  • Fail closed: the application returns an error at once and never asks the database to do the cache’s job. It protects the database and gives up the reads that needed the cache.
  • Shed load: accept only as many reads as the database can serve and refuse the rest quickly. You decide who is refused, and every refusal is a failed request.
  • A standby cache, or serving stale data: the database stays shielded. The costs are more machines, and answers that are a little old.

Add one more fact an interviewer will like: when the cache comes back it is empty, so the database faces the full load again until the cache refills. The lesson on when the cache fails runs this as a lab, with the connect timeout that the Builder does not model.

What happens when traffic increases 10 times?

Section titled “What happens when traffic increases 10 times?”

The question is about growth, but the answer is about order: something gives way first, and fixing it moves the problem to the next component. Your job is to find the first and the next one by arithmetic.

Take the same design at 10 times its load, 50,000 requests per second. Each component’s capacity is its instances times the requests it works on at once, divided by the time each takes. The four API instances do 4 × 16 ÷ 0.002, which is 32,000. The cache does 80,000 and the database 1,600. The first limit is the API, at 50,000 ÷ 32,000, which is 156%.

Run it and the engine agrees: the API is the bottleneck, the design carries about 64% of the 50,000 (32,000 requests per second), and about 36% of the requests fail, which is 1 − 32,000 ÷ 50,000. Now fix only that. Set the Instances of the API to 8, which makes its capacity 64,000, and run again.

  • The API is now at 78% of its capacity. The bottleneck has moved to the database, which is asked for 5% of 50,000, which is 2,500, against its 1,600: 156% again.
  • Only 5% of the requests reach the database, and a little over a third of those fail, so about 1.8% of all requests fail. A first fix that removes most of the errors still leaves the next limit standing.
  • Give the database 4 instances, 3,200 in all, and nothing is overloaded. The design now carries up to 1.28 times the 50,000, which is 64,000 requests per second. The alternative is to raise the hit ratio to 0.99, which cuts the database’s load to 500 and is a better lever if the data allows it, and it has its own cost.

That is the whole answer in five sentences: the API gives way first, at 156%; I add API capacity; the database is next, at 156%; I add capacity or hit ratio there; and then I tell you the next limit I would look for.

The failure lab below shows the same pattern in a service with a load balancer, an API, a cache and a database. Predict first which component gives way, then read the chain.

Add 10x traffic

The shop serves 900 requests a second. A campaign, a news story or a bug in a client multiplies the traffic by ten: 9,000 requests a second against an API that can serve 1,200. You add the traffic and watch where the system gives way. Then choose how to answer it, and notice what gives way next.

The failure lab needs JavaScript. The healthy system is shown below.

1. Break it

The healthy system is shown first. When you press the button the failure strikes, and the run follows the 30 seconds after it.

What the failure does: Traffic jumps to ten times what the system normally serves.

0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIBusy75%CacheHealthy1%DatabaseHealthy20%
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIBusy75%CacheHealthy1%DatabaseHealthy20%

Each dot stands for many requests. The numbers show the real rate.

No bottleneck: every component has headroom.

0 ms

Cause and effect

Each step leads to the next. A step that is changing right now is marked.

Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%90090000 ms0 ms0 ms
Load balancerHealthy<1%90090000.03 ms0.15 ms0.23 ms
APIBusy75%900900014 ms60 ms93 ms
CacheHealthy1%81081000.14 ms0.6 ms0.92 ms
DatabaseHealthy20%25225205.5 ms24 ms37 ms
Traffic
900 req/s
Successful
900 req/s
Error rate
0%
Mean latency
24 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • The system starts healthy at 900 requests a second. Three API instances can serve 1,200 a second and the database about 1,250 operations a second. Nine in ten requests read through the cache, which answers four in five of those reads; the rest, and every write, go to the database.
  • A failure strikes at one instant and nothing repairs it during the run: no restart, no autoscaling, no operator. The load balancer notices a dead instance at once.
  • The instances of a component are identical, and each request is independent of the others.

Simplifications

  • The traffic jumps to ten times at one instant and stays there. Real traffic ramps up, and autoscaling, which is not modelled, reacts to a ramp. The model shows what happens if nothing reacts.
  • Every request costs the same. In practice a flood often arrives as a different mix of requests, such as many more reads than writes.
  • A retry is a fresh, independent attempt with the same chance of failing as the last one. How long a real client library waits between attempts (backoff, jitter), and any retry budget or circuit breaker, are not simulated: attempts are counted, not scheduled.
  • A client gives up when its first timeout runs out, so the latency shown stops at what a client would wait: the timeout of the first hop, times its attempts. After that the request counts as a failure.
  • The cache's hit ratio is constant. Warming, stampedes and hot keys have their own labs.
  • Nothing here models garbage collection, connection pools on the client, DNS, TLS handshakes, CPU throttling, noisy neighbours or cold caches. A real system fails in more ways, and in messier ones, than this model does.
  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.

Parameter values are illustrative. They are not benchmarks of any real product.

The arithmetic of the lab is the one you use in the interview. If 9,000 requests per second arrive at a service that can do 1,200, at most 1,200 ÷ 9,000, which is 13%, can succeed, and 87% cannot, whatever you configure. Only more capacity raises the number. Load shedding and backpressure decide which requests fail and how long the survivors wait. The overload lesson works the lab in full, and the autoscaling lesson explains why a policy that reacts to load is not ready at the moment the traffic arrives.

This is the same question without the failure. The method is to compute the capacity of every component on the path and name the smallest, with the number. In the plain design of the last lesson, the four API instances can do 32,000 requests per second and the single database can do 4 ÷ 0.005, which is 800. At 500,000 requests per second the database is asked for 32,000, which is all that the API can pass on, and it can serve 800: the database is the bottleneck, and the design carries about 0.2% of the load.

Two habits make this answer strong. Give the capacity of the component with its unit, and say how you got it: instances, the requests each works on at once, and the time each takes. And say what you would do next and what limit that would reveal, because the bottleneck you fix is never the last one.

The failure is that a whole region stops answering. The arithmetic has two parts: where its users go, and whether the regions left can carry them. The second part is a one-line formula.

With N regions, each at utilisation u, losing one sends its load to the others. They now carry u × N ÷ (N − 1), assuming the load is spread evenly. A layout survives the loss only if that is 100% or less, which means each region must run at no more than (N − 1) ÷ N.

Regions Each at After one is lost Highest safe utilisation
2 60% 120%, overloaded 50%
3 60% 90% 67%
3 75% 113%, overloaded 67%
4 75% 100%, no margin 75%

Read the table the way an interviewer will. Two regions mean running each at half its capacity to survive losing the other, which is paying for twice the capacity you use. Three regions bring that to two thirds. More regions lower the cost of the spare capacity and raise the cost of everything else: replication, routing and operations.

The defence has three parts, matching the three things a lost region takes. Capacity: the regions left must hold the load, per the table. Time: the users of the failed region must be sent elsewhere, which depends on health checks and on how long clients cache the old answer. Data: the writes not yet copied out of the failed region are at risk, and synchronous copies save the data at the price of latency on every write. State the cost of each. The lesson on regional failure runs these numbers in a lab, and AWS’s disaster-recovery whitepaper adds a point worth repeating in an interview: a failover should use only data-plane operations, so that it does not depend on the region that failed.

The failure is that a message arrives twice. With a queue that promises at-least-once delivery, it will, for a plain reason: the consumer processes a message, the acknowledgement is lost, and the queue sends the message again. The messaging module explains the moment of the acknowledgement. You cannot prevent this without risking lost messages, so the design has to accept it.

The arithmetic is a rate. If 10,000 messages per second are consumed and 0.1% of the acknowledgements are lost, 10 messages per second are processed twice, which is 864,000 a day. These two figures are assumptions chosen for the example, and they are not measurements. The conclusion does not depend on them: any rate above zero means that, over a day, a duplicate will arrive, and a design that is correct only without duplicates is not correct.

The defence is to make a repeat harmless. The message carries an identifier, such as an idempotency key, and the consumer records the identifiers it has handled and skips a repeat. The cost has three parts: a store of identifiers that must be consulted for every message, a time to live after which an old identifier is forgotten, and the care to record the identifier in the same transaction as the effect, or a crash between the two brings the duplicate back. The idempotency module covers all three, and the duplicate messages lesson works the lost acknowledgement.

What happens if you answer with the fix and no arithmetic?

Section titled “What happens if you answer with the fix and no arithmetic?”

Take the first question again. “What happens if Redis fails?” A candidate says: “I would add more application servers and a second Redis.” Both are fine as parts of a design, and neither answers the question asked.

The arithmetic shows why. The application servers were at 16% before the failure and are at 16% after it: in the Builder, setting the API to 8 instances changes nothing for the database, which is still asked for 5,000 requests per second against 1,600. The extra servers cost money and fix nothing. A second Redis is a better answer, but it raises more questions: is it a replica that takes over, how long does that take, and what does the database see while it does? The candidate has not said, and the interviewer will ask.

The answer with the arithmetic takes the same time to say: the database sees 20 times the reads, which is 313% of its capacity, so most requests fail; the cheapest defence is to fail closed and keep the database up, the one that removes the cause is database capacity for all the reads, and each costs this much. Neither answer is long. Only one of them shows that you know which component is in trouble.

Before you move on, take a different failure through the four moves. A flash sale sells 100 units to 50,000 shoppers in the same minute. What fails, what is the arithmetic, what is the defence, and what does it cost?

Interview question

Writing and reviewing an answer needs JavaScript.

The interviewer asks

A shop sells 100 units of one product in a flash sale, and 50,000 shoppers press ‘buy’ in the same minute. How do you make sure you never sell more than 100, and what happens to a shopper who reserves an item and then does not pay?

Assume the stock is one record in an inventory service, a payment takes about 300 ms at the payment provider, and a reservation can be held for 10 minutes.

Write your answer first. The strong answer unlocks once you have written a real attempt.

0 of 60 characters written to unlock the strong answer

  • A what-if answer has four moves: name the failure, do the arithmetic, state the defence, state the cost. Skipping the arithmetic is the usual mistake.
  • A failed cache multiplies the database’s reads by 1 ÷ (1 − h). At a hit ratio of 0.95 that is 20 times: the database goes from 250 to 5,000 requests per second against 1,600, so the design carries about 32% of the load. More API servers do nothing, because the API is at 16%.
  • At 10 times the load (50,000 requests per second), the API gives way first at 156%. Doubling it to 8 instances moves the limit to the database, at 156% again, and 4 database instances remove it. Fix one limit, then name the next.
  • If 9,000 requests per second arrive at a service that does 1,200, at most 13% succeed. Capacity raises the number, and shedding only chooses who fails.
  • After losing one of N regions, the others carry u × N ÷ (N − 1). Three regions must each run at 67% or less to survive the loss, and two at 50% or less.
  • Duplicates are certain with at-least-once delivery. Make the repeat harmless with an identifier, a record of the identifiers seen, and a limit on how long they are kept.

Next: Senior follow-ups, defending a choice and saying “it depends” well.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.