Sub topic 7
Queueing basics and Little's law
This chapter assumes you know what latency, throughput, and p50/p99 mean. It also assumes you can read short Python, and that you understand the dynamic batching and max queue delay ideas from the earlier chapters. By the end you will be able to estimate how many requests a model service holds at once, explain why latency rises sharply as a replica fills up, and turn both facts into a first replica count.
Why a model server is a queue
A model replica can work on only so many requests at once. Requests that arrive while it is full wait their turn. That waiting line, plus the work in progress, is what queueing theory describes.
Every request passes through three stages:
- It arrives at some rate.
- It waits in a queue if no capacity is free.
- It is served, either alone or inside a batch, and then leaves.
Dynamic batching adds a deliberate wait: the batcher holds the first request for up to max_wait_s so that others can join. This wait is a second queue, one you chose to have. Capacity planning has to account for both the queue you chose and the queue you did not.
Terms used in this chapter
| Symbol | Name | Meaning | Typical unit |
|---|---|---|---|
| λ (lambda) | Arrival rate | Requests entering the system per second | req/s |
| W | Time in system | Average time from arrival to response, queueing plus service | seconds |
| L | Concurrency | Average number of requests in the system at one moment | requests |
| S | Service time | Time to process one request when nothing is waiting | seconds |
| ρ (rho) | Utilization | Fraction of capacity in use | 0 to 1 |
Little's law
Little's law states that, for a system in a steady state:
L = λ × W
The average number of requests in the system equals the arrival rate times the average time each one spends there. It holds for any arrival pattern and any service order. It also holds for any boundary you draw, as long as you count both arrivals and time inside the same boundary.
Two examples:
- 200 req/s with a 120 ms average latency gives L = 200 × 0.12 = 24 requests in flight.
- If you draw the boundary around the queue only, then Lq = λ × Wq. This gives the average queue length from the average wait before service starts.
The second form is useful because queue depth is a common autoscaling signal (the later chapter on scaling signals covers it). If you know the arrival rate and the average wait, you know the expected queue depth.
Steady state and averages
Little's law needs two conditions:
- The system is in steady state. Over your measurement window, the number of requests that arrive is about the number that leave. During a traffic ramp or a burst, the queue is growing, and the law applied to a short window gives wrong answers.
- The numbers are averages. W is the mean latency, not p99. Plugging a p99 into the law gives a quantity you can use as a conservative sizing figure, but it is not the average concurrency.
From concurrency to replicas
Once you know L, divide by the concurrency one replica can handle well. Leave headroom, because a replica running at 100% utilization has long queues (see the next section). This is the calculation from the spine's code example:
import math
def required_replicas(arrival_rate_rps, avg_latency_s, concurrency_per_replica, target_utilization=0.7):
"""Little's law: in-flight requests L = arrival rate * latency."""
in_flight = arrival_rate_rps * avg_latency_s
usable = concurrency_per_replica * target_utilization
return math.ceil(in_flight / usable)
# 200 req/s at 120 ms average latency, 8 concurrent requests per replica
print(required_replicas(200, 0.120, 8)) # 5Step by step: L = 24. One replica handles 8 concurrent requests, and you plan to use only 70% of that, so the usable concurrency is 5.6. Then 24 / 5.6 = 4.29, which rounds up to 5 replicas.
concurrency_per_replica is a measured quantity. For a batching server it is usually the number of requests the replica can hold across the batch being processed and the batches waiting to start. Measure it with a load test. Do not guess it from the maximum batch size alone.
Why latency climbs as utilization rises
Utilization is the fraction of time a server is busy. For one server, ρ = λ × S. If a request takes 100 ms and 7 requests arrive per second, ρ = 0.7.
Real traffic is bursty. Several requests sometimes land within a few milliseconds of each other, and the later ones wait. The classic single-server model with random (Poisson) arrivals and random service times, called M/M/1, gives an average time in system of:
W = S / (1 − ρ)
With S = 100 ms, the result is:
| Utilization ρ | Average time in system |
|---|---|
| 0.5 | 200 ms |
| 0.7 | 333 ms |
| 0.8 | 500 ms |
| 0.9 | 1000 ms |
| 0.95 | 2000 ms |
The M/M/1 formula is a simplification. A GPU server with batching has several parallel slots and service times that depend on batch size, so the exact numbers differ. The shape holds, though. Latency is flat at moderate load, then bends upward, and it grows without bound as ρ approaches 1. This is why target_utilization in the sizing function is below 1. It is also why you plan capacity for tail latency, not only for the average. Tail percentiles bend upward earlier than the mean does.
Check the formula with a simulation
The script below simulates one server with random arrivals and random service times. It tracks when the server becomes free and records each request's time in the system.
import random
def simulate(arrival_rate, service_s, n=200_000, seed=1):
"""Single-server FIFO queue with exponential arrivals and service times.
Returns the average time in system in seconds."""
rng = random.Random(seed)
t_arrival = 0.0
server_free_at = 0.0
total = 0.0
for _ in range(n):
t_arrival += rng.expovariate(arrival_rate)
start = max(t_arrival, server_free_at)
server_free_at = start + rng.expovariate(1 / service_s)
total += server_free_at - t_arrival
return total / n
service_s = 0.1
for rho in (0.5, 0.7, 0.9):
rate = rho / service_s
simulated_ms = simulate(rate, service_s) * 1000
theory_ms = service_s / (1 - rho) * 1000
print(f"rho={rho}: simulated {simulated_ms:.0f} ms, formula {theory_ms:.0f} ms")The simulated values land close to 200, 333, and 1000 ms. The match is loosest at ρ = 0.9, where the queue converges slowly and results vary more between seeds. Try a different seed or a larger n to see that spread.
How batching interacts with queueing
Dynamic batching changes both terms of the model:
- It raises the service rate. A batch of 8 costs much less than 8 separate calls, so the same hardware sustains a higher λ before ρ approaches 1.
- It adds waiting time. A request can wait up to
max_wait_sfor the batch to fill. At low load that wait is almost pure added latency. At high load batches fill quickly and the wait shrinks, so batching helps most when it is needed most.
For Little's law, both effects show up in W. Measure the end-to-end average latency under realistic load, with batching enabled, and use that W. A latency number taken from a single request on an idle server leaves out both the batching wait and the queueing wait. It will make your replica count too small.
Common errors
- Using unloaded latency. Benchmarks that send one request at a time measure S, not W. Use latency measured at the expected arrival rate.
- Sizing to 100% utilization. The formula above shows why: W grows without bound. Start with a target between 0.6 and 0.7 and adjust from load tests.
- Mixing units. Arrival rate in requests per second needs latency in seconds. A 120 ms latency entered as 120 gives a result 1000 times too large.
- Using the law on a transient. During a ramp or burst the system is not in steady state. Size for the sustained peak, and treat bursts with queue limits and scaling policy.
- Assuming one request type. If a model serves short and long inputs, use the average latency weighted by the traffic mix, and check the heavy requests separately.
- Treating
max_batch_sizeas the replica's concurrency. The replica can have one batch running and others queued. Measure concurrency under load.
Exercise
A model service receives a steady 300 requests per second. Under load, with dynamic batching enabled, the average end-to-end latency is 80 ms. One replica handles 6 concurrent requests, and you want to run each replica at 75% utilization. How many replicas do you need, and what happens to the latency if you cut the replica count by one?
Show answer
In-flight requests L = 300 × 0.08 = 24. Usable concurrency per replica = 6 × 0.75 = 4.5. Replicas = ceil(24 / 4.5) = ceil(5.33) = 6. With 5 replicas, the same 24 in-flight requests spread over 5 × 6 = 30 slots, which is 80% utilization, above the 75% target. By the shape of the utilization curve, queueing delay grows noticeably as you pass the target, so average and especially tail latency would rise, and a burst could push it much higher. Treat 6 as the starting count and confirm with a load test.