Sub topic 1
What overload is and why capacity is finite
Prerequisites
This chapter assumes you have the following background:
- You can read short Python examples.
- You know the producer/consumer pattern, where one part of a program creates work and another part processes it.
- You know HTTP status codes and what request latency is.
- You have a basic idea of threads, async tasks, or worker pools.
No earlier chapter is required. This is the first chapter of the tutorial.
What overload means
A service is overloaded when work arrives faster than the service can finish it, and this stays true for long enough to matter. The key word is "finish". A service that accepts requests quickly but completes them slowly is not keeping up.
Overload is a rate problem, not a size problem. Three numbers describe it:
- Arrival rate is how many units of work arrive per second. It is often written as λ (lambda).
- Service rate is how many units of work the system completes per second at its limit. This is the system's capacity. It is often written as μ (mu).
- Utilization is arrival rate divided by service rate. Write it as ρ = λ / μ.
When ρ is below 1, the system can keep up on average. When ρ is above 1, work piles up for as long as the condition lasts. The pile never shrinks by itself, because the system is already running at its limit.
A short burst above capacity is not necessarily a failure. A queue can absorb a burst and drain afterward. The failure begins when the excess is sustained, or when it is large enough to exhaust some resource before the burst ends.
Why capacity is finite
Every service sits on resources that have a hard upper limit. Whichever resource runs out first sets the capacity.
| Resource | What limits it | What happens at the limit |
|---|---|---|
| CPU | Number of cores and clock speed | Tasks wait to be scheduled, so every request slows down |
| Memory | Installed RAM or container limit | Garbage collection thrashes, or the process is killed |
| Threads or workers | Pool size, stack memory | New requests wait for a free worker |
| Connections | File descriptor limit, database pool size | New connections are refused or wait |
| Network bandwidth | Link speed | Packets queue or are dropped |
| Downstream services | Their own capacity | Calls slow down or fail, and the delay spreads upstream |
Adding hardware raises the limit but does not remove it. Autoscaling also does not remove it, because new instances take time to start, and a downstream dependency such as a database may not scale at all. A system that relies on scaling alone is still overloaded during the minutes before scaling finishes.
The bottleneck sets the capacity
A request usually passes through several stages. The slowest stage, the bottleneck, determines how much work the whole path can complete.
Suppose a web handler uses a database connection pool of 20 connections, and each query takes 50 ms while holding a connection. Each connection can serve 20 queries per second (1 / 0.05). The pool as a whole can serve 20 × 20 = 400 queries per second. If the handler can use 2,000 requests per second of CPU but each request needs one query, the real capacity is 400 requests per second. The database pool is the bottleneck.
Capacity is also not fixed over time. It drops when:
- A cache goes cold after a restart, so more requests reach the database.
- A dependency slows down, so each request holds its resources longer.
- Garbage collection pauses stall the process.
- One instance in a fleet fails, so the others absorb its traffic.
You therefore cannot treat capacity as a constant you measured once.
What overload looks like
Overload has a typical shape, and recognizing it helps you tell it apart from an ordinary bug.
- Arrival rate exceeds the service rate.
- Requests wait for a worker, so latency rises first.
- Waiting requests hold memory, connections, and file descriptors, so resource use rises.
- Clients time out and retry, which adds more arrivals.
- The service spends effort on requests whose clients have already gone, so useful throughput falls.
Step 5 is the dangerous one. The service is busy but the share of busy time that produces useful results shrinks. Engineers call the useful part goodput: the rate of completed work that someone still wants. Throughput counts all completed work, and goodput counts only the work that mattered. Under overload, throughput can stay high while goodput collapses toward zero.
The loop from step E through H shows why overload feeds itself. Once it starts, the extra load comes from the clients' reaction, not only from new users.
Seeing it in a small program
The following program simulates a single worker that can process 100 items per second while a producer sends 150 items per second. It reports how long each item waited. You can run it as shown.
import asyncio
import time
SERVICE_TIME = 0.01 # worker takes 10 ms per item, so capacity is 100/s
ARRIVAL_GAP = 1 / 150 # producer sends 150 items per second
ITEMS = 300
async def producer(queue: asyncio.Queue) -> None:
for i in range(ITEMS):
await queue.put((i, time.monotonic()))
await asyncio.sleep(ARRIVAL_GAP)
await queue.put(None)
async def worker(queue: asyncio.Queue) -> None:
while True:
item = await queue.get()
if item is None:
break
i, enqueued_at = item
await asyncio.sleep(SERVICE_TIME)
waited = time.monotonic() - enqueued_at
if i % 50 == 0:
print(f"item {i:3d} total time {waited * 1000:6.0f} ms "
f"queue length {queue.qsize()}")
async def main() -> None:
queue = asyncio.Queue()
await asyncio.gather(producer(queue), worker(queue))
asyncio.run(main())The output shows total time per item growing steadily as the run continues. Early items finish in about 10 ms. Later items wait much longer, because the queue gets longer by about 50 items for every second of the run. Nothing in the code is slow or broken. The only fault is that arrivals (150 per second) exceed capacity (100 per second). The exact timings depend on your machine, but the upward trend does not.
Notice that the queue here has no size limit. The next chapters explain why that choice hides the problem until it is severe.
Why this matters for design
Overload is not an edge case to handle later. Any service with a public interface, shared dependencies, or users who retry will meet it. The useful question is not "how do we avoid overload" but "what should the service do when it happens".
A service has four basic choices when work exceeds capacity:
- Queue it and make everyone wait longer.
- Slow the producer so it sends less, which is back pressure.
- Reject some work so the rest completes, which is load shedding.
- Fail unpredictably by running out of memory or crashing.
The first choice is only safe for short bursts. The last choice is what happens when you make no decision. The rest of this tutorial covers the middle two and the supporting techniques.
Common misunderstandings
- "More workers fix overload." More workers help only if the bottleneck is worker count. If the bottleneck is a database or a lock, extra workers add contention and can lower capacity.
- "A big queue makes us safe." A big queue only moves the failure to a later time and makes the delay longer for everyone.
- "Overload means a traffic spike." Capacity can fall while traffic stays flat, for example when a dependency slows down.
- "If the CPU is below 100 percent, we have headroom." A service can be saturated on connections, locks, or a downstream limit while its CPU is mostly idle.
Exercise
A service has a pool of 10 database connections. Each request holds one connection for 25 ms. Requests also need 2 ms of CPU on a single core, and the CPU work does not hold a connection. Traffic arrives at 500 requests per second. What is the capacity of the connection pool, which resource is the bottleneck, and is the service overloaded? What is the utilization ρ?
Show answer
Each connection serves 1 / 0.025 = 40 requests per second, so the pool serves 10 × 40 = 400 requests per second. The single core serves 1 / 0.002 = 500 requests per second. The pool is the bottleneck, with capacity 400 per second. Arrivals of 500 per second exceed 400, so the service is overloaded. Utilization is ρ = 500 / 400 = 1.25. About 100 requests per second of excess work accumulates for as long as the traffic continues.
Exercise
Explain the difference between throughput and goodput, and describe how a service can have high throughput and near-zero goodput during overload.
Show answer
Throughput is the rate of all work the service completes. Goodput is the rate of completed work that a client still wants. When queues are long, requests wait so long that clients time out and leave. The service still processes those requests and counts them as completed, so throughput stays high. The results go to clients that are no longer listening, so goodput is close to zero.