ealeng

Episode 41 of 55 · Overload

The slowdown ended and the service stayed down: retry storms, and saying no early

A service with a capacity of 800 requests a second answered only 39% in time at exactly that load, once nothing limited the line. A 10-second slowdown ended, and with clients retrying at once, not one request succeeded in the 45 seconds after it. Backoff with jitter didn't help either: 3.34 attempts per request, as before. A retry budget capped retries at 10% and recovered after 27 seconds. A server that refused work early, with the same worst-case clients, recovered at once with 92% answered in time.

Lab 17 · Overload, Rate Limits and Backpressure10 min read
Video transcript10:43

0:00Cold open

0:01

My service handles about eight hundred requests a second, on paper.

0:07

I sent it eight hundred a second, on average. In the second half of the test, only thirty-nine percent got an answer in time.

0:16

Then I slowed it down for ten seconds, with clients that retry. The slowdown ended. In the forty-five seconds I kept watching, not one request succeeded.

0:28

How does a system stay down after the problem is gone?

0:34Intro

0:36

Hands-on system design, part forty-one.

0:39

Today: saying no early, called load shedding, and how retries can keep a system down.

0:46Meet Lucía

0:48

I’m Lucía, a platform engineer. This is my service: four copies, called instances, behind a load balancer. Each has four workers.

0:57

A request holds a worker for twenty milliseconds. So one worker serves fifty a second, and sixteen workers, eight hundred.

1:06

My test client sends on schedule, whether earlier requests were answered or not. And it gives up after a set time.

1:15

What counts is goodput: requests answered OK, in time. An answer that arrives after the client gave up doesn’t count.

1:25

Think of a restaurant with sixteen cooks. Diners who wait too long leave. Shedding is a host at the door: full, come back later.

1:3501 · What you need

1:36

To run it yourself, you need Docker, for the service, the load balancer and the test client. Everything runs on one laptop.

1:46

The offline tests need no Docker, and pass in a few seconds. No cloud, nothing to pay for.

1:5402 · The test

1:56

First test, no retries; the client gives up after one second. I raised the load step by step, from half the capacity to three times.

2:06

With no limit on waiting, at half the capacity, every request was answered.

2:13

At exactly the capacity, eight hundred a second: only thirty-nine percent in time.

2:20

From one and a half times on: zero. Almost every request was too late.

2:26

Why? The line grows faster than the cooks can clear it. Soon every request has waited over a second, and its client is gone. But the server still cooks for it. After the three-times step, the servers worked about thirty-seven more seconds, for nobody.

2:43

And why at the capacity? With a long line, the servers likely managed only about seven hundred fifty a second. So eight hundred was too much, and the line grew every second.

2:5703 · At most eight

2:58

Now one change. Each instance lets at most eight requests wait. The next one gets an immediate “busy, try later”: an error called a five-oh-three.

3:10

At three times the capacity, what do you think goodput did?

3:16

It stayed flat: seven hundred seventy-seven a second. With a short line, the servers don’t fall behind.

3:25

Eight in line, four cooks: at most forty milliseconds of waiting. Everyone let in was served in time, the slowest one in a hundred in sixty-five milliseconds. The rest hear no at once.

3:3904 · In code

3:40

In code, it’s one check when a request arrives: if eight are already waiting, refuse it with “queue full”.

3:48

Otherwise it joins the line. A fast no costs the server almost nothing. A yes that arrives too late costs it a worker, for nobody.

3:5905 · Other limits

4:01

Another idea: let everyone wait. But when a request reaches a cook after its client gave up, throw it away instead of cooking it.

4:10

I expected that to help. It barely did: four hundred a second at the capacity, twenty-five at one and a half times.

4:19

The likely reason: the line is first come, first served. So the request reaching a cook is the one that waited longest, just before its client gives up. It gets cooked, and the answer is often a moment too late: the ones that made it took typically just under a second.

4:38

A limit on waiting time, two hundred milliseconds, works like the limit of eight: the same goodput, with answers in about two hundred twenty milliseconds.

4:5006 · Priorities

4:51

Not every request matters equally. At twice the capacity, I marked half of them critical and half sheddable: fine to refuse. With no limit, both halves got nothing.

5:04

With priority shedding, sheddable requests are refused once four wait, and critical ones may still join, up to sixteen. Ninety-five percent of critical requests got through: critical alone was eight hundred a second, a bit over what the servers can do.

