Sub topic 1
Latency and Throughput in Model Serving
Prerequisites
This chapter assumes that you know how a trained model is exposed through an HTTP or gRPC endpoint. You should also understand basic latency measurements such as p50 and p99, read simple Python and YAML, and know that a Kubernetes Deployment runs one or more copies of a service.
Model serving turns an incoming request into a prediction. The request usually passes through a client, a network, a frontend or router, a model server, and the model runtime. Each stage adds work and may add waiting time.
Latency and throughput
Latency is the time needed to complete one request. For an inference service, it can include:
- Network time between the client and server.
- Queue time while the request waits for a worker or batch.
- Preprocessing time, such as tokenization or image decoding.
- Model execution time on the CPU, GPU, or another accelerator.
- Postprocessing and response transmission time.
A useful model is:
end-to-end latency = network time + queue time + processing time
The average can hide overload. Measure percentiles instead:
- p50 is the latency at the midpoint. Half of requests are faster.
- p95 is a higher-tail measurement. Five percent of requests are slower.
- p99 shows behavior for the slowest one percent.
Throughput is the amount of work completed per unit of time. For request-based services, it is often measured in requests per second. For language models, tokens per second may also matter.
These goals compete. Processing requests together can improve throughput because an accelerator can perform matrix operations more efficiently on a larger input. However, the first request in a group may wait for later requests. That waiting increases latency, especially when traffic is light.
| Workload condition | Likely priority | Main risk |
|---|---|---|
| Interactive predictions | Low p95 or p99 latency | Waiting in a batch |
| Offline scoring | High throughput | Underusing the accelerator |
| Mixed traffic | A controlled balance | One policy harms the other |
A simple measurement model
Suppose a model server handles one request in 40 ms when it is alone. Its maximum single-worker rate is approximately:
processing_time_s = 0.040
requests_per_second = 1 / processing_time_s
print(f"{requests_per_second:.1f} requests/s")This prints 25 requests per second. It is only a starting estimate. Real throughput is lower when requests wait, preprocessing consumes CPU, or the model server has limited concurrency.
Batching can change the estimate. If a batch of eight requests takes 100 ms, the effective model execution rate is 80 requests per second, before queueing and other overhead. Each request may still experience more latency than a single request because it waits for the batch to form.
Measure both per-request latency and completed work. A service that reports high throughput but misses its latency objective is not healthy for interactive use.
Exercise
Exercise
A request takes 50 ms to process when it runs alone. What is the approximate single-worker throughput, ignoring overhead?
Show answer
1 / 0.050 = 20 requests per second.