Sub topic 16
Testing behavior beyond capacity
This chapter assumes you know the earlier material: bounded queues, load shedding, in-flight limits, and 503 with Retry-After. Those mechanisms only count if they work when the system is overloaded. This chapter shows how to test that.
Why ordinary load tests miss overload
Most load tests stop at the expected peak. An overload test deliberately goes past capacity and checks what the service does there. Two mistakes make tests look better than reality:
- Closed-loop load. If each virtual user waits for a response before sending the next request, a slow server automatically receives less traffic. Real clients keep arriving, so the test hides the queue growth you want to see.
- Averages. A mean latency of 80 ms can hide a p99 of several seconds. Look at percentiles, and measure them only for requests that succeeded.
Use open-loop load: arrivals follow a schedule that does not depend on how fast the server answers.
What to measure
| Metric | Healthy behavior beyond capacity |
|---|---|
| Goodput (successful responses per second) | Stays near maximum capacity |
| p99 latency of successes | Stays roughly flat |
| Rejection rate | Rises with offered load |
| Memory and queue depth | Stay bounded |
Goodput is the key number. A service with no protection shows goodput falling as load rises, because it spends its time on requests whose callers have already given up.
A runnable experiment
This script applies the in-flight limit from the earlier chapter to a simulated service. Each request takes 0.1 s and 50 can run at once, so capacity is about 500 requests per second. The test offers 1000.
import asyncio
import time
MAX_IN_FLIGHT = 50
in_flight = 0
async def handle() -> int:
global in_flight
if in_flight >= MAX_IN_FLIGHT:
return 503
in_flight += 1
try:
await asyncio.sleep(0.1) # simulated work
return 200
finally:
in_flight -= 1
async def timed(results: list) -> None:
start = time.monotonic()
status = await handle()
results.append((status, time.monotonic() - start))
async def run(rate: int, seconds: float) -> None:
results: list = []
tasks = []
start = time.monotonic()
sent = 0
while time.monotonic() - start < seconds:
due = int((time.monotonic() - start) * rate)
while sent < due: # open loop: arrivals follow the clock
tasks.append(asyncio.create_task(timed(results)))
sent += 1
await asyncio.sleep(0.001)
await asyncio.gather(*tasks)
ok = sorted(t for s, t in results if s == 200)
shed = sum(1 for s, _ in results if s == 503)
p99 = ok[int(len(ok) * 0.99) - 1]
print(f"offered={rate}/s goodput={len(ok) / seconds:.0f}/s "
f"shed={shed} p99={p99 * 1000:.0f} ms")
for rate in (200, 500, 1000):
asyncio.run(run(rate, 3))Expect goodput to level off near 500 per second at the higher rates, shed counts to rise, and p99 to stay close to 100 ms. If you remove the limit, every request is accepted, so the service never rejects anything and latency is no longer protected.
Test the recovery too
After the spike ends, queue depth, latency, and error rate should return to baseline within seconds. Slow recovery points to a retry storm or a backlog of stale work. Add clients that retry, with and without jitter and a retry budget, and compare.
Common errors
- Generating load from the same machine that runs the service, so the load generator becomes the bottleneck.
- Testing only a step to 2x capacity. Also try 10x, and a sudden spike versus a slow ramp.
- Counting rejected requests as failures. During overload they are the intended behavior.
Exercise
A load test with 100 virtual users shows stable latency even though you push the server hard. Each user sends a new request only after the previous one returns. Why can't this test show overload behavior, and what should you change?
Show answer
It is closed-loop. When the server slows down, the users send fewer requests, so offered load never exceeds capacity and no queue builds up. Switch to open-loop load that sends requests at a fixed rate regardless of response times, and then raise that rate beyond capacity.