5:21

The sheddable half was refused almost entirely. That’s the point: refuse what can wait, so what matters gets through.

5:3107 · A slowdown

5:33

Second test: a normal load, five hundred sixty a second, seventy percent of the capacity. This client is less patient: it gives up after half a second, and may retry up to three times.

5:47

From fifteen to twenty-five seconds, every request takes three times as long, like a slow disk. Capacity drops to about two hundred sixty-seven a second. Then it’s back to normal.

6:00

Four ways for the client to retry: not at all; at once; after a random wait that grows, called backoff with jitter; and a retry budget, where retries may be at most ten percent of recent requests.

6:1508 · No retries

6:16

No retries first. Green is goodput, orange is attempts sent.

6:22

During the slowdown, goodput fell to zero, not to two hundred sixty-seven. With no limit on waiting, every request waited longer than half a second.

6:34

About three thousand extra requests piled up: five hundred sixty in, two hundred sixty-seven out, for ten seconds. With at most two hundred forty a second to spare, catching up takes at least twelve seconds. It took fifteen.

6:5109 · Retry at once

6:52

Now the clients retry at once. As soon as clients start giving up, attempts jump to over two thousand a second.

7:01

The slowdown ended at twenty-five seconds. Goodput stayed at zero, to the end.

7:08

From the slowdown on, almost every request used all four tries: over the whole run, three point three four, one try each before the slowdown, nearly four after. Four tries each is over two thousand two hundred a second: nearly three times what the servers can do.

7:26

The slowdown is over, but the retries alone keep the servers overloaded. That’s a metastable failure: stuck down, with no cause left.

7:3810 · Backoff and jitter

7:39

The textbook fix is backoff with jitter: before each retry, wait a random time, up to a limit that doubles each try. Do you think it recovered?

7:51

No. Same as retrying at once: from the slowdown on, almost every request used all four tries, and goodput stayed at zero.

8:01

Jitter spreads retries out in time. It doesn’t make fewer of them. And with waits of at most a fifth of a second, they still landed on servers that were already full.

8:1311 · A retry budget

8:15

A retry budget does make fewer: one point zero six attempts per request.

8:21

It recovered, twenty-seven seconds after the slowdown ended. But that’s slower than no retries at all.

8:30

The orange spikes likely show why. The budget counts over the last ten seconds, so it spent its whole allowance at once, then again ten seconds later. Four bursts of about six hundred extra tries: at least ten more seconds of catching up.

8:4712 · In code

8:48

In code: the retries allowed are ten percent of the requests in the window, plus a small floor.

8:56

Over budget, the client gives up instead. In an outage, retries are capped at about ten percent over any ten seconds, however many requests fail.

9:0713 · The server defends itself

9:09

Last: the worst client, retrying at once, against servers that defend themselves. Each lets at most eight wait, and throws away requests whose client already left.

9:21

During the slowdown, attempts rose, but they were refused fast. Goodput stayed at about two hundred sixty a second: all the slowed servers could do.

9:32

And it was back to normal at once. Ninety-two percent got an OK: only the slowdown’s excess was lost. With no limit: twenty-two, just the first fifteen seconds.

9:4514 · Side by side

9:46

All five side by side. Only the server’s own limit came back at once, even with the worst client.

9:55Back to Lucía

9:57

So, three lessons.

10:00

Without a limit on waiting, a server works for clients who already left, and goodput collapses as soon as the load passes what the servers can really do.

10:10

Retries multiply the load exactly when there’s none to spare. Jitter spreads them out; only a budget makes fewer.

10:19

And the server’s own limit works, whatever the clients do.

10:24

Next time: a flash sale, a circuit breaker, and an autoscaler against a sudden spike.

10:31

For today: say no early.Lucía: Say no early.

10:35Summary

10:36

Limit the line, budget the retries, and say no early.

