How does continuous batching work and why does static batching waste GPU utilization?
Written by the mlprep team · Updated
Walk me through how continuous batching works in LLM serving engines like vLLM or TensorRT-LLM. Why does traditional static batching waste so much GPU capacity on autoregressive generation?
Why static batching fails for autoregressive models#
In traditional deep learning (e.g., ResNet image classification or BERT encoding), every item in a batch runs the exact same fixed-depth computation and finishes together. Static batching works well there.
LLM generation is fundamentally different because output lengths are unknown upfront and highly variable:
- Request A might emit an EOS token after 25 tokens.
- Request B in the same batch might generate 1,200 tokens.
With static batching (request-level scheduling), the entire batch is locked until the longest sequence finishes:
- Finished slots sit idle (or compute wasted padding tokens) for over 90% of the batch lifetime.
- Newly arriving requests must wait in the queue even though memory and compute slots are effectively free.
- Short requests suffer huge tail latency because they wait behind stragglers in both queueing and execution.
How continuous (iteration-level) batching works#
Continuous batching — introduced by Orca and popularized by vLLM — changes the scheduling unit from request-level to iteration-level (one forward pass / one token step at a time):
- After every single decode iteration, the scheduler checks which sequences emitted an EOS token or hit
max_tokens. - Finished sequences are immediately evicted from the running batch and their KV cache memory is freed.
- Waiting requests from the queue are immediately admitted into the newly freed slots on the very next iteration.
Because LLM decode is memory-bandwidth-bound (streaming model weights from HBM once per forward pass), keeping the active batch full amortizes weight reads across more tokens per step — often yielding 5× to 20× higher throughput at similar or lower latency compared to static batching.
Why attention makes mixed batches non-trivial#
Linear layers (QKV projections, FFN/MLP, LM head) operate token-by-token, so they don't care which request a token belongs to — you can flatten all active tokens into a single [total_tokens, hidden_dim] matrix.
The tricky part is attention, because each sequence has a different context length and its own KV cache. Modern engines solve this with custom fused attention kernels (such as PagedAttention or FlashDecoding) that accept variable sequence lengths and non-contiguous KV cache pointers per request in a single GPU kernel launch.
What happens when a newly admitted request has a 4,000-token prompt (prefill) and joins a batch of 30 decode requests that each only need 1 token computed? How do serving engines prevent prefill from stalling decode latency?
The prefill-decode interference problem#
If you admit a 4,000-token prefill into the active decode batch all at once, that single iteration suddenly becomes compute-bound and might take 300–500ms instead of 25ms. Every streaming user in the decode batch experiences a visible 500ms stutter (a spike in Time Per Output Token / TPOT).
Solution 1: Chunked prefill (Sarathi-Serve / vLLM)#
Instead of processing all 4,000 prompt tokens in one shot, split the prefill into fixed-size chunks (e.g., 512 tokens per iteration) and piggyback one chunk alongside the active decode tokens:
- Each iteration has a predictable token budget (e.g.,
max_num_batched_tokens = 512or1024). - Decode tokens get priority slots; remaining budget is filled with a prefill chunk.
- Bonus benefit: Decode alone is memory-bandwidth-bound while prefill is compute-bound. Piggybacking a prefill chunk onto a decode step uses the GPU's idle Tensor Cores almost "for free," improving both throughput and TPOT stability.
Solution 2: Preemption and swapping#
When admitted decode sequences grow and exhaust KV cache memory, the scheduler must preempt the lowest-priority (usually most recently arrived) request — either by recomputing its KV cache later (fast when prefill is cheap) or swapping KV blocks to CPU RAM over PCIe.
Senior/staff considerations#
- Tuning
max_num_batched_tokensdirectly trades off TTFT (larger chunks finish prefill faster) against TPOT/ITL jitter (smaller chunks keep decode steps smooth). - Under heavy long-context traffic, even chunked prefill can struggle to meet tight TTFT and TPOT SLAs simultaneously — which is why large deployments move to prefill-decode disaggregation.
Interview red flags#
- Claiming continuous batching pads all sequences to the same length (padding defeats the point; modern engines use ragged/packed tensors and variable-length attention kernels).
- Ignoring KV cache memory as the true batch-size ceiling (you almost always run out of KV cache memory before hitting a hard compute limit).
- Forgetting that admitting a long prefill stalls active decode requests unless chunked prefill or disaggregation is used.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Static batching locks GPU slots until the slowest sequence finishes, wasting capacity on variable-length generation. Continuous batching schedules at the iteration level — evicting completed requests and admitting new ones every decode step — while chunked prefill prevents long incoming prompts from causing latency spikes for active streaming users.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How do you decide between recomputation vs. CPU-RAM swapping when KV cache memory runs out under continuous batching?
- How does prefix caching interact with chunked prefill when multiple requests share a large system prompt?
- At what batch size does decode transition from memory-bandwidth-bound to compute-bound on an H100?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
How do you serve hundreds of fine-tuned LoRA adapters on one base model efficiently?
Why do prefill and decode have opposite hardware bottlenecks, and how does disaggregated serving help?
How do you measure and optimize TTFT, TPOT, and goodput under production SLA constraints?
How would you design an LLM serving system for millions of users?