Foundations to Frontier
Tests
F. NLP and Large Language Models › F8 Inference and Serving

Continuous batching

An LLM server receives requests at random times, and their outputs range from a few tokens to thousands. Static batching groups requests and runs the group until its longest member finishes. Short requests wait for long ones, finished sequences occupy slots, and new arrivals queue until the whole batch ends. Continuous (iteration-level, in-flight) batching, introduced by Orca and used by vLLM, TGI, TensorRT-LLM and SGLang, reschedules after every decoding step: finished requests leave and queued ones join at once. This page covers static and continuous batching, the latency metrics (time to first token, time per output token, tail latency), chunked prefill and load. In the exercises you compute the waste of a static batch and the latency metrics. You then simulate a server with 600 requests under a realistic step-cost model. At low load both policies kept up, but continuous batching cut mean latency 4.6× (8.1 s → 1.7 s). At 12 requests per second, static batching saturated at 578 tokens/s with a p95 latency of 100 s. Continuous batching delivered 1394 tokens/s with a p95 of 11.6 s.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Never let a seat stay empty

Imagine a bus that won’t leave until every passenger has reached their stop, and won’t pick anyone up until it returns to the depot. That is static batching: short answers wait for the longest one, and new users wait for the whole group. Continuous batching is a bus that drops passengers off and picks new ones up at every stop: as soon as any answer finishes, a waiting request takes its place. The same GPU then serves many more users, much faster.

LLM lab
7B-class model (32 layers, 32 query heads of 128 dims, fp16) at 8192 tokens: full multi-head KV cache 4.00 GiB; with 8 KV heads (grouped-query attention) 1.00 GiB per sequence.
Why engineers care

Continuous batching is why one GPU can serve many concurrent users with low latency; when choosing a serving stack, it is a must-have feature.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Static batching makes short requests…
  2. 2. Continuous batching reschedules…
  3. 3. Continuous batching mainly improves…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Static batching: collect B requests, prefill, decode until all finish. Waste: slot-steps after each sequence ends, plus queueing of new arrivals.
  • Continuous batching (Orca, 2022): schedule at the granularity of one iteration; requests join and leave between steps. Needs per-sequence KV management (paged caches, F8-2).
  • Prefill vs decode: a joining request’s prefill is compute-heavy and can stall the batch’s decoding; chunked prefill splits long prompts across steps, and some systems run prefill and decode on separate GPUs (disaggregation).
  • Metrics: time to first token (TTFT), time per output token (TPOT), end-to-end latency, throughput (tokens/s), and tail percentiles (p95, p99) under a given request rate. Goodput: throughput that meets latency targets.
  • Little’s law: mean concurrency = arrival rate × mean latency.
📝 Quick check (Level 1)
0/3 answered
  1. 1. TTFT measures…
  2. 2. Chunked prefill prevents…
  3. 3. Little's law: concurrency =

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Batching in practice

Exercise 1.

  • Count decode steps of a static batch.
  • Count slot-steps wasted on finished sequences.
  • Compute the useful fraction of slot-steps.

Practice.

  • vLLM, TGI, TensorRT-LLM (in-flight batching) and SGLang all batch continuously; tune max_num_seqs and max_num_batched_tokens.
  • Benchmark with realistic arrival rates and length distributions (for example vLLM’s benchmark_serving), not one request at a time.
  • Report throughput together with TTFT and TPOT percentiles at each load.
  • Enable chunked prefill for long prompts to protect decode latency.
# vLLM server with continuous batching and chunked prefill
# vllm serve meta-llama/Llama-3.1-8B-Instruct --max-num-seqs 128 \
#      --max-num-batched-tokens 8192 --enable-chunked-prefill
# load test: python benchmarks/benchmark_serving.py --request-rate 8 --num-prompts 1000
🐍 Exercise 1: the waste of static batching
Compute the steps of a static batch, its wasted slot-steps and its useful fraction.
📝 Quick check (Level 2)
0/3 answered
  1. 1. vLLM limit on concurrent sequences:
  2. 2. Benchmark serving with…
  3. 3. Report latency as…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why batching is nearly free. Each decode step reads all weights once, whatever the batch size; per-sequence work (attention over its cache, small matrix rows) is much smaller. With a step cost of a + bB, throughput B/(a + bB) rises almost linearly in B until bB is comparable to a. Any idle slot is lost throughput.

Queueing view. A server is stable only if its capacity exceeds the arrival rate of work. Static batching’s capacity is lower (slots idle while the longest request finishes), so at high load the queue grows without bound and latency explodes. Continuous batching raises capacity and keeps the queue short. Near capacity, waiting time still grows sharply, as in any queue, which is why tail latency should be measured across loads.

Latency at low load. Even when capacity is ample, static batching ties each request’s completion to the longest in its batch and makes arrivals wait for the batch in progress, multiplying latency for short requests.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Throughput rises with batch size because…
  2. 2. Above capacity, queue length…
  3. 3. At low load static batching mainly hurts…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much do continuous batching’s throughput and latency gains depend on load?

History.

  • G.-I. Yu et al., “Orca: a distributed serving system for transformer-based generative models” (OSDI 2022).
  • W. Kwon et al., vLLM (2023); NVIDIA TensorRT-LLM in-flight batching (2023).
  • A. Agrawal et al., “Sarathi-Serve” (OSDI 2024): chunked prefill.

Exercise 2.

  • Requests: 600, Poisson arrivals at 2, 6 or 12 per second; prompts 50–500 tokens; outputs geometric with mean 150 (at most 1000).
  • Engine model: a step takes 10 ms + 0.3 ms per running sequence + 0.02 ms per prefilled prompt token; at most 32 sequences.
  • Policies:
    • static: batches of up to 32, run until the longest finishes, results returned at the end;
    • continuous: join and leave after every step.
load policy throughput (tokens/s) mean latency (s) p95 latency (s) mean TTFT (s)
2 req/s static 278 8.09 14.75 2.85
2 req/s continuous 281 1.74 5.44 0.02
6 req/s static 578 27.09 52.81 19.38
6 req/s continuous 812 2.20 6.80 0.03
12 req/s static 578 52.91 99.56 45.19
12 req/s continuous 1394 4.50 11.58 1.49
  • Low load: both kept up (throughput equals demand), but static batching made latency 4.6× worse.
  • Static batching saturated at about 580 tokens/s: above that, its queue grew and latency rose with every request (p95 100 s at 12 req/s).
  • Continuous batching kept serving: 2.4× the throughput at 12 req/s with a tenth of the tail latency, approaching its own capacity (TTFT starting to grow).
  • Seeds 1 and 2: at 12 req/s, 611 vs 1380 and 591 vs 1405 tokens/s.
  • Caveat: the step-cost model is linear and ignores memory limits (assumed handled by paging); real systems add prefill interference, which chunked prefill reduces.
🐍 Exercise 2 (research): how much do continuous batching's throughput and latency gains depend on load?
Simulate static and continuous batching at three request rates; compare throughput, mean and tail latency, and time to first token.

Batching in AI, robotics and aerospace

  • LLM APIs and chat services: continuous batching underlies essentially all production LLM serving.
  • Shared inference servers: batching vision or policy models from many robots or sensors on one GPU uses the same queueing trade-offs.
  • Ground segments: batch processing of downlinked data trades latency for throughput in the same way.

References

  1. G.-I. Yu, J. S. Jeong, G.-W. Kim, S. Kim and B.-G. Chun (2022). “Orca: a distributed serving system for transformer-based generative models.” OSDI.
  2. W. Kwon et al. (2023). “Efficient memory management for large language model serving with PagedAttention.” SOSP.
  3. A. Agrawal et al. (2024). “Taming throughput-latency tradeoff in LLM inference with Sarathi-Serve.” OSDI.
  4. Y. Zhong et al. (2024). “DistServe: disaggregating prefill and decoding for goodput-optimized LLM serving.” OSDI.