Key takeaways

  • ✓With no limit on waiting, a service at exactly its capacity of 800 requests a second answered 39% of requests in time, and from 1.5 times that load on, almost none: it kept working on answers for clients that had already given up, and after the 3× step it needed 37 seconds to clear the backlog.
  • ✓Load shedding with a queue of at most 8 waiting requests per instance kept goodput (requests answered in time) flat at 756 to 777 a second from the capacity up to three times it, with a p99 (the slowest one in a hundred) of 65 ms for the requests it let in, and refused the rest at once with a 503, the HTTP status for 'busy, try later'.
  • ✓Dropping requests whose client had already left barely helped, with 400 a second answered at 1× and 25 at 1.5×, because a first-come, first-served queue hands a worker the request that has waited longest, just before its deadline; a 200 ms limit on waiting did what the queue bound did, with answers in about 220 ms.
  • ✓Priorities decide whom to refuse: with 1,600 requests a second, half marked critical, priority shedding answered 95% of the critical ones and 0.1% of the sheddable ones, while with no limit both groups got nothing.
  • ✓Retrying at once after a 10-second slowdown that ended at 25 seconds sent 3.34 attempts per request, about 2,300 attempts a second against 777 a second of real capacity, and goodput stayed at zero to the end of the 70-second run: a metastable failure, kept down by its own retries after the cause was gone.
  • ✓Backoff with jitter, waits of up to 50, 100 and 200 ms before the three retries, still averaged 3.34 attempts per request and never recovered (22% answered OK overall), because jitter spreads retries out in time without making fewer of them.
  • ✓A retry budget that allows retries for at most 10% of recent requests cut the average to 1.06 attempts and recovered 27 seconds after the slowdown ended, while a server that allowed at most 8 waiting and dropped expired requests recovered at once against the same worst-case client, with 92% answered OK.

Lab 17 of the system design labs sent a service 560 requests a second, then slowed the service down for 10 seconds. When the slowdown ended, clients that retried at once got nothing back: no request succeeded in the 45 seconds that followed. Backoff with jitter didn't fix it. A retry budget did, slowly, and a server that refused work early recovered at once. This chapter takes the three failures apart: why a line makes overload worse, how retries multiply the load, and why saying no early is the fix that works whatever the clients do.

The examples come from Lab 17 (Overload, Rate Limits and Backpressure), the scripts 02_shedding.py (the load sweep) and 03_retry_storm.py (the slowdown and the retry policies). The policy code is in shedding.py (the server's queue and limits) and retry.py (the client's retry rules). The service has four instances, each with four workers, and each request holds a worker for 20 ms. So one worker does 50 requests a second, and the four instances together 800. The lab ran on the same laptop as the earlier chapters, an Apple M4 Max with Docker Desktop, and each configuration is one recorded run.

Goodput, and why a line makes it fall

Throughput counts every request the server finishes. Goodput counts only the ones answered OK within the client's timeout. The difference is the whole story here: a server can be busy every second and deliver nothing a user can use.

A service with a fixed number of workers serves a fixed rate, here 800 a second. Past that rate, requests wait. With no limit on the waiting line, every request waits behind every earlier one. Once the wait is longer than the client's timeout (one second in the sweep), the client has gone, but the server still cooks the answer. The work is done for nobody.

Think of a restaurant with sixteen cooks, where diners who wait too long walk out. If the kitchen keeps cooking every order, including the ones whose diners have left, the cooks are busy and the paying diners still wait.

Retries multiply the load

A timeout makes the client send again. That is the whole mechanism, and it turns a slow answer into more load. Under overload, the retry arrives at a server that is already full, so it also waits, and it too may time out and be sent again.

With three retries allowed, one failed request can become four attempts. If most requests fail, the load can reach four times the normal rate, at exactly the moment the servers have no spare room. The trigger can end, and the load stays above capacity, because the failures it creates are what keep the load high. That is a metastable failure: the system is stuck in a bad state that its own load sustains, after the cause has gone.

The incident review in that case would blame the slowdown, which ended at 25 seconds. The outage did not end, so the cause is the loop: retries with no budget, against a queue with no bound. The next 10-second slowdown would do it again.

Jitter spreads retries out, and a budget makes fewer

Backoff with jitter waits a random time before each retry, up to a limit that doubles with each try. In the lab, the limit was 50 ms before the first retry, 100 ms before the second and 200 ms before the third. Jitter stops the clients from retrying in lockstep. It does not reduce how many retries there are, so a failing server still sees up to four attempts per request.

A retry budget limits retries as a share of the traffic. The lab's budget allowed retries for at most 10% of the requests in a 10-second window, plus a small floor, so in an outage the extra load is capped at about 10%. Once the budget is spent, the client gives up on that request rather than retrying it.

