Sub topic 5
Latency Objectives and Tail Latency
Assume you know how a model is served through an HTTP or gRPC endpoint, and you understand latency, throughput, p50, and p99. You should also be able to read basic Python and Kubernetes YAML.
Define the latency objective
Inference latency is the time from when a request enters the service until its response is ready. It usually includes several parts:
- Network and request handling time
- Time waiting in a batch queue
- Model execution time
- Response serialization and network time
A service-level objective (SLO) is a measurable target for service behavior. For example, an inference service might require that at least 99% of requests finish within 200 milliseconds. This requirement is written as:
p99 latency <= 200 ms
A percentile describes a position in the latency distribution. If p99 is 200 ms, 99% of requests took 200 ms or less. The slowest 1% took longer.
| Metric | What it describes | Typical use |
|---|---|---|
| p50 | The median request | Typical user experience |
| p95 | The slower edge of most requests | Capacity and user-facing alerts |
| p99 | The tail of the distribution | Strict latency objectives |
| Maximum | The single slowest request | Finding extreme failures |
The average can hide problems. If 99 requests take 50 ms and one takes 2 seconds, the average is about 69.5 ms, but one user still waits two seconds. Percentiles expose this uneven behavior.
Why batching changes the tail
Batching combines multiple requests into one model execution. A batch can improve accelerator utilization and increase throughput, but a request may wait for other requests to arrive.
Dynamic batching creates a batch when it reaches a configured size or when a queue-delay deadline expires. The queue delay becomes part of request latency:
total latency = queue delay + model time + overhead
Suppose model execution takes 40 ms for a batch and the maximum queue delay is 10 ms. A request that waits for the deadline can take at least 50 ms before network overhead. Under light traffic, many requests may wait close to the full deadline. Under heavy traffic, batches may fill quickly, reducing queue delay but increasing execution time.
Tail latency often rises when:
- The queue delay is too large for the SLO.
- Batches contain inputs with different processing costs.
- The accelerator is saturated.
- A replica receives a burst while other replicas are idle.
- Autoscaling adds replicas too slowly.
import statistics
latencies_ms = [42, 45, 47, 49, 51, 52, 55, 61, 72, 180]
def percentile(values, fraction):
values = sorted(values)
index = min(len(values) - 1, int(fraction * len(values)))
return values[index]
print(f"p50: {percentile(latencies_ms, 0.50)} ms")
print(f"p95: {percentile(latencies_ms, 0.95)} ms")
print(f"p99: {percentile(latencies_ms, 0.99)} ms")
print(f"mean: {statistics.mean(latencies_ms):.1f} ms")Set a latency budget
Break the SLO into budgets for each stage. If the p99 target is 200 ms, you might reserve 20 ms for network and handling, 30 ms for queueing, and 150 ms for model execution and response processing. These values are planning limits, not guarantees.
A useful test compares several batching configurations under realistic traffic. Record throughput, p50, p95, p99, batch size, queue delay, and error rate. Select the configuration that meets the tail-latency objective with enough throughput headroom.
Exercise
A service has a p99 objective of 200 ms. Network and handling consume 35 ms at p99, and model execution consumes 120 ms at p99. What is the maximum remaining p99 budget for batching delay?
Show answer
45 ms, because 200 - 35 - 120 = 45 ms. Keep additional headroom because percentiles from separate stages do not always add predictably.