Sub topic 15
Tuning Batching and Autoscaling Together
Assume you understand model-serving endpoints, p50 and p99 latency, basic Python and YAML, and Kubernetes Deployments. This chapter treats batching and autoscaling as one control system. Changing one changes the signals used by the other.
Start with a measurable target
Define a service-level objective (SLO) before changing settings. For example:
- Throughput: at least 200 requests per second
- Latency: p99 below 300 ms
- Error rate: below 1%
- Replica range: 2 to 20
Measure these values during a representative load test. Include request size, response size, model version, and hardware type. A batch size that works for short inputs may fail for long inputs.
Batching usually improves accelerator utilization. However, requests wait while a batch forms, so queue delay increases latency. Autoscaling adds capacity when work accumulates, but new replicas may start slowly and initially have poor cache or kernel performance.
Tune batching before autoscaling
First, hold the replica count constant. Test several combinations of maximum batch size and maximum queue delay.
| Maximum batch | Queue delay | Typical effect |
|---|---|---|
| 1 | 0 ms | Lowest batching delay, lower throughput |
| 8 | 5 ms | Good starting point for many online services |
| 32 | 20 ms | Higher throughput, greater tail-latency risk |
Use a load test that gradually increases request rate. Record achieved throughput, batch-size distribution, queue time, model execution time, p50, p95, and p99 latency.
A useful tuning sequence is:
- Choose the largest queue delay that still meets the p99 objective.
- Increase maximum batch size until accelerator utilization or throughput stops improving.
- Reject settings that meet average latency but violate p99 latency.
- Repeat the test at different request sizes and traffic patterns.
Tune autoscaling against the tuned batcher
After selecting batching settings, add autoscaling. Queue depth is often a direct signal: growing depth means requests are waiting. Concurrency measures in-flight work. Accelerator utilization shows hardware pressure, but high utilization can be healthy when the queue is empty.
Use at least one work signal and one user-impact signal:
- Work signal: queue depth or in-flight requests
- User-impact signal: p95 or p99 latency
- Capacity signal: accelerator utilization
Do not scale directly on a noisy one-second value. Use a short rolling window, minimum replicas, and a delayed scale-down.
Avoid control-loop conflicts
A large batch size can make each replica efficient, so the autoscaler may see high utilization and add no replicas. A small queue-delay limit can produce underfilled batches, reducing throughput and causing unnecessary scale-out.
Set scale-up fast enough to handle bursts, but keep scale-down conservative. For example, require low queue depth for several minutes before removing replicas. Include cold-start time in capacity planning. If a replica needs 90 seconds to load the model, scaling after p99 has already failed is too late.
Verify the combined result
Run tests for steady traffic, sudden bursts, and traffic that falls to zero. Confirm that:
- p99 remains within the SLO.
- Batch sizes increase under load without excessive queue time.
- Replica count rises before the queue becomes unsafe.
- Scale-down does not cause repeated scale-up and scale-down cycles.
- Cold starts do not violate the latency objective.
Exercise
A service meets its throughput target with batches of 32, but p99 latency exceeds its SLO during bursts. What should you try first?
Show answer
Reduce the maximum queue delay or maximum batch size, then retest p99 and throughput. Do not add replicas first, because the batching delay itself may be the main cause.