Batching
Why batch
In each decode step, the GPU reads every weight once. For a 7B model in BF16, that is about 14 GB per step. The math for one user is small, so reading the weights takes most of the time. If 32 users share the step, the weights are read once for all of them. Running a batch of many requests together is almost free per extra user, so throughput goes way up.
Static batching
Static batching waits until a group of requests is ready, then runs them all until the last one ends. A request that needs 10 tokens sits idle while another needs 500. New requests wait outside until the whole group is done.
Continuous batching
Continuous batching (also called iteration-level batching) changes the batch at every step. When a request finishes, it leaves at once, and a waiting request takes its place. The GPU stays full, and nobody waits for a stranger's long answer. vLLM, SGLang and TensorRT-LLM all work this way.
The scheduler
The scheduler decides who runs in each step. Common rules:
- First come, first served (FCFS): oldest request first.
- Shortest first: short jobs go ahead, which lowers the average wait.
- Priority classes: for example paid users before free users.
It also respects limits, like how much KV cache memory is free.
Starvation
A rule that helps the average can hurt a few. With shortest first and a steady stream of short requests, a long request may never get a turn. That is starvation. A common fix is aging: the longer a request waits, the higher its priority. Check P99 for each group of users, not just the overall mean.
Remember
- One weight read serves the whole batch, so batching is cheap per user.
- Static batching makes short requests wait for long ones.
- Continuous batching swaps requests in and out every step.
- The scheduler picks who runs; FCFS, shortest first, priority.
- Watch the worst-off group, or some users starve.