Sub topic 6
Choosing max batch size and max queue delay
This chapter assumes you know what p50 and p99 latency mean and can read Python and a small config file. It also assumes you have seen why batching helps an accelerator and how a dynamic batcher groups requests. Here you will turn those ideas into two concrete numbers for a service, and learn how to check them against a latency objective.
The two knobs
A dynamic batcher closes a batch on whichever of two conditions comes first:
- Max batch size is the largest number of requests sent to the model in one call.
- Max queue delay is the longest the batcher holds the first request of a batch while it waits for others to join.
The two knobs do different jobs. Max batch size caps how much work one call can do, which bounds memory use and the worst-case batch runtime. Max queue delay caps how much latency you will pay for the chance of a bigger batch. The delay is added to a request's latency only when the batch is not already full.
Setting max queue delay from the latency budget
Start from the objective, not from the hardware. Suppose the p99 target is 100 ms end to end, and network, serialization, and preprocessing take 30 ms. That leaves 70 ms for the batcher and the model.
A request can wait in three places:
- In the batcher, up to the max queue delay
d. - Behind a batch that is already running on the model, up to one batch runtime
t(B). - Inside its own batch, which takes
t(B)at full size.
A conservative bound on the worst case is therefore:
worst case ≈ d + 2 × t(B)
This must stay under the budget. With a 70 ms budget and t(32) = 44 ms, the bound is already 88 ms plus d, so a max batch size of 32 does not fit. The bound will not match your system exactly. A server with several model instances overlaps batches, so it may be pessimistic. A server with slow preprocessing in the same process may be optimistic. Use it to pick candidates, then confirm with a load test.
The delay also needs to be compared with how fast requests arrive. At arrival rate λ, a window of length d collects about λ × d extra requests. The time to fill a batch of size B is roughly (B − 1) / λ.
- At 200 req/s and
d = 5 ms, you collect about one extra request, so batches hold about 2 requests. A max batch size of 32 is never reached, anddis what shapes the batches. - At 5,000 req/s the queue is rarely empty, batches fill immediately, and
dalmost never triggers.
This is why the same configuration looks different at night and at peak. At low load the delay is pure added latency with little batching benefit. At high load the batch size cap decides behavior. If a service is usually quiet, keep d small, often 1 to 5 ms for models that run in tens of milliseconds. A good rule is to keep d under about 10 to 20 percent of the single-request model latency, unless the throughput gain justifies more.
Setting max batch size from a measured curve
Do not guess the batch size. Measure the batch latency on the target hardware at sizes 1, 2, 4, 8, and so on, using realistic inputs. A typical result looks like this:
| Batch size | Batch latency (ms) | Throughput (req/s) |
|---|---|---|
| 1 | 8 | 125 |
| 2 | 9 | 222 |
| 4 | 11 | 364 |
| 8 | 15 | 533 |
| 16 | 24 | 667 |
| 32 | 44 | 727 |
| 64 | 85 | 753 |
Throughput is batch size divided by batch latency. The gains shrink as the accelerator saturates. Going from 8 to 16 adds 25 percent, going from 16 to 32 adds 9 percent, and going from 32 to 64 adds under 4 percent, while the latency of each batch roughly doubles each time.
The following script encodes a simple rule. It picks the largest size that fits the latency bound and still improves throughput by at least a minimum fraction over the previous size.
profile = {1: 0.008, 2: 0.009, 4: 0.011, 8: 0.015, 16: 0.024, 32: 0.044, 64: 0.085}
def choose_batch_size(profile, budget_s, max_wait_s, min_gain=0.10):
"""Largest size that fits budget_s (using d + 2*t(B)) and still gains >= min_gain."""
best = None
prev_throughput = None
for size in sorted(profile):
latency = profile[size]
if max_wait_s + 2 * latency > budget_s:
break
throughput = size / latency
if prev_throughput is not None and throughput < prev_throughput * (1 + min_gain):
break
best, prev_throughput = size, throughput
return best
print(choose_batch_size(profile, budget_s=0.100, max_wait_s=0.005)) # 16Size 32 fits the latency bound (5 + 88 = 93 ms), but it is rejected because its throughput gain over size 16 is only 9 percent. Below the bound, the extra latency variance of larger batches buys very little. The choice is 16. If your budget were 60 ms, the latency check would stop the search at 8.
Other limits can also cap the batch size below what the curve allows:
- Accelerator memory. Activation memory grows with batch size. Test the largest size with the longest inputs you accept, not typical ones.
- Padding. For variable-length inputs such as text, the batch is padded to its longest member. One long request can make every short request in the batch pay for the long one. Limiting input length or grouping requests by length reduces the waste.
- Model format. Some compiled engines are optimized for specific batch sizes and run slowly or fail outside them.
Putting it in a server config
Triton Inference Server expresses both knobs in the model configuration:
max_batch_size: 32
dynamic_batching {
preferred_batch_size: [ 8, 16 ]
max_queue_delay_microseconds: 5000
}max_queue_delay_microseconds is the queue delay, here 5 ms. Set preferred_batch_size only when profiling shows that particular sizes are much faster than their neighbors. If the throughput curve is smooth, as in the table above, omit it and let the server form the largest batch it can. In this tutorial's own example, the plain Python batcher takes the same two values as constructor arguments, max_batch_size and max_wait_s.
Failure modes and edge cases
- Delay timer starts late. In a simple batcher like the one in this tutorial, the loop runs one batch at a time. Requests that arrive while the model is busy have already waited, but the timer for the next batch starts only after the loop picks up its first request. Under load, real queue time exceeds
d. Servers such as Triton measure the delay from the oldest request's arrival, which is the safer behavior. - Batch-wide failures. If the model call raises an exception, every request in the batch fails. One malformed input can take out up to
B − 1healthy requests. Validate inputs before they enter the queue. - Unbounded queues. A max batch size does not limit the queue. If arrivals exceed capacity, requests wait ever longer. Set a queue size limit or a per-request timeout, and return an error rather than serving answers that are already too late.
- Tail latency of small traffic. At low load, a request that arrives alone always pays the full
d. The p50 may be fine while p99 shows the delay. Always judge parameters on p99, not on the mean. - Parameters interact with scaling. Adding replicas lowers the rate each replica sees, which lengthens fill times and shrinks batches. A later chapter covers tuning both together.
Exercise
A model has batch latency 6 ms at size 1, 10 ms at size 8, 18 ms at size 16, and 34 ms at size 32. Your p99 budget for batcher plus model is 60 ms, and you set max queue delay to 4 ms. Using the bound worst case = d + 2 × t(B), which batch sizes fit? Then, if requests arrive at 100 req/s, roughly how many requests does a 4 ms window collect, and what does that say about setting max batch size to 32?
Show answer
Size 16 gives 4 + 36 = 40 ms, which fits. Size 32 gives 4 + 68 = 72 ms, which exceeds 60 ms, so the largest size that fits is 16. At 100 req/s, a 4 ms window collects about 100 × 0.004 = 0.4 extra requests, so batches will usually hold 1 or 2 requests. A max batch size of 16 or 32 will almost never be reached at this load. The delay, not the size cap, determines batch formation, and the 4 ms delay mostly adds latency without much batching gain. At this rate you should lower the delay or accept that batching helps only at higher traffic.