Sub topic 3
Static Batching and Its Limits
Prerequisites and the basic model
Assume you understand model serving through an HTTP or gRPC endpoint, latency percentiles such as p50 and p99, Python, and Kubernetes Deployments. You should also know that an accelerator, such as a GPU, can process several inputs in one operation.
A batch is a group of inference inputs processed together. Static batching means the group is formed before inference begins, usually by the client or by a fixed processing job. The batch size is known in advance, and the model runs once for that group.
For example, a client can collect four images and send them together:
import numpy as np
def predict_batch(model, images, batch_size=4):
"""Run inference on fixed-size batches."""
outputs = []
for start in range(0, len(images), batch_size):
batch = np.asarray(images[start:start + batch_size])
outputs.extend(model(batch))
return outputsThis approach is simple and predictable. If the model receives 16 images and the batch size is four, it performs four inference calls. The client knows which outputs belong to which inputs because ordering is preserved.
Why static batching improves throughput
Accelerators perform matrix operations more efficiently when they receive multiple inputs. A larger batch can reuse model weights and reduce per-request overhead. This often increases throughput, which is the number of requests completed per second.
The trade-off is that each request may wait for other requests to join its batch. Static batching works best when:
- Requests arrive in regular groups.
- Inputs have compatible shapes.
- The workload is an offline job, such as daily image processing.
- A fixed processing time matters more than immediate response time.
The following diagram shows the request pattern.
Limits for online inference
Static batching becomes less suitable when requests arrive unpredictably.
If only three requests arrive, a client that requires batches of four must either wait for another request or send a smaller final batch. Waiting increases latency. Sending a smaller batch reduces accelerator efficiency.
Inputs can also have different shapes. For example, text sequences may contain different numbers of tokens. Padding shorter inputs to the longest input makes one rectangular tensor, but the model then spends computation on padding tokens.
A fixed batch can also create a straggler effect. If one input takes longer to prepare, the whole batch waits. All responses may be delayed even when most inputs were ready earlier.
| Problem | Effect |
|---|---|
| Low or uneven traffic | Requests wait for a complete batch |
| Different input sizes | Padding wastes computation or requires separate batches |
| One slow input | The whole batch can finish late |
| Sudden traffic changes | A fixed size may underuse or overload the server |
> callout warning > Measure p95 and p99 latency, not only average latency. Static batching can improve average throughput while making the slowest requests violate the service objective.
Static versus dynamic batching
With dynamic batching, the server collects requests that are already waiting and starts inference when it reaches a maximum batch size or a short deadline. This adapts better to changing traffic. Static batching remains useful when the application already owns the complete workload or needs deterministic batch boundaries.
Static batching also differs from continuous batching, which is designed for autoregressive models such as large language models. Continuous batching can add and remove sequences between generation steps. A fixed image batch cannot do this because all inputs usually pass through the model together.
Exercise
A client sends requests in fixed batches of eight, but traffic often arrives as one request at a time. What two latency problems can occur?
Show answer
Each request may wait for seven more requests, or the client may send underfilled batches that reduce accelerator efficiency. A timeout can limit waiting, but it changes the fixed-batch behavior.