Sub topic 1
What overload is and why capacity is finite
Prerequisites
This chapter assumes you can read short Python examples. It also assumes you know basic producer/consumer ideas, HTTP status codes and request latency, and what a thread, async task, or worker pool is.
What overload means
A service is overloaded when work arrives faster than it can finish that work, and the imbalance lasts long enough to matter. A short spike that the service absorbs is not overload. A sustained gap is.
Every service has a finite capacity, which is the maximum rate of work it can complete while still meeting its latency goals. Capacity is finite because every request uses resources that are limited:
- CPU time, so only so many requests can be computed at once.
- Memory, which each in-progress request holds until it finishes.
- Connections and file descriptors, which the operating system caps.
- Threads or worker slots, which limit how many requests run in parallel.
- Downstream capacity, such as a database that allows a fixed number of connections.
The slowest of these limits sets the capacity of the whole service. This is the bottleneck.
Arrival rate versus service rate
Two numbers describe the situation. The arrival rate is how many requests come in per second. The service rate is how many the service can complete per second. If a single worker needs 10 ms per request, its service rate is 100 requests per second. Ten such workers give 1,000 requests per second.
workers = 10
seconds_per_request = 0.010
capacity = workers / seconds_per_request # requests per second
arrival_rate = 1200
print(f"capacity: {capacity:.0f} req/s")
print(f"excess: {arrival_rate - capacity:.0f} req/s")This prints a capacity of 1000 req/s and an excess of 200 req/s. Those 200 requests per second have nowhere to go. Each second, the backlog grows by 200 requests. Nothing in the service stops this on its own.
What happens past capacity
Below capacity, extra load costs little. Latency stays close to the time one request needs. As arrival rate approaches capacity, requests begin to wait for a free worker, so latency rises. Past capacity, the waiting line grows without limit, and so does latency.
Throughput, the number of requests completed per second, does not rise past capacity. It can even fall. The service spends effort on work that is already too late, switches between too many tasks, or exhausts memory. Users then see timeouts, and many of them retry, which adds even more load.
Why more capacity is not the whole answer
Adding servers raises capacity, but it does not remove the limit. Load can still exceed it, for several reasons:
- A traffic spike, such as a viral link or a marketing event.
- A slow dependency, which lowers the service rate while arrivals stay the same.
- A failure that removes servers, so the survivors take on more load.
- A retry wave from clients that saw errors.
Because overload cannot be ruled out, a service needs a planned response. The two main techniques are back pressure, which slows the sender down, and load shedding, which rejects some work on purpose. Later chapters cover both.
Exercise
Exercise
A service has 20 workers. Each request takes 50 ms of worker time. Requests arrive at 450 per second. Is the service overloaded, and by how much does the backlog grow each second?
Show answer
Capacity is 20 / 0.050 = 400 requests per second. Arrivals are 450 per second, so the service is overloaded. The backlog grows by 450 - 400 = 50 requests each second, and it keeps growing until something limits it.