Sub topic 2
Choosing max batch size and max queue delay
This chapter assumes you can deploy a model behind an HTTP endpoint, read p50 and p99 latency, and read a Kubernetes Deployment manifest.
Choosing max batch size and max queue delay
The server holds an arriving request until either the batch reaches max_batch_size or max_queue_delay expires. A larger batch raises throughput because the accelerator processes more inputs per pass. A longer delay gives the server more time to fill the batch, but every request waits that long before compute starts.
Set the delay so that queue wait plus compute time stays under your p99 latency target. If the target is 100 ms and one batch takes 60 ms to run, a queue delay above about 30 ms will break the target for the slowest requests.
max_batch_size: 32
dynamic_batching {
preferred_batch_size: [ 8, 16 ]
max_queue_delay_microseconds: 5000
}Queueing basics and Little's law
Little's law states that the average number of requests in the system equals the arrival rate multiplied by the average time each request spends there: L = λW. At 200 requests per second and 0.12 s average latency, about 24 requests are in flight at any moment. You need enough replica concurrency to hold those 24 requests without queuing grows out of control.
Continuous batching for LLM-style workloads
Language model requests produce output one token at a time, and each request finishes at a different time. Static batching waits for the slowest request in a batch. Continuous batching instead adds a waiting request to the running batch at each decoding step, as soon as a slot frees up.
Choosing what to scale on
| Signal | What it shows | Caveat |
|---|---|---|
| Queue depth | Requests waiting for a batch slot | Rises only after capacity is already short |
| Concurrency | Requests in flight per replica | Needs the replica's concurrency limit to interpret |
| Utilization | Accelerator busy time | Can stay low while requests still queue |
Queue depth usually works best as a leading signal for a model service.
Horizontal autoscaling of replicas
The following HorizontalPodAutoscaler adds replicas when the average queue depth per pod exceeds 4. Scale-down waits five minutes to prevent flapping.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: model-server
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: model-server
minReplicas: 2
maxReplicas: 20
metrics:
- type: Pods
pods:
metric:
name: inference_queue_depth
target:
type: AverageValue
averageValue: "4"
behavior:
scaleUp:
stabilizationWindowSeconds: 0
scaleDown:
stabilizationWindowSeconds: 300Exercise
A service receives 100 requests per second at 0.2 s average latency. Each replica handles 4 concurrent requests, and you want 50% target utilization. How many replicas do you need?
Show answer
In-flight requests are 100 × 0.2 = 20. Usable concurrency per replica is 4 × 0.5 = 2. Replicas needed are 20 ÷ 2 = 10.