Interview Study Guide

Guide / AI engineering Fundamentals

LLM serving: batching, speculative decoding, GPUs and capacity

How inference servers (vLLM, TGI, TensorRT-LLM) reach high throughput: continuous batching, PagedAttention, prefix caching, speculative decoding, parallelism, GPU sizing and autoscaling.

Created · Updated · 6 min read

Hosting a model is a capacity and latency engineering problem. The goal: maximum tokens per second per dollar while meeting TTFT and inter-token latency targets.

Fundamentals#

V.1 Metrics#

MetricMeaningDriven by
TTFTTime to first tokenQueue wait + prefill (prompt length)
ITL / TPOTInter-token latency / time per output tokenDecode step time, batch size
ThroughputTokens/s across all requestsBatch size, hardware, quantization
GoodputRequests meeting the SLO per secondThe real target
Cost per 1M tokensGPU $/h ÷ tokens per hourUtilisation

Typical SLO: TTFT < 1 s, ITL < 50 ms (a readable stream is ~20 tokens/s).

V.2 Serving techniques#

  • Continuous (in-flight) batching: instead of waiting for a batch to finish, the server adds new requests and removes finished ones at every decode step. Eliminates idle GPU time from variable output lengths; often several× throughput over static batching.
  • PagedAttention (vLLM): stores the KV cache in fixed-size blocks like virtual memory pages, eliminating fragmentation and allowing sharing; raises the number of concurrent sequences.
  • Prefix caching: reuse the KV cache of a shared prefix (system prompt, few-shot examples, document). Cuts prefill cost and TTFT for repeated prefixes; put stable content first (Prompting).
  • Chunked prefill: split long prompts so they interleave with decode steps and do not stall other users’ streaming.
  • Speculative decoding: a small draft model proposes several tokens; the large model verifies them in one pass and accepts the matching prefix. Same output distribution, 1.5–3× faster decode when acceptance is high. Variants: Medusa heads, EAGLE, n-gram/prompt lookup for copy-heavy tasks.
  • Quantization: FP8/INT8/INT4 for weights and KV (Transformer internals).
  • Kernel fusion and CUDA graphs (TensorRT-LLM, FlashInfer) to cut overhead.
  • Disaggregated prefill/decode: separate GPU pools for compute-bound prefill and memory-bound decode, transferring the KV cache between them; improves utilisation at scale.
  • Structured/constrained decoding: grammar-guided generation to guarantee valid JSON (Prompting) with small overhead.

V.3 Parallelism for big models#

TypeSplitsUse
Tensor parallelismEach layer’s matrices across GPUs in a nodeModel too big for one GPU; needs fast interconnect (NVLink)
Pipeline parallelismLayer groups across GPUs/nodesVery large models across nodes; adds pipeline bubbles
Data parallelism / replicasCopies of the modelScale throughput; the usual way to scale out
Expert parallelismMoE experts across GPUsMoE models

Prefer the smallest tensor-parallel degree that fits the model and KV cache, then scale with replicas.

V.4 Sizing example#

Goal: serve an ~8B model (fp16) to chat users at peak 50 requests/s, average 500 prompt + 300 output tokens.

  • Tokens: input 25K tokens/s, output 15K tokens/s.
  • One H100 with vLLM and continuous batching might sustain, say, ~2,500–5,000 output tokens/s at acceptable ITL for this size (assumption: measure on your workload). Output demand 15K tokens/s → about 3–6 GPUs, plus headroom 30% and N+1 → ~6–8 GPUs.
  • KV memory check: 80 GB − 16 GB weights − overhead ≈ 55 GB of cache ÷ (128 KB × 800 tokens ≈ 100 MB per request) ≈ 500 concurrent requests per GPU: not the limit here; compute/bandwidth is.

Always benchmark with production-shaped traffic (prompt/output length distribution, concurrency, prefix reuse); vendor numbers are best case.

V.5 Operating it#

  • Autoscaling on queue depth/in-flight requests or GPU KV-cache utilisation, not CPU. GPU nodes take minutes to start (image pull, model load of tens of GB), so keep warm capacity, cache weights on local NVMe or shared volumes, and load-balance with least outstanding requests or prefix-aware routing (route requests with the same prefix to the same replica to hit its cache).
  • Admission control and priorities: reject or queue with deadlines; separate pools for interactive and batch; per-tenant token limits (Rate limiting).
  • Reliability: health checks that run a tiny generation, graceful drain on deploy (finish streams), fallback to another model or provider when overloaded (LLMOps gateway).
  • Observability: TTFT, ITL, queue time, tokens/s, KV utilisation, preemptions, GPU utilisation and memory, error and timeout rates, cost per request.
  • Managed vs self-hosted: API providers give zero ops and the best frontier models; self-hosting makes sense for data residency, high steady volume, custom or fine-tuned models, or latency control. Break-even requires high, stable utilisation; idle GPUs burn money.

Most asked questions#

1. What is continuous batching? Scheduling at the token level: finished sequences leave and new ones join each step, so the GPU stays full despite variable lengths.

2. How does speculative decoding speed things up? A cheap draft proposes k tokens, the big model verifies them in parallel, accepting the longest correct prefix; the output distribution is unchanged.

3. How do you reduce TTFT? Shorter prompts, prefix caching, chunked prefill, more prefill capacity, less queuing (autoscaling, admission control), faster/quantised model.

4. How do you scale an LLM service? Replicas behind a load balancer using least outstanding requests, autoscale on queue depth, keep warm capacity, use tensor parallelism only when needed.

5. Why is GPU memory the constraint? Weights plus KV cache must fit; concurrency is limited by KV memory.

6. Self-host or use an API? API for speed to market and frontier quality; self-host for scale economics, privacy, customisation and control, after measuring utilisation.

Senior-level questions#

1. “p99 TTFT spikes when a few users send 30K-token prompts.” Long prefills block the batch: chunked prefill, separate long-context pool, per-request token caps, prioritisation, and queue limits; route long prompts to dedicated replicas.

2. “GPU utilisation is 30% but latency is high.” Queuing from poor batching config, CPU-side tokenization or scheduling bottlenecks, network/streaming overhead, small max batch, KV-bound preemptions, or bursty traffic; profile and tune max_num_seqs, batch tokens, and add replicas.

3. “How would you roll out a new model version safely?” Shadow traffic, canary by percentage with eval-based quality metrics and latency checks, per-tenant opt-in, automatic rollback (Evals).

4. “Cost per million tokens is double the API price. Why?” Low utilisation (idle capacity for peaks), no batching optimisation, over-provisioned GPUs, small batches, wasteful long contexts; consider autoscaling to zero for batch jobs, spot GPUs, quantization, or just use the API.

5. “Prefix-aware routing: what is the catch?” It concentrates load on replicas holding popular prefixes (hot spots); need load-aware fallbacks and prefix replication.

Quiz#

1. Why is static batching wasteful for LLMs?

Sequences finish at different times, leaving GPU slots idle until the longest finishes.

2. What does PagedAttention fix?

KV cache fragmentation and over-reservation, allowing more concurrent sequences per GPU.

3. Which latency does prefix caching mainly improve?

TTFT (and prefill cost) for requests sharing a prefix.

4. What should autoscaling be based on for GPU LLM replicas?

Queue depth, in-flight requests or KV-cache utilisation, not CPU.

5. When use tensor parallelism?

When the model plus KV cache does not fit on one GPU (or to cut latency), preferring the smallest degree and NVLink-connected GPUs.

Finished this page?Mark it done to track your progress across the guide.
Pritam Ghosh · System design & AI engineering study notes