Module 2 · Model serving and inference scaling · Chapter 05 · Free
Serving engines: what vLLM and its peers actually do
- A serving engine sits between the HTTP request and the GPU. It decides which requests run, in what order, and how memory is shared.
- Its core trick is continuous batching: new requests join the running batch as slots free up, instead of waiting for a whole batch to finish.
- Most operator pain comes from three settings: sequences at once, tokens per step, and GPU memory share for the KV cache.
The picture: the engine is air traffic control for tokens, the GPU is the runway, and the controller decides whether planes queue or flow.
- The five jobs a serving engine does per request.
- How continuous batching, PagedAttention, prefix caching and chunked prefill help.
- A worked static versus continuous comparison, waits added up.
- The three settings you will actually touch, and their trades.
- A matching lab: six symptoms, six fixes.
Copperline is a made up B2B SaaS company. Its first support copilot was a naive script by Priya Nair, the staff platform engineer: load the model, take one request, generate the full answer, reply, repeat.
- One user at a time meant the GPU sat mostly idle while everyone else queued. All figures here are illustrative.
- At an illustrative 40 peak users, the last in line waited through 39 full answers before hers started.
- Ana, the support lead, said the copilot felt broken. The GPU graph looked calm. Both were right: the scheduling was not.
The fix was not a bigger GPU. It was a serving engine.
- The engine pipeline, from HTTP request to streamed token.
- Continuous batching, and why static batches waste the GPU.
- The KV cache, PagedAttention and prefix caching.
- Chunked prefill and streaming output.
- The three settings an operator actually touches.
- The picture, a worked comparison, and a matching lab.
Next: what happens between the request landing and the first token.
Step 1: The pipeline, from HTTP request to running batch
A serving engine owns the model on the GPU. Your service sends a prompt over HTTP. Everything after that is the engine's job.
- Accept and tokenise: prompt text becomes tokens, and the request queues.
- Schedule: a scheduler picks which requests run next, based on your limits and free memory.
- Prefill: the prompt is processed once, building the KV cache needed to generate. Chapter 3 covers that cache in depth.
- Decode: one token per step per running request, until each finishes or hits its limit.
- Stream: tokens go back as produced, not at the end.
Engines in this category include vLLM, TensorRT-LLM and SGLang. They differ in details, so this chapter teaches the shared jobs, and names a feature only where it is documented for that engine.
Think of a kitchen pass: orders queue, work is grouped, dishes leave as ready. Where the picture breaks down:
- A kitchen finishes each dish once. The engine revisits every request each step.
- Requests share one GPU and cache pool, so one long request can crowd out others.
Next: the scheduling idea behind the whole category.
Step 2: Continuous batching, the batch that never waits
A GPU works on many rows at once far faster than on one row. So the engine groups requests into a batch and runs one step for the group. The question is when the group changes.
- Static batching: run a batch until every request finishes, then form the next. A request needing 2 tokens waits for a batch mate needing 200.
- Continuous batching: check the batch at every step. A finished request frees its slot at once, and a queued request joins the next step.
vLLM documents continuous batching as a core part of its design, and the main reason an engine beats Priya's script on the same GPU.
Think of a bus versus a metro where riders get on and off at each station. Where the picture breaks down:
- Batch slots are limited by memory, so a join still waits when the cache is full.
- Passengers do not slow each other down. Each added sequence adds work per step, raising per token latency.
Next: the memory system that decides how many requests can run at once.
Step 3: The KV cache, PagedAttention and prefix caching
While a request runs, the model keeps a KV cache: stored key and value data for every token so far, so each new token is cheap to compute. It lives in GPU memory and grows with prompt and answer length.
- The problem: if each request reserves one long stretch for its worst case length, most of it sits empty, and fewer requests fit.
- PagedAttention: vLLM's documented approach splits the KV cache into small blocks, like pages in an operating system. A request uses only the blocks it needs, and freed blocks return to the pool at once.
- The result: less waste, so more sequences run at once in the same memory.
- Prefix caching: many requests share the same prompt start, such as a system prompt. The engine caches those blocks and reuses them, skipping repeat prefill.
Think of renting whole rooms versus single dorm beds. Where the picture breaks down:
- Beds are static. Blocks fill token by token, tracked per request.
- A guest checks out once. Blocks free and refill many times a second.
Next: chunked prefill and streaming.
Step 4: Chunked prefill and streaming output
Prefill is heavy: a long prompt takes a long step, and one huge prefill pauses every other running request.
- Chunked prefill, at a glance: a long prompt is split into chunks across steps, mixed with decode steps. Long prompts get in, but nobody else freezes.
- Streaming: each token goes to the caller as generated. Text starts fast and grows, instead of silence then a wall of text.
- Why operators care: streaming makes time to first token the number users feel, and Chapter 4 sets targets on it.
Next: the three settings that control all of this.
Step 5: The three settings an operator actually touches
Engines expose many flags, but you will tune three. Names vary by engine; the bracketed names are vLLM's documented ones.
- Max sequences (
max_num_seqs): the most requests running at once. Raise it for throughput, until latency or memory says stop. - Max batched tokens (
max_num_batched_tokens): the token budget for one step, shared by prefill chunks and decode. Raise it to admit long prompts faster; lower it to protect decode smoothness. - KV cache memory share (
gpu_memory_utilization): the share of GPU memory the engine may use, weights plus cache. Higher means more room for blocks, but less headroom on a shared card.
vllm serve copperline-copilot-model \
--max-num-seqs 128 \
--max-num-batched-tokens 8192 \
--gpu-memory-utilization 0.90 \
--enable-prefix-caching
This command is illustrative. Treat the numbers as a sketch, not a recipe. The right values come from your prompt lengths, answer lengths and latency target: the sizing work in Chapter 9.
Predict first:
More sequences per step means more work per step, so per token latency rises even as throughput improves. Every running sequence also holds cache blocks, so doubling concurrency doubles cache pressure. Both effects come from the same knob.
Next: the picture, one request flowing through the engine.
Step 6: The picture, inside a serving engine
Press Next to walk a request through the engine.
- The prompt arrives over HTTP and queues. Queue time is part of user latency.
- The scheduler checks your limits and free blocks, then admits it when a slot is safe.
- Under continuous batching it joins the next step, not a fresh batch.
- Prefill and each token add blocks, which PagedAttention lets live anywhere in the pool.
- Each token streams at once. At the end, slot and blocks free up.
Next: the worked example, static versus continuous.
Step 7: Worked example, static versus continuous batching
Six requests reach the copilot at once. All figures are illustrative. One step makes one token per running request. The batch holds at most 3. A needs 8 tokens; the rest need 2 each.
1. Static, first batch. A, B and C start at step 0 and run until the longest finishes: 8 steps. B and C waste 6 steps each. All complete at time 8.
2. Static, second batch. D, E and F run steps 8 to 10. Completions: 8 + 8 + 8 + 10 + 10 + 10 = 54. Average: 54 / 6 = 9 steps.
3. Continuous. A, B and C start at step 0. B and C finish at time 2. D and E join, finishing at 4. F joins, finishing at 6. A finishes at 8.
4. Continuous totals. Completions: 2 + 2 + 4 + 4 + 6 + 8 = 26. Average: 26 / 6, about 4.3 steps.
5. The lesson. Same GPU, same requests. The average wait is less than half, because finished slots refill instead of idling.
Next: the lab, matching symptoms to fixes.
Step 8: Hands-on lab, match the symptom to the engine
Why this exercise: on call, you meet symptoms first. Name the behaviour behind each one before touching a flag.
Lab: On call, you meet symptoms first. This lab runs in your browser and needs JavaScript enabled.
- Four of the six are engine behaviours. Only two are knobs, each with one trade.
- When a symptom fits two answers, ask what is scarce: slots, blocks, steps or budget.
Next: a field note and the mix-ups.
In production practice, teams moving from a hand rolled script to a dedicated engine often report the same surprise: the GPU was never the bottleneck, the scheduling was. Keep settings as a small documented set per service, changed one at a time with a latency graph open. Gains vary, so measure on your own traffic.
Mix-ups to avoid
- "Batching means waiting to fill a batch." That is static batching. Continuous batching refills slots each step.
- "The KV cache is a result cache." It stores per token state for running requests, not finished answers.
- "Raise max sequences and everything gets faster." Throughput rises, but per token latency and cache pressure rise too.
- "Streaming makes generation faster." It makes the wait feel shorter. Total time is unchanged.
- "All engines work the same inside." Jobs are shared, but features and defaults differ. Check your engine's docs.
Chapter quiz
Five questions, 70% to pass.
Review this chapter in 5 minutes
Watch the whole chapter recapped: continuous batching, PagedAttention and prefix caching, chunked prefill, and the three operator settings.
- Five jobs: accept, schedule, prefill, decode, stream. Category examples: vLLM, TensorRT-LLM and SGLang.
- Continuous batching refills freed slots each step: average completion fell from 9 steps to about 4.3 in the example.
- PagedAttention is vLLM's documented block based KV cache management: small blocks, no long reservations.
- Prefix caching reuses shared prompt starts. Chunked prefill splits long prompts so they do not stall decode.
- Three knobs: max sequences, max batched tokens, KV cache memory share.
Decision rule: name what is scarce first, slots, blocks, steps or token budget, then pick the behaviour or setting that targets it.
Review
Glossary
- Serving engine
- Program that runs a model on the GPU and handles requests. Example: the engine behind the copilot API.
- Scheduler
- Engine part that picks which queued requests run next. Example: admitting one when a slot frees.
- Static batching
- A fixed group runs until every member finishes. Example: a 2 token answer waiting on a 200 token mate.
- Continuous batching
- The batch refills at every step as requests finish and arrive. Example: joining one step after a slot frees.
- KV cache
- Stored key and value data per token, in GPU memory during a request. Example: it grows as an answer lengthens.
- PagedAttention
- vLLM's block based KV cache management. Example: three small blocks, not one long reservation.
- Prefix caching
- Reusing blocks for a prompt start shared by many requests. Example: one system prompt computed once.
- Prefill
- Processing the whole prompt once before generation. Example: a long invoice prompt, one heavy step.
- Chunked prefill
- A long prompt split across steps, mixed with decode work. Example: a 4,000 token prompt in four chunks.
- Streaming
- Each token sent as it is generated. Example: text appearing word by word.
- Max sequences
- Engine limit on requests running at once. Example: a cap of 128.
Next: "Batching and streaming: the latency budget of a token".