Continuous batching and the serving scheduler
Static batches waste decode slots. Continuous batching admits and removes requests between decode steps, turning token generation into a scheduler problem.
By the end
Explain why iteration-level batching improves decode throughput and identify the metrics that keep it honest.
LLM serving is not just model execution. It is queueing.
A static batch is easy to picture: collect requests, run them together, wait until all are done, then collect the next group. That works poorly for generation because outputs have different lengths. Short requests finish early, long requests keep running, and batch slots sit unused.
Continuous batching changes the unit of scheduling. Instead of waiting for the entire batch to finish, the scheduler can add new requests between decode iterations as old requests complete.
Decode as a repeated step
Generation is a loop:
choose active requests
run one forward pass
sample one token per active request
update KV cache
remove finished requests
admit new requests if capacity allows
repeat
That "admit new requests" line is the heart of continuous batching. The batch is not a fixed group. It is a living set.
The scheduler must respect memory, max sequence length, priority, fairness, and latency targets. This is why serving engines look like systems software even when the model code is only a few lines.
Why it helps
Imagine four requests:
A: 4 decode tokens
B: 4 decode tokens
C: 40 decode tokens
D: 40 decode tokens
With static batching, A and B finish early, but their slots remain effectively occupied until C and D finish. With continuous batching, the scheduler can insert E and F into the next decode steps after A and B are done.
The GPU sees more useful work per step. The queue drains faster. Throughput rises, especially at high concurrency and varied output lengths.
The catch is that better throughput does not automatically mean better user experience. A scheduler can improve aggregate tokens per second while making tail latency ugly. That is why every serving experiment needs both throughput and latency percentiles.
The metrics
Track at least:
- Time to first token.
- Inter-token latency.
- End-to-end latency.
- Output tokens per second.
- Request throughput.
- Queue wait time.
- GPU memory used by KV cache.
- Active live tokens.
Those metrics separate three different experiences:
Fast first token, slow completion. Good for chat responsiveness, bad for long answers.
High throughput, high queue wait. Good benchmark headline, bad product.
Low latency, low utilisation. Pleasant at small scale, expensive at real traffic.
Inference engineering is choosing which tradeoff the product actually wants.
The scheduler knobs
Common knobs include:
- Maximum number of concurrent sequences.
- Maximum number of batched tokens.
- Prefill chunk size.
- Admission policy for long prompts.
- Priority classes.
- KV-cache memory fraction.
The dangerous knob is the one that sounds purely technical but changes product behaviour. For example, shrinking max batched tokens may improve latency for short requests while starving long-context jobs. Increasing concurrency may raise throughput while making time-to-first-token worse.
Treat scheduler changes like product changes. Measure the distribution, not just the mean.
Prefix caching
Prefix caching reuses KV blocks when requests share a prompt prefix. It is common in workloads with repeated system prompts, retrieval templates, agent scaffolds, or batch evaluation. It can be close to free when it applies, but it does not change the model's outputs; it skips repeated prefill work for shared prefixes.
The reason it belongs beside continuous batching is that both are scheduler-level wins. Neither changes the model. Both change how much useful work the serving system extracts from the same GPU.
References
- vLLM documentation
- vLLM automatic prefix caching design
- Efficient Memory Management for Large Language Model Serving with PagedAttention
Checkpoint
- Why does static batching waste capacity during decode?
- Name two metrics that can get worse while throughput improves.
- Why is prefix caching a scheduler/system optimisation rather than a model-quality optimisation?
Practice this lesson
The reading is the model. These drills are the hours — 2 problems that force the numbers onto paper before the next lesson.