Load shedding: refuse work early

Load shedding means refusing work at the door, at once, when the server can't serve it in time. A refused request costs almost nothing. An accepted request that is answered too late costs a worker, and gives nothing back to the user.

The lab's server limits its line. With at most 8 requests waiting per instance, the next request gets an immediate 503 instead of joining the line. The line never grows, so the wait is bounded: at most 8 waiting over 4 workers, about 40 ms, which is the arithmetic of the setup. The requests it let in were served in time.

The other limits the lab tried were weaker. Dropping a request whose client has already gone sounds right, but a first-come, first-served queue hands a worker the request that has waited longest, which is the one just before its deadline. It gets cooked and answered a moment too late. A 200 ms limit on waiting works like the bound: the queue it allows is about 200 ms long, and goodput is the same.

Priorities decide whom to refuse. When half of the traffic is marked critical and half sheddable, shedding the sheddable half first keeps the critical half flowing.

Try it · Goodput past capacityrecorded sweep, 02_shedding.py

Goodput per second is the share of requests answered within the client's 1 s timeout. Bars are scaled to the 800/s capacity; each row is one 20 s step, measured over its last 10 s.

400/s (0.5×)
396/s · 100% · p99 26 ms
800/s (1.0×)
313/s · 39% · p99 998 ms
1,200/s (1.5×)
0/s · 0%
1,600/s (2.0×)
0/s · 0%
2,400/s (3.0×)
0/s · 0%

Priorities, 1,600/s with half critical and half sheddable

No shedding: critical OK
0%
No shedding: goodput
0/s
Shedding: critical OK
95%
Shedding: sheddable OK
0%

With shedding, sheddable requests are refused once 4 wait and critical ones may queue up to 16. Critical p99 110 ms. Goodput 760/s.

The workflow and the results

The load sweep. 02_shedding.py raises the load in 20-second steps, from 0.5× to 3× the capacity of 800 a second, with a 1-second client timeout and no retries. Goodput is measured over the last 10 seconds of each step. It takes about 8 minutes.

Offered per second No limit: answered in time Bounded queue (8): answered in time
400 (0.5×) 396 (100%) 396 (100%)
800 (1×) 313 (39%) 756 (94%)
1,200 (1.5×) 0 764 (63%)
1,600 (2×) 0 770 (48%)
2,400 (3×) 0 777 (33%)

Without a limit, goodput collapsed at exactly the capacity. After the 3× step, the backlog took 37 seconds to drain. With the bound, the 3× step turned away 16,114 requests in its last 10 seconds, each with a fast 503, and the p99 of the requests it served was 65 ms.

The retry storm. 03_retry_storm.py sends 560 requests a second for 70 seconds, with a 0.5-second client timeout and up to 3 retries. From 15 to 25 seconds every request takes three times as long, so the capacity falls to about 267 a second. It takes about 10 minutes.

Client retries Server Attempts per request Goodput, last 15 s Recovered after the slowdown OK overall
None No limit 1.00 557/s After 15 s 65%
Retry at once No limit 3.34 0 Never 22%
Backoff with jitter No limit 3.34 0 Never 22%
Retry budget (10%) No limit 1.06 557/s After 27 s 49%
Retry at once Bounded queue (8) and drop expired 1.27 557/s At once 92%
Try it · A 10 s slowdown, four retry policiesrecorded run, 03_retry_storm.py

Every timeout is sent again at once, up to 3 times. Attempts pass 2,000 a second and goodput never returns.

slowdown, 3× slower07001,4002,1002,800offered 560/s0 s20 s40 s60 s

goodput per second (answered in time)attempts per second (retries included)

Attempts per request
3.34
Goodput, last 15 s
0/s
Recovered
never
OK overall
22%

Recovered: never (0 for the last 15 s). Load 560/s against about 267/s during the slowdown and about 777/s normally. One run per policy.

Read the table in this order. Retry at once and backoff with jitter both sent about 2,250 to 2,300 attempts a second in the last 15 seconds, from clients that wanted 560, and both stayed at zero goodput. The budget cut the attempts to 1.06 a request and recovered, but its allowance came in bursts (about 1,100 attempts in each of four seconds, 16, 26, 36 and 46), and it took 27 seconds to come back, against 15 with no retries at all. The server's own limit was the best of the five: with the worst client, retries were refused fast rather than queued, so the queue never filled and goodput was back the moment the slowdown ended.

