Sub topic 1
Latency and throughput in model serving
Every decision about batching and autoscaling in this tutorial trades latency against throughput. This chapter defines both terms precisely for a model server. It shows where a request spends its time, and it shows why latency rises as a server approaches its capacity.
Prerequisites
This chapter assumes you know the following:
- How a trained model is served behind an HTTP or gRPC endpoint.
- What percentile metrics such as p50 and p99 mean.
- Enough Python and YAML to read short examples.
- What a container and a Kubernetes Deployment are.
You do not need any knowledge of queueing theory or GPU internals. The chapter introduces what it needs.
Two measurements, two questions
Latency is the time one request takes from arrival to response. It answers the question "how long does a user wait?" It is measured in milliseconds per request, and you report it as a distribution (p50, p95, p99), never as a single number.
Throughput is the number of requests the system completes per unit of time, usually requests per second (RPS). For LLM servers it is often tokens per second. It answers the question "how much work can this system absorb?"
The two are linked but not interchangeable. A server that handles one request at a time in 20 ms has 20 ms latency and can sustain at most 50 RPS. A server that processes eight requests together in 25 ms has 25 ms latency and can sustain 320 RPS. The second server is slower for each request and much faster overall. This gap is the reason batching exists.
| Configuration | Time per model call | Requests per call | Max throughput |
|---|---|---|---|
| Batch of 1 | 10 ms | 1 | 100 RPS |
| Batch of 8 | 25 ms | 8 | 320 RPS |
| Batch of 32 | 70 ms | 32 | about 457 RPS |
These numbers are illustrative, not measured. Real values depend on the model, the hardware, and the input size. The pattern is typical. Time per call grows more slowly than batch size, so throughput rises while each request waits longer.
The inference request path
A request passes through several stages. Each stage adds to latency, and only some of them are the model.
The end-to-end latency is the sum of these parts:
- Network time. Transit between client, load balancer, and replica. It is often small inside a cluster and large across regions.
- Preprocessing. Decoding images, tokenizing text, and building tensors. This usually runs on the CPU and can be a hidden bottleneck.
- Queue time. Waiting for the model to become free. This is the only stage that grows sharply under load.
- Model execution. The forward pass. Also called service time.
- Postprocessing. Decoding outputs and serializing the response.
Queue time plus service time makes up most of what the rest of this tutorial tunes. Batching changes service time per request, and it adds deliberate waiting. Autoscaling changes how long the queue gets.
Why latency rises as load rises
If requests arrived at perfectly regular intervals, a server running below capacity would never queue. Real traffic is bursty. Requests arrive in clumps, and a clump forms a short queue even when the average load is low.
A standard approximation for a single server with random arrivals gives mean response time as service time divided by (1 − utilization). Utilization is the fraction of time the server is busy.
| Utilization | Mean response time (20 ms service time) |
|---|---|
| 50% | 40 ms |
| 80% | 100 ms |
| 90% | 200 ms |
| 95% | 400 ms |
| 99% | 2000 ms |
The curve is flat at first and then bends sharply upward. The service time never changed. Only waiting did. Three practical consequences follow:
- Running replicas near 100% utilization is not efficient, because the latency cost is severe.
- A small increase in load near the knee of the curve causes a large increase in latency.
- Tail latency (p99) degrades before the average does.
If arrivals exceed capacity for any sustained period, the queue grows without bound. Latency then keeps climbing until requests time out or the load drops.
Measuring both with a load test
The script below simulates a server that handles one request at a time with a 20 ms service time. It sends requests at a fixed rate and reports latency percentiles and achieved throughput. Capacity is 50 RPS, so the 60 RPS run is overloaded.
import asyncio
import statistics
import time
SERVICE_TIME_S = 0.020
async def handle(lock):
arrived = time.perf_counter()
async with lock: # one request at a time, like a single model instance
await asyncio.sleep(SERVICE_TIME_S)
return time.perf_counter() - arrived
async def run(rate_rps, duration_s=3.0):
lock = asyncio.Lock()
start = time.perf_counter()
total = int(rate_rps * duration_s)
tasks = []
for i in range(total):
# Open loop: arrivals follow a fixed schedule and do not wait for replies.
delay = start + i / rate_rps - time.perf_counter()
if delay > 0:
await asyncio.sleep(delay)
tasks.append(asyncio.create_task(handle(lock)))
latencies = await asyncio.gather(*tasks)
elapsed = time.perf_counter() - start
cuts = statistics.quantiles(latencies, n=100)
print(
f"offered={rate_rps:3d} rps "
f"throughput={total / elapsed:5.1f} rps "
f"p50={cuts[49] * 1000:6.0f} ms "
f"p99={cuts[98] * 1000:6.0f} ms"
)
async def main():
for rate in (20, 40, 60):
await run(rate)
asyncio.run(main())Expect a pattern like this, with exact values varying by machine:
- At 20 RPS, p50 is close to 20 ms and p99 is not much higher.
- At 40 RPS, utilization is 80%, and p99 is noticeably above p50.
- At 60 RPS, achieved throughput stays below the offered rate, because the server cannot go faster than 50 RPS. Latency climbs for the whole run, so p99 reaches several hundred milliseconds.
Throughput is bounded by the slowest stage
Throughput is limited by the stage with the lowest capacity. If the model can do 300 RPS but single-threaded preprocessing can do only 120 RPS, the replica delivers 120 RPS and the accelerator sits idle. When you profile a service, check each stage's capacity separately before assuming the model is the limit.
Accelerator-bound services show high accelerator utilization with a queue in front of it. CPU-bound services show a busy CPU, an idle accelerator, and a queue in front of preprocessing. The fix differs. The first calls for batching or more replicas. The second calls for faster preprocessing or more CPU.
What to carry forward
- Latency is a distribution per request. Throughput is a rate for the whole system.
- Queue time is what grows under load. Service time is mostly fixed for a given batch size.
- Latency degrades sharply as utilization approaches 100%, so capacity targets need headroom.
- Accelerators gain throughput from grouping work, at the cost of waiting. The next chapter explains why.
Exercise
A replica has a 25 ms service time per request and handles one request at a time. (a) What is its maximum throughput? (b) Using the mean response time approximation from this chapter, what is the mean response time at 80% utilization? (c) At what arrival rate is the replica at 80% utilization?
Show answer
(a) 1 / 0.025 s = 40 RPS. (b) Service time divided by (1 − 0.8) = 25 ms / 0.2 = 125 ms. (c) 0.8 × 40 RPS = 32 RPS.
Exercise
Your service reports p50 latency of 30 ms, but users complain about slowness. A load test shows accelerator utilization at 35% while the CPU is saturated. Which stage should you investigate first, and why would adding more accelerator capacity not help?
Show answer
Investigate preprocessing and postprocessing on the CPU. Throughput is limited by the slowest stage, and here the CPU stage is saturated, so requests queue in front of it while the accelerator is mostly idle. More accelerator capacity would not raise the CPU stage's capacity. The p50 figure also hides tail behavior, so check p95 and p99 as well.