Overview
Curated: · Written: · Reviewed:
Model Serving & Inference Infrastructure
In one line
Serving gets predictions out of a trained model at a p99 latency and cost the product can live with. For LLMs the work is budgeting KV cache memory and scheduling prefill and decode separately. Reach for continuous batching, paged KV and admission control when concurrency times context approaches the KV budget. For low request rates on a small model, one replica with simple batching is cheaper and easier to run.
Key definitions
- TTFT: arrival to first output token. Queue wait plus prefill.
- TPOT / ITL: mean gap between output tokens after the first. Set by decode step time.
- KV cache: stored key and value projections for every processed token, reused on each decode step.
- Continuous batching: the scheduler evicts finished sequences and admits waiting ones at every decode step, instead of running a fixed batch to completion.
- Chunked prefill: splits a long prompt into chunks interleaved with decode steps, so ITL stays predictable.
- Admission control: bounds accepted requests and prompt length by the KV budget.
Formulas and complexity
Here L is layers, H_kv is KV heads, d_head is head dimension, and b is bytes per element. Llama-2-7B (MHA, fp16): 2 x 32 x 32 x 128 x 2 = 512 KiB per token. Llama-3-8B (GQA, 8 KV heads): 128 KiB per token.
B is batch size, T is service latency for one batch, and lambda is arrival rate. Filling a batch always takes at least as long as serving it.
How it works
- Prefill processes the whole prompt in one compute-bound pass and emits the first token, which sets TTFT.
- Each decode step emits one token per sequence and reads all weights and the KV cache, so it is bandwidth-bound and sets TPOT.
- The scheduler admits and evicts sequences at every step, so no slot waits on a long generation.
- Paged KV allocates 16-token blocks on demand (vLLM default) and reuses freed blocks, which keeps waste low.
- When a request does not fit, the levers are preempt or swap, recompute, shrink max_num_seqs or max_model_len, fp8 KV, or reject.
When to use and when not to
| Situation | Choose | Why |
|---|---|---|
| Low rate, small model, strict deadline | One replica, simple dynamic batching | Continuous batching and paged KV add overhead you do not need |
| High concurrency, long context | Continuous batching, paged KV, admission control | KV cache is the binding constraint |
| Long prompts stall decode | Chunked prefill or a separate prefill pool | Head-of-line blocking raises ITL |
| Work tolerates delay | Asynchronous or batch inference | Cheaper than an always-on low-latency endpoint |
| Freshness allows scheduled output | Batch | No continuously running endpoint |
| Strict SLO with a large model | Retained warm capacity | Scale-to-zero pays model load time on the request path |
Traps interviewers probe
- High GPU utilization means good serving. A saturated GPU at batch 64 can have worse p99 than a half-idle one at batch 16. Fix: report p99 at the operating rate and split queue wait from compute time.
- Benchmark throughput equals production capacity. Uniform prompt lengths overstate batching. Fix: load test with the real length distribution and measure sustainable throughput at the latency objective, not peak throughput under saturation.
- A bigger batch always helps. Filling a batch takes at least as long as serving it, and at low arrival rates a large batch never fills. Fix: check that B / lambda is at least T and that capacity B / T exceeds arrivals.
- Quantization is free. Loss concentrates in rare slices. Fix: accept or reject per slice against the full-precision baseline, not on the aggregate.
- Autoscale on CPU. CPU stays low while the GPU saturates. Fix: scale on in-flight requests, queue depth or accelerator signals, and keep warm capacity for strict SLOs.
Interview tips
Opening: Prefill is compute-bound and sets TTFT. Decode is bandwidth-bound and sets TPOT. The KV cache is the budget that decides concurrency.
Follow-ups to expect:
- How do you size the KV cache for a model and GPU? Give the formula, then concurrency at the target context.
- What happens when a request does not fit? Preempt, swap, recompute, shrink limits, or reject.
- Why is TTFT high while TPOT is fine? Queue wait or prefill, not decode.
- Why is TPOT high while TTFT is fine? Batch too large, KV too long, or prefill interleaving.
- How would you serve a 128K-context model? Chunked prefill, paged KV, likely disaggregation, and admission control.
30-second answer outline:
- Name the two phases and their bottlenecks.
- Give the KV-per-token formula and the concurrency at the target context, and say which constraint binds.
- Choose continuous batching for generation and bounded queues with admission control.
- Split queue wait from compute, and state p99 TTFT and TPOT targets.
- Cover failure: deadlines, a fallback that never invents a default prediction, and readiness only after the correct version is loaded and warm.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.