Sub topic 2
Queues and Little's law
This chapter assumes you can read short Python examples, know basic producer/consumer ideas, and understand request latency. It introduces the one formula you need to reason about queues, and shows how to use it to size work in flight.
The working model
A service is a place where work arrives, waits, and gets done. Three quantities describe it:
- Arrival rate (λ): requests arriving per second.
- Service time (W): how long one request spends in the system, from arrival to completion. This includes waiting and processing.
- Work in the system (L): the average number of requests inside the system at one moment, both waiting and being processed.
Little's law ties them together:
L = λ × WIt holds for any stable system, whatever the arrival pattern or processing order. You do not need to know how the service is built inside.
A worked example
A service receives 200 requests per second. Each request takes 50 ms in the system on average.
arrival_rate = 200 # requests per second
time_in_system = 0.050 # seconds
in_flight = arrival_rate * time_in_system
print(in_flight) # 10.0On average, 10 requests are in the system at once. If the service has 10 workers, they are all busy. If it has 4, the rest must wait in a queue.
Capacity and the queue
Each worker finishes one request per service time seconds. With c workers and a processing time s, the maximum throughput is:
capacity = c / sWith 4 workers and 50 ms of processing per request, capacity is 4 / 0.05 = 80 requests per second. Arrivals at 200 requests per second exceed that, so the queue grows by about 120 requests every second.
Little's law applies only to a stable system, where arrivals do not exceed capacity over time. Once λ is above capacity, there is no steady average. The queue keeps growing, and W grows with it.
Using the law to size things
Rearranged, the law answers practical questions.
| Question | Formula |
|---|---|
| How many requests are in flight? | L = λ × W |
| How many workers do I need? | c ≈ λ × s |
| What latency does a given queue imply? | W = L / λ |
The last row matters most for overload. If a queue holds 1,000 requests and the service completes 80 per second, a request that joins the back waits about 1000 / 80 = 12.5 seconds. Most clients gave up long before that.
Common errors
- Using average latency from a healthy period.
Wrises as the queue grows, so a number measured at low load underestimatesLat high load. - Sizing to exactly λ × s. At 100% utilization, small bursts build queues that never drain. Leave headroom, often 20 to 30 percent.
- Counting only processing time.
Win Little's law includes time spent waiting. - Applying the law during overload. The formula describes averages in a stable system. Use it to pick limits before overload, and to read your metrics afterward.
Exercise
A service receives 120 requests per second and each request spends 250 ms in the system on average. How many requests are in the system on average? If it has 20 workers that each need 200 ms of processing per request, can it keep up?
Show answer
L = 120 × 0.25 = 30 requests in the system. Capacity is 20 / 0.2 = 100 requests per second, which is below the 120 arriving, so it cannot keep up. The queue will grow by about 20 requests per second.