Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

LABS · 20  /  SERVING

The Assembly Line.

A GPU serving one request is a factory running one item at a time — almost all of it idle. Batch the requests and throughput soars, because you stream the weights once for many sequences. But batches fight back with latency and memory — and naive batching stalls on its slowest member. Run the line and find the balance.

Act 1 · Why one request wastes a GPU
Stream the weights once, serve many
Generating a token means streaming every weight through the GPU — the compute barely registers next to that memory traffic. So a single request leaves the compute almost idle. Add more requests to the same pass and they ride the same weight-stream nearly for free. Slide the batch up.
Batch size (concurrent requests)1
— tok/s
Throughput
total tokens/sec
—%
GPU compute used
—
Cost / token
relative to batch 1
At batch 1 the GPU is a supercomputer doing a pocket-calculator's work. Each request you add rides the same weight-streaming pass, so throughput climbs and cost-per-token falls — the entire economic case for batching.
Act 2 · The trade
Throughput up, latency up too
Batching isn't free lunch forever. Bigger batches raise total throughput but each request waits to be gathered and shares the pass — so per-request latency climbs, and eventually the KV cache runs out of memory. Find the batch that hits your latency target.
Batch size16
Latency budget (per token)40 ms
—
Throughput
—
Per-token latency
—
Within budget?
Interactive chat wants small batches (low latency); offline bulk jobs want huge batches (max throughput). The serving system's job is to sit at the biggest batch that still clears your latency budget.
Act 3 · The straggler problem
Static batching stalls; continuous doesn't
Requests finish at different times. Static batching waits for the whole batch to complete — short requests finish and their slots sit idle behind the longest one. Continuous batching evicts a finished request instantly and admits a waiting one. Run both and watch the idle time.
Mode:
Generating Idle slot (wasted) Finished Waiting to admit
—%
Avg slot utilization
0
Requests completed
0
Steps elapsed
Static leaves a growing graveyard of idle slots as short requests finish and wait for the long one. Continuous refills instantly — several-fold more throughput from the same GPU. It's why vLLM and friends made it the default.

Why a busy GPU is a cheap GPU

The economics of LLM serving rest on one fact: decode is memory-bound. Producing a token streams every weight of the model through the processor, and that memory traffic — not arithmetic — dominates the time. A forward pass therefore has enormous spare compute: at batch size 1, the GPU's tensor cores are mostly idle while the weights crawl through.

Batching spends that spare compute. Run 32 requests in one forward pass and you stream the weights once but advance 32 sequences — roughly 32× the tokens for about the same memory cost. Throughput (tokens per second) is what sets your cost per token, so batching is the single biggest lever on serving economics. The limit is the KV cache: every concurrent request holds its own cache, so you can only batch as many as fit in VRAM.

Throughput ≠ latency

Two different clocks. Throughput (system tokens/sec) sets cost and rises with batch size. Latency (one request's tokens/sec) sets user experience and falls as batches grow, because each request shares the pass. You tune batch size to the biggest value that still meets a latency target — small for chat, huge for offline.

The straggler tax, and the fix

Requests have wildly different output lengths. Static batching runs a fixed group until all finish — so a batch with one 2,000-token request drags 31 short ones, whose freed slots sit idle to the end, and no new request can join. The GPU runs at the pace of its slowest member.

Continuous batching (a.k.a. in-flight batching) fixes it: the instant any sequence finishes, it's evicted and a waiting request takes its slot — every single step. The batch is never padded out for stragglers, utilization stays pinned high, and real-world throughput often jumps several-fold. It's the headline feature of modern serving stacks like vLLM-style engines, and the reason a well-run GPU serves so many more users than a naive one.

The picture: the same eight requests, two schedulers

static batch 1batch 2 waits for step 400 continuous 400-token request done at 490 0200400600800 steps mean finish 342.5 → 167.5 steps; last request 800 → 490
The worked example above: four slots, eight requests of 20, 50, 100 and 400 tokens, twice over. Static batching holds every slot until the 400-token request ends, so three slots sit idle (dashed) and the second batch cannot start before step 400. Continuous batching refills a slot on the step it frees: the short requests cycle through slots 1 and 2, the second 400-token request starts at step 90, and everything is done by step 490. Slot use rises from 1,140 of 3,200 slot-steps (35.6%) to 1,140 of 1,960 (58.2%). It is not 100% only because this toy queue runs dry: after step 150 nothing is waiting, so two slots idle while the 400-token requests finish. Under steady traffic a freed slot is refilled at once.

Batching strategies: cost and trade-offs

Figures from the worked example above (Llama 3 8B in bf16 on one H100 SXM, 2,000-token contexts) where they apply. Here Time is what one user feels and Space is GPU memory.

CaseTimeSpaceWhy
Batch 1At least 4.9 ms per token, the fastest a user can getWeights plus one cache205 tokens per second: the GPU spends each step waiting on memory, with compute to spare.
Static batchEvery request waits for the longest: mean finish 342.5 steps in the exampleEvery cache held until the last request endsFine for offline jobs of similar length; wasteful when lengths vary.
Continuous, batch 32At least 7.3 ms per token, 137 tokens per second per user8.39 GB of cache on top of 16.06 GB of weights4,385 tokens per second in total, about 21 times batch 1, so about 1/21 of the cost per token on the same GPU.
Batch at the memory limitKeeps rising as cache reads growAn 80 GB card minus 16.06 GB of weights fits at most about 243 caches of 2,000 tokens, before activations and overheadBeyond the latency budget, the extra throughput is no use to an interactive user.
Chunked prefillA long prompt’s first token arrives a little laterNothing extraRunning decodes stop stalling behind big prompts, so time per output token stays steady.

Two clocks again: the time column is per user, the last column is per GPU. Most knobs trade one for the other. Continuous batching is the one row that improves both, because it removes idle slots instead of moving along the curve.

Where this shows up in production and interviews

  • Serving engines

    vLLM, Hugging Face’s Text Generation Inference and NVIDIA’s TensorRT-LLM all schedule this way; TensorRT-LLM calls it in-flight batching. vLLM pairs it with PagedAttention, which stores each KV cache in fixed-size blocks, so memory freed by a finished request can go straight to a new one.

  • Batch APIs

    Model providers sell the far end of the trade-off: submit requests in bulk, get results back asynchronously, commonly at half the price. With no latency target to meet, the provider can run them at whatever batch keeps its GPUs fullest.

  • Classic model serving

    Models that answer in one forward pass, such as image classifiers and embedding models, have no decode loop, so no request holds a slot for hundreds of steps. Servers like NVIDIA Triton use dynamic batching instead: wait a few milliseconds to gather requests, then run them as one batch.

  • System design interviews

    “Design an LLM serving system” comes down to this page’s arithmetic. Memory per sequence sets the largest batch, bandwidth sets the time per step, and the latency target picks a batch between them. Name continuous batching, a paged KV cache and chunked prefill, then size them with numbers.

Continuous batching, step by step

What the scheduler in a serving engine does on every iteration, the arithmetic that sets a decode step's speed, and a static-versus-continuous run worked by hand.

One scheduler iteration

  1. Admit. While a slot and enough KV-cache memory are free, take the next waiting request and run its prompt through the model (prefill), in chunks if the prompt is long.
  2. Step. Run one forward pass over every running sequence; each one gains exactly one token.
  3. Retire. Any sequence that emitted its end token or hit its length cap leaves now, freeing its slot and its cache.
  4. Protect memory. If the cache would overflow, preempt the newest sequence and requeue it rather than crash the batch.

Static batching skips steps 1 and 3 until the whole batch is done, so its slot utilization is Σ Lᵢ / (B · max Lᵢ) for output lengths Lᵢ and batch size B. In the memory-bound regime a decode step can be no faster than (weight bytes + KV bytes read) / bandwidth; throughput is B divided by that time, and each request's own speed is one token per step.

Worked example

  1. Static. Four slots, eight requests with output lengths 20, 50, 100, 400, 20, 50, 100, 400. The first batch holds all four slots for 400 steps but fills only 570 of its 1,600 slot-steps, 35.6%. The second batch runs from step 400 to 800. Mean completion: 342.5 steps.
  2. Continuous. The 20-token request frees its slot at step 20 and the next request starts there at once. Completions come at steps 20, 40, 50, 90, 100, 150, 400 and 490: a mean of 167.5 steps, half the latency, with everything done in 490 steps instead of 800.
  3. A real step. Llama 3 8B in bf16 is 16.06 GB of weights. At 3.35 TB/s (an H100 SXM) one step takes at least 4.9 ms at batch 1 with a 2,000-token context: about 205 tokens per second.
  4. Batch 32 at 2,000 tokens each adds 8.39 GB of cache reads (128 KiB per token): at least 7.3 ms per step, 4,385 tokens per second in total but 137 per request.
  5. Long contexts erode the deal: at 8,000 tokens the same batch reads 33.6 GB of cache per step, twice the weights, and drops to 2,161 tokens per second.
  6. The widget's Act 2 is a teaching model with the same shape: at its defaults, batch 16 gives 660 tokens per second at 32 ms per token, and the largest batch inside the 40 ms budget is 25, at 904 tokens per second.

Knobs and pitfalls

Engines expose a cap on concurrent sequences and on tokens processed per step (in vLLM, max_num_seqs and max_num_batched_tokens). A long prompt's prefill can stall every running decode, so chunked prefill splits it across iterations. Report time to first token and time per output token at p50 and p99, not throughput alone, and watch for preemption churn when caches run full. For offline jobs, push the batch as high as memory allows.

Check yourself

Four questions. Batch your best guesses.

See every answer, with the reasoning
  1. Why does batching raise a GPU’s throughput so much?

    Answer: Decode is memory-bound, so many requests ride one weight-streaming pass nearly for free — Stream the weights once, advance many sequences — the spare compute was there all along.

  2. What happens to per-request latency as batch size grows?

    Answer: It rises — each request waits to be gathered and shares the pass — Throughput and latency move opposite ways; you tune batch to a latency target.

  3. Why is static batching wasteful?

    Answer: Short requests finish and their slots idle until the batch’s slowest request completes — The batch runs at the pace of its slowest member; freed slots sit idle.

  4. What does continuous (in-flight) batching do?

    Answer: Evicts a finished request instantly and admits a waiting one, keeping the GPU full — No padding for stragglers → high utilization → several-fold more throughput. vLLM’s default.

Finished this one? 0 / 61 Labs done
NextThe Rounding Room: Quantization Next on the “Fast, and cheap” ladder · rung 3 of 5

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs