Sub topic 1
What Overload Is and Why Capacity Is Finite
Prerequisites
Assume you know basic producer/consumer programming, HTTP latency and status codes, and the difference between asynchronous tasks and worker threads.
Capacity and Overload
A service has finite capacity because its CPU, memory, database connections, workers, and network links are limited. Let arrival rate be the number of requests arriving per second. Let service rate be the number the system can complete per second.
Overload begins when arrivals stay above the service rate:
arrival rate > service rateThe excess work waits in a queue. If arrivals remain higher, the queue grows until memory is exhausted, requests time out, or the service becomes too slow to be useful.
Queues and Little's Law
Little's law connects the average amount of work in a stable system to its arrival rate and time in the system:
L = λWLis average in-flight work, including queued and running requests.λis average throughput, such as requests per second.Wis average time spent in the system, in seconds.
For example, a service processing 100 requests per second with an average latency of 0.5 seconds has about 50 requests in flight. If latency rises to 5 seconds while throughput stays at 100 requests per second, about 500 requests are in flight.
flowchart TD
A[Requests arrive] --> Q[Queue]
Q --> W[Workers]
W --> D[Completed requests]
W --> S[Finite CPU, memory, and I/O capacity]Why Unbounded Queues Make Things Worse
An unbounded queue accepts every request until the process or host runs out of memory. This hides overload at first, but latency grows continuously. Many queued requests may already have expired deadlines, so workers spend resources on work nobody will accept.
A large queue can also delay health checks and priority work. When memory pressure triggers swapping or termination, the service may fail completely.
Bounding the Queue
A bounded queue limits waiting work. The limit makes overload visible and forces a policy: slow producers or reject excess work.
import asyncio
async def producer(queue: asyncio.Queue) -> None:
for item in range(100):
await queue.put(item) # waits when the queue is full
await queue.put(None)
async def consumer(queue: asyncio.Queue) -> None:
while (item := await queue.get()) is not None:
await asyncio.sleep(0.01)
async def main() -> None:
queue = asyncio.Queue(maxsize=10)
await asyncio.gather(producer(queue), consumer(queue))
asyncio.run(main())Back Pressure: Slowing the Producer
Back pressure is feedback that makes a producer reduce its sending rate when downstream capacity is limited. In the example, await queue.put(item) suspends the producer while the bounded queue is full. This preserves work, but it transfers waiting time to the producer and can propagate upstream.
Exercise
A service receives 120 requests per second and completes 100. What happens if its queue is unbounded?
Show answer
The queue grows by about 20 requests per second, so memory use and latency increase until requests fail or the service runs out of resources.