Education › Inference › Stage 4: Hands-on model serving

Batching, scheduling and benchmarking

Continuous batching, request queues and schedulers — and a benchmarking method that changes one variable at a time.

Intermediate ~35 min read Module 8 of 15

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.

After this module you can
  • 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

  1. A request arrives and enters the waiting queue.
  2. 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.
  3. It builds the step's batch: decode tokens for running sequences first, then prefill chunks for newly admitted ones, up to the token budget.
  4. 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.
  5. Finished sequences free their blocks, which lets the next waiting requests in.
Knob (vLLM name)Raising itLowering it
--max-num-seqsMore concurrency and throughput, slower decode per userFaster per-user streaming, more queueing
--max-num-batched-tokensBigger prefill chunks, higher throughputSmoother inter-token latency under long prompts
Cache memory (utilisation, FP8 cache, lower max length)More requests admitted, fewer preemptionsEarlier 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.
A sweep with vLLM's benchmark: same workload, concurrency as the only variable
bash
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"
done

Engine-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.

Tip

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.

Hands-on practice

A sweep and a one-page write-up

  1. Serve a small model with vLLM on one GPU.
  2. Run the concurrency sweep above and collect the five results files.
  3. Tabulate output tokens per second, p50 and p99 TTFT and TPOT per concurrency level.
  4. Pick a target (for example p99 TPOT under 50 ms) and find the highest concurrency that meets it.
  5. Change exactly one setting (for example --max-num-batched-tokens or --kv-cache-dtype fp8), rerun only the levels around your operating point, and compare.
  6. Write one page: question, setup, variable, results table, bottleneck with evidence, trade-off, recommendation, next test.
Cheat sheet

Batching, scheduling and benchmarking — at a glance

Main things to focus on

  • Continuous batching re-forms the batch every step; it is the main throughput win.
  • Admission needs free cache blocks and token budget; preemption frees cache under pressure.
  • More sequences per step: more throughput, slower per-user decode.
  • Benchmark with real length distributions, warm-up, steady state and repeats.
  • Closed-loop finds capacity; open-loop shows queueing under real arrivals.
  • Change one variable at a time; write up bottleneck, trade-off, result.

vllm bench serve

--dataset-name random --random-input-len N --random-output-len MSynthetic lengths
--max-concurrency CClosed-loop load level
--request-rate ROpen-loop arrivals per second
--num-prompts NRequests per run
--percentile-metrics ttft,tpot,itl --metric-percentiles 50,99What to report
--save-result --result-filename FKeep raw results

Scheduler knobs

--max-num-seqsCap on sequences per step
--max-num-batched-tokensTokens per step, incl. prefill chunks
--scheduling-policy priorityLet high-priority requests go first

AIPerf

aiperf profile --url URL --model M --endpoint-type chat --streamingBenchmark any OpenAI-compatible server
--concurrency C --request-count NLoad level and size
aiperf plotChart the results of a profile run

Write-up template

Question / Setup / VariableWhat, on what, changing what
Results table + one chartThroughput vs latency percentiles
Bottleneck + evidenceWhich resource limited it, and how you know
Trade-off / Result / Next testWhat you gave up, what to set, what to try

Common pitfalls

  • Fixed short prompts that do not resemble production.
  • Including warm-up requests in the results.
  • Changing several settings between runs.
  • Only closed-loop tests, missing how queues explode under bursty arrivals.
  • Publishing numbers without versions, flags and hardware.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →