Key takeaways
- ✓When every booking did everything, the four servers answered 47.4% of 59,693 buyers in time during the minute of sale: a full booking held a worker for about 33 ms, mostly waiting for the recommendations service, so the other 52.6% got a fast HTTP 503, meaning busy, try later.
- ✓Skipping the two optional steps (the recommendations call and the seats-sold count) whenever four bookings were waiting answered 100% of buyers in time at 995 bookings a second, with 55% of the answers served without the extras, and all 50,000 seats sold.
- ✓Skipping the extras never double-booked a seat: 0 seats were sold twice in either run, because each seat is one atomic update (a single database change that either completes or does not) and a booking that is skipped or refused never reaches the seat count.
- ✓Autoscaling changed nothing against a 10-second spike at 1,000 requests a second: 5,911 of 9,828 requests failed with the autoscaler on, and the same 5,911 with it off, because a new server took 30 seconds to start and the first one served 5 seconds after the spike ended.
- ✓A slow dependency is worse than a dead one: with recommendations answering in two seconds against a one-second timeout, no booking was answered in time for 25 seconds, while a circuit breaker that stopped calling it brought the answered share back to 98.5%, with a median answer of 1.8 ms.
- ✓Autoscaling can follow a slow climb: from 200 to 1,000 requests a second over five minutes, with servers added as the load rose, 214,119 requests had 0 failures, with the server count going from 2 to 6.
Four servers, one flash sale at a thousand bookings a second, and half the buyers were told "busy". Skipping two optional steps while the servers were busy told nobody "busy" and sold every seat. A slow service that the booking depended on then answered no buyer in time, until a circuit breaker stopped waiting for it. The last test asked whether an autoscaler, which adds servers when load rises, could catch a spike. It could not.
The examples come from Lab 17 (Overload, Rate Limits and Backpressure) of the system design labs: script 04_flash_sale.py for the sale and the slow service, and script 05_autoscale.py for autoscaling, with the breaker's code in breaker.py. Both scripts run on the same laptop setup as the earlier chapters, an Apple M4 Max with Docker Desktop and 16 virtual CPUs. The four servers, their database and a load simulator all ran in Docker. The simulated buyers wait one second for an answer and do not retry. Each configuration is one recorded run, and the README puts scripts 01 to 05 at about 40 minutes.
A booking has an essential part and extras
Each booking does three things. It books a seat, which is one quick update in the database. That is the part that matters. It also asks a recommendations service for a "you might also like" list, which takes about 30 ms, and it counts the seats sold, which is a cheap read.
The servers have four workers each, and a booking holds a worker until it is done. Each server lets at most 32 bookings wait. A booking that arrives when the line is full gets the fast 503 at once, instead of waiting for a worker that will not come in time.
Think of a restaurant kitchen on a rush night. Each worker is a cook making one dish at a time. The recommendations are the garnish. Degradation means holding the garnish when the line is long: the dish still goes out, just without the extra. A circuit breaker is the cook who stops phoning a supplier who never picks up. Autoscaling is calling in more cooks, who take a while to arrive.
The sale, doing everything
Doing everything, a full booking holds a worker for about 33 ms, and most of that is waiting for recommendations. Four servers with four workers each finish about 120 bookings a second per server, roughly 480 for the four. The sale offered 1,000 a second for a minute.
The lucky buyers waited too. A typical answer took 301 ms at the median: 32 bookings were queued ahead on a server, and four were served at once, so a buyer waited about eight rounds of 33 ms before their own booking began. At the end of the sale, 13,666 of the 50,000 seats were still unsold, while 31,415 buyers had been sent away.
Degradation, in code
The change is a few lines. When a booking arrives, the server looks at how many bookings are waiting. If there are four or more, it skips the recommendations call and the seats-sold count, and it still books the seat. The extras run only when the line is short.
Nothing is refused by this rule. A buyer whose booking was degraded still gets a seat, just without the garnish. The rule decides what work a booking does, not whether it is allowed to book. Refusal is a separate mechanism: the 503 comes from the queue being full, and it happens to bookings that arrive when the queue has no room.
Skipping the extras
With the same sale and the same four servers, the extras are skipped whenever four bookings are waiting. Without the recommendations call, a booking holds a worker for a few milliseconds of database work, not 33. The 55% of answers that came without the extras are the ones that kept the line short.
- Doing everything: 47.4% answered in time; 31,415 fast 503s; 36,334 of 50,000 seats sold.
- Skipping the extras when four wait: 100% answered in time; 0 fast 503s; all 50,000 seats sold.
The answers per second show the difference across the minute of sale. Doing everything stayed flat at about 471 a second. Skipping the extras held about 995 a second for the whole minute, because the same four servers could keep up with the five-times peak.
Every booking calls recommendations and counts the seats sold. A full booking holds a worker for about 33 ms, so the 16 workers across four servers run out of room, and the overflow is refused with a fast 503 ("busy, try later").
Nobody was double-booked in either run. Each seat is one atomic update, so two buyers cannot both win the same seat. A degraded or refused booking never reaches that update, so it cannot take a seat that someone else already has.
One honest detail from the run: the seats ran out before the sale ended (about 9,700 of the 59,693 answers were "sold out"). After that, buyers heard a quick "sold out" answer. Nobody timed out, and nobody heard "busy" while seats were left. In the lab, a "sold out" answer counts as a successful answer, because it came in time. The 100% figure therefore means every buyer got a timely answer, not that every buyer got a seat.
A slow dependency is worse than a dead one
Now a different problem. The booking service still gets 200 bookings a second, and from about 10 seconds into the run, the recommendations service takes two seconds to answer. The booking service waits at most one second for it. The buyers also wait one second. So each booking waits as long as its buyer does, and it can never answer in time.
Without a breaker, from then on, no booking was answered in time for 25 seconds. The reason is capacity. Sixteen workers in all, each stuck for a full second, can finish only about 16 bookings a second, and every one of them is too late. The rest got a fast 503. The recommendations service was optional, and it still took the whole service down.
A timeout is not a no. The seat is booked before the recommendations call, so a booking that waited out the timeout had already taken its seat for a buyer who had given up. In the run with the breaker off, 2,617 seats were sold but only 2,009 bookings got a recommendations answer back, so about 600 seats were taken for buyers who had already timed out. That figure is a subtraction from two counts in the results, not a count the lab made directly.
A fast refusal costs a request a few milliseconds. A slow one costs a worker for a second. That is why the slow case is the dangerous one, and why the lab's own question about a service that answers 503 at once is easier: a dead dependency is cheap, a slow one is expensive.
Circuit breakers: closed, open, half-open
A circuit breaker sits in front of a call and remembers how the call has been going. It has three states.
- Closed: calls go through, and each outcome is counted over the last 5 seconds. Timeouts count as failures. If at least 20 calls were made in that window and at least half failed, the breaker opens.
- Open: every call fails at once, without touching the dependency, for 3 seconds.
- Half-open: after the 3 seconds, the breaker lets three trial calls through. If all three succeed, it closes again and forgets the old window. If one fails, it opens again for another 3 seconds.
The same numbers are in breaker.py, which is written from scratch for the lab. Each booking server runs its own breaker, with a failure share of one half, at least 20 calls, a 5-second window, a 3-second open period and three probes.
Recommendations get slow: two seconds per answer, and the app gives up after one. Every booking waits the full second, so no booking answers its buyer in time, and the buyers give up after one second too.
Calls go through, and the outcomes (including timeouts) are counted over the last 5 seconds.
Opens when at least 20 calls were made and half of them failed.
Every call fails at once, without touching the service, for 3 seconds.
Then the breaker moves to half-open.
Three trial calls go through. If all three succeed, the breaker closes. If one fails, it opens again.
With the breaker on, the breaker took a few seconds to open. The results do not record the moment it opened, so the time it took (about five seconds) is an estimate. During those seconds, the window still held the good calls from before the slowdown. After it opened, bookings no longer waited for recommendations at all. The breaker's table shows 98.5% of the slow-phase bookings answered in time, at a median of 1.8 ms and a 99th percentile of 6.3 ms. That is faster than before the slowdown, when a booking took about 33 ms.
Some bookings still timed out: 72 in the slow phase. The results do not say which calls they were. Most likely they are the three trial calls each server makes every few seconds, plus requests caught in the seconds before the breaker opened. The lab's README attributes them to the seconds before it opened, and the probes are the other likely source.
Why autoscaling alone did not fix the spike
The autoscaling test used a simpler request, 20 ms of work, so each server handles about 200 a second. The servers started with two, and the autoscaler's rule was to keep the average load per server near three requests waiting or in progress. Every two seconds it averaged its last three readings, and it asked for enough servers to bring the average back to three. A new server took 30 seconds to start.
The first test was a spike: 10 seconds at 1,000 a second, which is more than two servers can handle (about 400 a second). The autoscaler asked for six servers at 30.9 seconds. Those servers started answering at 61.9 and 64.1 seconds, more than 20 seconds after the spike had ended. The first extra server that answered at all did so at 44.6 seconds, 5 seconds after the spike ended.
- Spike, autoscaler on: 5,911 of 9,828 requests failed (60.1%).
- Spike, autoscaler off (two servers): 5,911 of 9,828 failed (60.1%).
Two servers were handling 400 a second, so 600 of every 1,000 requests were refused. The autoscaler saw the problem and reacted, but the answer came after the problem had gone. Capacity for a spike has to be there before the spike, or at least already on its way.
The second test was a ramp: from 200 to 1,000 a second over five minutes. The autoscaler added servers while the load was still rising slowly, and each new server came online about 30 seconds after it was asked for. Across 214,119 requests, none failed, and the server count went from two to six. Autoscaling follows ramps. It does not catch spikes.
One earlier run is worth knowing about, because its result looked better. Its base load was set exactly at the autoscaler's target, so a few noisy readings started two extra servers before the spike arrived. The spike then found them ready: 38.3% of requests failed with the autoscaler against 60.7% without. The lab's README calls this an artefact of the base load, not an effect of the autoscaler, and the later run with the base load at half the target shows the same 60.1% either way.
The workflow and the results
The sale and the breaker. Run 04_flash_sale.py (about six minutes). Here p50 is the median answer time, and p99 is the time that 99 answers in 100 beat. It runs the sale twice, once with degradation off and once with it on, each time at 200 a second, then 1,000 a second for 60 seconds, then 200 a second again. It then runs the slow-service test twice, once with the breaker off and once with it on, at 200 a second for 40 seconds with recommendations slowed from about 10 seconds on. The script checks each run for double-booked seats.
| Degradation | Answered in time (sale) | Booked per second | p50 | p99 | Answers without the extras | Fast 503s | Seats sold | Double-booked |
|---|---|---|---|---|---|---|---|---|
| Off (doing everything) | 47.4% | 471 | 301 ms | 316 ms | 0% | 31,415 | 36,334 of 50,000 | 0 |
| On (skip the extras when four wait) | 100% | 995 | 36 ms | 67 ms | 55% | 0 | 50,000 of 50,000 | 0 |
Before the sale, both runs answered 100% of bookings in time at about 198 a second, with a median of 33 ms.
| Breaker | Answered in time (slow phase) | p50 | p99 | Fast 503s | Timeouts | Recommendations calls skipped | Breaker state at the end |
|---|---|---|---|---|---|---|---|
| Off | 0.0% | none | none | 4,514 | 395 | 0 | closed (never opens) |
| On | 98.5% | 1.8 ms | 6.3 ms | 2 | 72 | 4,956 | open, on all four servers |
Before the slowdown, both breaker runs answered 99.4% of bookings in time, with 8 timeouts each.
Autoscaling. Run 05_autoscale.py (about 12 minutes). It runs the spike with the autoscaler on and off, and the ramp with the autoscaler on.
| Run | Requests failed before the spike | During the spike (10 s at 1,000 a second) | After the spike | When the servers answered |
|---|---|---|---|---|
| Spike, autoscaler on | 12 of 5,956 | 5,911 of 9,828 (60.1%) | 0 | first extra at 44.6 s; four at 61.9 s; six at 64.1 s |
| Spike, autoscaler off | 23 of 5,956 | 5,911 of 9,828 (60.1%) | 0 | none extra |
| Ramp, autoscaler on (200 to 1,000 a second over 5 minutes) | 0 of 3,986 | 0 of 214,119 over the whole ramp | 0 | from two servers to six |
The numbers in the tables come from data/results/04_flash_sale.json, 05_autoscale.json and, for the earlier run, 05_autoscale_at_target.json. The timeline in the autoscaling file records the server count and the desired count about every two seconds, which is where the 30.9-second request and the 44.6-second answer come from. The README says the autoscaler "saw the spike at 36 s". The timeline shows the first request for six servers at 30.9 seconds, so the chapter uses that figure.
Caveats
- One run per configuration. Every row is a single recording on one laptop, with a load average of 5.4 during the sale run. Treat the counts as a single observation, not a measured average.
- "Answered in time" counts sold-out answers. A fast "sold out" is a success in the lab's counting, so the 100% in the degradation test means every buyer got a timely answer, not a seat.
- The seat count in the breaker run is not used. The results file's seats-sold count for the breaker-on run (7,129) is larger than the number of requests in that run, so the chapter does not use it. The 608 figure is a subtraction from two counts, and the lab does not state it.
- The breaker's timing is estimated. The results file does not record when the breaker opened, and the probe explanation for the 72 timeouts is a likely reading of the lab's README, not a timestamp in the results.
- Buyers do not retry. The simulated buyers wait one second and give up. Real buyers who see "busy" and try again would add load, which is a different test (the lab's previous episode covers retry storms).
- Each booking server has its own breaker. The lab does not share breaker state across servers, so each server learns about the slow service separately. The README lists shared breakers as an exercise.
- Not covered: a breaker's tuning (the 20 calls, 5 seconds, 3-second open period and three probes were chosen for this lab), adaptive limits that follow latency, last-in-first-out serving under overload, scaling on a leading signal such as the queue in front of the servers, and multi-region traffic. The lab lists several of these as exercises.
The checklist
- Find the part that must always run. Keep the essential work (here, the seat update) on every request, and make the optional work the first thing to drop.
- Check that shedding and degradation cannot change correctness. The seat count is one atomic update, and anything skipped or refused never reaches it.
- Put a breaker in front of any dependency that can get slow. A fast failure costs a request milliseconds. A slow one costs a worker for its full timeout, so a breaker that opens on slow calls protects the whole service.
- Give up before your caller does. The breaker's open period and the timeout should be set so that the service stops waiting before the buyer gives up.
- Plan capacity for spikes before they come. An autoscaler follows a ramp, and its new servers take time to start. A spike needs headroom, shedding, or a scale-up scheduled before a known event.
Quiz
1.Four servers each run four workers, and a full booking holds a worker for about 33 ms. Roughly how many bookings a second can the four servers finish?
2.A circuit breaker is open. What happens to the next call to the recommendations service?
3.The autoscaler saw the spike and asked for more servers, yet the spike's failures were the same with and without it. What explains that?
Check your understanding
Try each question before opening its answer.
Why did skipping the extras answer every buyer in time, when the same four servers answered only 47.4% when they did everything?
Because a full booking held a worker for about 33 ms, mostly waiting for the recommendations call, so four servers finished only about 480 bookings a second. Without the recommendations call and the seats-sold count, a booking held a worker for a few milliseconds, and the same four servers kept up with 995 bookings a second, with the 55% of answers that came without the extras.
Skipping the extras kept the seat count right in both runs. What is it about the seat update that makes that true?
Each seat is one atomic update: a single database change that either completes or does not, so two buyers cannot both take the same seat. A booking that is skipped or refused never reaches that update, so it cannot take a seat that someone else already has. In both runs, 0 seats were sold twice.
A circuit breaker is open. What happens to a booking that arrives, and why is that better than the recommendations call timing out?
The booking fails at once, without calling the recommendations service, and the buyer gets a fast answer. A timeout would have held a worker for a full second first. The breaker stays open for 3 seconds, then lets three trial calls through to see whether the service has recovered.
The autoscaler saw the spike and asked for six servers. Why did the spike fail the same number of requests with it on and off?
Because a new server takes 30 seconds to start. The autoscaler asked at 30.9 seconds, the first extra server answered at 44.6 seconds, and the spike had already ended about 5 seconds earlier. Those extra servers answered only after the requests that needed them had failed, so the same 5,911 requests failed either way.
The breaker on the slow service answered 98.5% of bookings in time, against 0% without it. Why is a timeout worse than a fast refusal here?
A timeout holds a worker for the full second the service takes to fail, so capacity drops to about 16 bookings a second across the four servers, and every booking waits. A fast refusal costs a few milliseconds. The breaker turns the slow failures into fast ones, so the workers stay free for the bookings that can still be answered.
Next up: Chapter 43, failover and lost writes. This chapter's server slowed down and stayed up. The next one is a server that dies, and the question is which writes were acknowledged and then lost.