Caveats

  • One run per setting. Each policy and each sweep step is a single recording, so the percentages are not error bars. The lab does not repeat runs to show how much they vary.
  • The "capacity" is a stand-in. Each request holds a worker for a 20 ms sleep, not a real piece of work, so the top speed is set by the harness. The recorded goodput plateau was 756 to 777 a second, below the 800 on paper, so the sweep's 1× step was already slightly over what the service could do.
  • Waiting time is arithmetic. The "at most 40 ms" for 8 waiting over 4 workers is what the setup implies. The lab measured latency, not the queue length.
  • Only one client type with one set of numbers. Every client had a 0.5-second timeout and 3 retries. The retry policies were not tested across two tiers of services, where retries multiply once per tier, and the budget's 10% and 10-second window are one choice among many.
  • The priority test is one point. It was run at 1,600 a second, half critical, with one queue setting.
  • Not covered: circuit breakers, graceful degradation, autoscaling and the flash sale, which the next episodes take up; adaptive concurrency limits; a last-in, first-out queue; the lab's rate limiters; and any real network, TLS or multi-region failure.

The checklist

  1. Measure goodput, not throughput. A server busy with answers nobody is waiting for is overloaded, whatever its CPU says.
  2. Bound the line. A short queue with a fast 503 keeps the wait short and the rest of the traffic on time.
  3. Count retries in the load. Every retry is more load at the moment of the failure. Multiply attempts by the number of tries before you size the system.
  4. Use jitter to spread retries, and a budget to limit them. Jitter alone kept 3.34 attempts per request; a budget brought it to 1.06.
  5. Let the server refuse work. It works whatever the clients do, and the worst client was the case where it mattered most.
  6. Mark what can wait. Refuse sheddable traffic first, so critical traffic keeps flowing.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.After a 10-second slowdown ends, clients that retry at once keep getting zero answers in time. Why does the outage outlast the slowdown?

  2. 2.Backoff with jitter and a retry budget both change when retries are sent. Which one reduced the number of attempts per request?

  3. 3.Dropping requests whose client has already gone, instead of answering them, barely helped at capacity: 400 a second answered at 1×. What explains it?

Check your understanding

Try each question before opening its answer.

Why did the 10-second slowdown's outage last well past its end, with retries at once?

The retries themselves kept the servers overloaded. Each request was sent up to four times, so about 2,300 attempts a second reached servers that could do about 777 a second. The queue filled with requests that would time out anyway, so goodput stayed at zero, even though the servers were healthy again. The trigger had gone, and the loop it started had not.

Backoff with jitter spreads retries out in time. Why didn't it keep the load down, when the retries were up to 200 ms apart?

Jitter changes when retries happen, not how many there are. With three retries allowed, a request that keeps failing still produces four attempts, and the lab averaged 3.34 attempts per request with or without it. The waits were a fraction of a second against a sustained overload, so the retries still landed on full servers.

A retry budget spends its allowance in bursts. Why does that make recovery slower than no retries at all?

The budget counts retries over a 10-second window, so it spends its allowance all at once and then waits for the next window. In the lab, that gave about 1,100 attempts a second in each of four bursts, 10 seconds apart, and each burst kept the servers full for a while. Recovery came 27 seconds after the slowdown ended, against 15 with no retries.

Dropping requests whose client had already left barely helped at capacity. Why not?

In a first-come, first-served queue, the request that reaches a worker is the one that has waited longest, so it reaches the worker just before its client gives up. Dropping expired requests therefore removed very little, and the ones that were served were answered about a second after they were sent, too late. A bound on the queue keeps the wait short, so what gets served is still on time.

Why does the server's own limit work even when the clients are the worst they can be?

The limit does not depend on what the clients do. A request that finds 8 already waiting is refused at once, so it costs the server almost nothing, and the queue stays short. Even with immediate retries sending about 2,300 attempts a second, goodput was back the moment the slowdown ended, and 92% of all requests were answered OK, against 22% without the limit.

Next up: Chapter 42, circuit breakers and degrading gracefully: a flash sale with 5× the normal traffic, which page parts to switch off under load, and a breaker in front of a slow dependency.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.