Batching is where inference throughput comes from, and the scheduler that forms the batches decides who waits and who streams. This lesson explains static, dynamic and continuous batching, how an engine's request queue, admission control and preemption work, and which knobs trade latency for throughput. The second half is a benchmarking method you can trust: realistic workloads, open versus closed load, warm-up, one variable at a time, and a write-up that records the bottleneck, the trade-off and the result.
- Contrast static, dynamic and continuous batching
- Explain request queues, admission control, scheduling order and preemption
- Tune batch-size knobs against latency targets
- Design a benchmark with realistic lengths, open or closed load, and warm-up
- Run a one-variable-at-a-time sweep and write up the result
Three kinds of batching
Static batching collects N requests, runs them together until the longest finishes, then starts the next batch. Short requests wait for long ones and the GPU idles as the batch drains. Dynamic batching, common in general model servers, waits a few milliseconds to group whatever arrived; good for fixed-size inputs such as image classification, still wasteful for generation of different lengths.
Continuous batching (also called in-flight or iteration-level batching) re-forms the batch at every decode step: finished sequences leave, waiting ones join, and the GPU always works on as many sequences as memory allows. Combined with chunked prefill, new requests' prompts are processed in slices alongside ongoing decodes. Every serious LLM engine does this; it is the single biggest throughput win over naive serving.
Inside the scheduler
- A request arrives and enters the waiting queue.
- Each step, the scheduler admits waiting requests if two budgets allow: free KV-cache blocks for the prompt, and room in the per-step token budget.
- It builds the step's batch: decode tokens for running sequences first, then prefill chunks for newly admitted ones, up to the token budget.
- If running sequences need more cache than is free, it preempts some, freeing their cache and returning them to the queue to be recomputed later.
- Finished sequences free their blocks, which lets the next waiting requests in.
| Knob (vLLM name) | Raising it | Lowering it |
|---|---|---|
--max-num-seqs | More concurrency and throughput, slower decode per user | Faster per-user streaming, more queueing |
--max-num-batched-tokens | Bigger prefill chunks, higher throughput | Smoother inter-token latency under long prompts |
| Cache memory (utilisation, FP8 cache, lower max length) | More requests admitted, fewer preemptions | Earlier queueing |
Scheduling order matters too. First-come-first-served is the default; priority scheduling lets interactive traffic jump ahead of batch jobs. Some workloads benefit from separating them entirely: an interactive deployment with tight latency settings and a batch deployment run for throughput.
A benchmarking method you can trust
- Realistic workload. Take input and output length distributions from production logs or a representative dataset. Fixed 128-in, 128-out tests flatter every engine.
- Closed versus open load. Closed-loop (fixed concurrency: each virtual user sends the next request when the last finishes) finds capacity. Open-loop (requests arrive at a set rate, often Poisson) shows how queues build when arrivals do not wait for you, which is how real traffic behaves.
- Warm-up. Discard the first requests: compilation, CUDA graph capture and cold caches distort them.
- Steady state and repeats. Run long enough for percentiles to settle and repeat each point; report variation.
- One variable at a time. Change concurrency, or batch budget, or precision, never two at once, or you cannot attribute the change.
- Record everything. Engine version, model, precision, GPU, flags, dataset, load, and the raw results file.
for c in 1 4 16 32 64; do
vllm bench serve --base-url http://localhost:8000 --model "$MODEL_ID" \
--dataset-name random --random-input-len 2000 --random-output-len 300 \
--num-prompts 400 --max-concurrency "$c" \
--percentile-metrics ttft,tpot,itl --metric-percentiles 50,99 \
--save-result --result-filename "c${c}.json"
doneEngine-neutral alternatives such as AIPerf (aiperf profile --url ... --concurrency ...) run the same kind of sweep against any OpenAI-compatible endpoint, which is useful when comparing engines.
Writing it up
A benchmark is only portfolio proof if someone else can read it and trust it. Keep a fixed template: the question, the setup, the variable you changed, a table and one chart, the bottleneck you identified with evidence (for example, cache usage at 100% and preemptions rising above concurrency 32), the trade-off (throughput gained versus latency lost), and the result: the recommended setting and the conditions under which it holds.
End every write-up with what you would test next. It shows you understand the limits of one experiment, and it is usually the most interesting part to an interviewer.