Serving a 70B LLM to Thousands of Concurrent Streaming Users
System DesignModel answerLast updated
Design a multi-GPU inference platform for a 70B model that streams tokens with low first-token latency, keeps GPUs busy, manages KV cache memory, and survives traffic spikes.
Build an inference platform for a large language model
You are asked to design the backend that serves a roughly 70-billion-parameter language model to thousands of users at the same time. Some users are in interactive chat sessions and expect tokens to appear as they are generated. Others submit large offline jobs and care only about total throughput.
What the platform must do
- Stream generated tokens back to the client incrementally (server-sent events).
- Expose an API for submitting generation requests with tunable settings such as maximum output length and sampling temperature.
- Host several model versions at once, and let operators move traffic to a new version atomically.
Qualities to optimize
- Keep time to first token (TTFT) as low as possible.
- Keep expensive GPUs as busy as possible.
- Absorb sudden load surges without falling over.
Given facts
- Inference runs on a fleet of machines with large-memory GPUs.
- The attention key/value cache (KV cache) is the main memory pressure point.
- The weights do not fit on one GPU, so the model must be split across devices with tensor and/or pipeline parallelism.
What to cover in your answer
- Architecture: how a request is routed, queued, and executed.
- Scheduling and batching: how you would run continuous batching when requests have very different prompt and output lengths.
- Memory: how you would manage KV cache to avoid fragmentation and out-of-memory failures.
- Scaling: how the fleet should grow and shrink around peak hours and bursts.
State your assumptions about load and latency targets, and justify them with rough capacity math.
Hints
Hint 1
Start from hardware arithmetic: weight size in bytes, KV cache bytes per token (layers x KV heads x head dim x 2 x precision), and how many concurrent sequences fit in the memory left over after weights.
Hint 2
Static batches waste capacity because short requests wait for long ones. Think about rebuilding the batch at every decode step, and about how a long prompt (prefill) can stall everyone else's token generation.
Hint 3
Treat KV memory like OS virtual memory: fixed-size blocks, a per-request block table, and reference counting for shared prefixes. For spikes and updates, think about preemptible batch work, admission control, warm capacity, and pinning each request to one model version.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Model answer
1. Assumptions and capacity math
I would state the workload first, then derive the fleet from hardware limits.
- Load: about 5,000 concurrent streams at peak, a 3x day/night swing, and bursts of 2x within minutes. Average prompt is about 2K tokens, average output about 500, and the context cap is 8K.
- SLOs: interactive TTFT p99 under 1 s and inter-token latency (ITL) under 50 ms. Batch jobs only need to finish within hours.
- Weights: 70B parameters in FP16 is about 140 GB. That needs tensor parallelism, for example TP=8 on one 8x80 GB node (640 GB total).
- KV cache per token: 2 (K and V) x 80 layers x 8 KV heads (grouped-query attention) x 128 dims x 2 bytes, about 320 KB.
- Capacity per node: roughly 450 GB remains after weights and activations, which is about 1.4M tokens of KV. At about 2.5K tokens per sequence, that is 400 to 500 sequences. Latency targets usually cap it nearer 200.
- Decode is memory-bandwidth bound: every step streams the weights from HBM, so a step has a floor of a few milliseconds and realistically 20 to 40 ms. Adding more sequences to a step is almost free until KV reads dominate. This is why big batches are the main lever for utilization.
- Fleet size: 5,000 / 200 is about 25 nodes. With headroom and redundancy, plan for 35 to 40.
2. API and control state
POST /v1/completionswith model (name or alias), prompt, max_tokens, temperature, top_p, stop sequences, stream flag, and a priority ofinteractiveorbatch. The response is an SSE stream ending in a finish reason and usage counts.POST /v1/batchestakes an input file location and returns a job id. Results are written to object storage.- A model registry maps alias to version to deployment details (weights location, parallelism layout, node pool). A node registry tracks health, loaded version, free KV blocks, queue depth, and cached prefixes, pushed to routers every ~100 ms.
3. Architecture
Client, then an API gateway (auth, rate limits, token quotas), then a per-version router, then an 8-GPU replica. Each replica runs an engine with a scheduler, a paged KV manager, and captured CUDA graphs. A CPU-side detokenizer and streamer sends tokens back, so the GPU loop never blocks on network I/O. Offline jobs go through a durable queue and fill spare capacity.
Routing scores replicas by free KV blocks, queued prefill tokens, and prefix-cache affinity. Sending requests that share a system prompt to the same replica lets them skip prefill, which directly lowers TTFT.
Queuing is two-level: a short queue at the router for backpressure, and a local queue in each engine that owns fine-grained scheduling.
4. Continuous batching
Static batching holds a slot until the slowest sequence finishes. Instead, the engine re-forms the batch every iteration:
loop every step:
for seq in running:
if seq.finished(): free_blocks(seq); remove(seq)
while waiting and free_blocks >= blocks_for(waiting.head.prompt) + watermark
and token_budget_left:
admit(waiting.pop())
run_forward(running + admitted_prefill_chunks) # ragged, no padding
emit_tokens()
Prefill versus decode interference. A long prompt is compute-bound and can stall all decoders for 100+ ms. Two fixes:
- Chunked prefill: split prompts into ~512-token chunks and mix one chunk with all decodes under a fixed per-step token budget (say 2,048). This bounds ITL.
- Disaggregation at larger scale: prefill nodes compute the KV and ship it over RDMA to decode nodes. Each pool is sized and tuned separately.
Throughput versus latency. Do not wait to fill a batch, because new requests join at the next step. The real knob is the maximum token budget or batch size: bigger raises throughput but lengthens steps and ITL. Derive it from the ITL SLO. Run batch-priority work in leftover capacity or on a separate pool with a larger budget.
5. KV cache management
Reserving max_context x 320 KB (about 2.6 GB at 8K) contiguously per request wastes memory in two ways: internal fragmentation (most requests stop early) and external fragmentation (holes between allocations). Reported waste is often 60 to 80%.
Paged KV cache solves this:
- Pre-allocate the whole KV region at startup as fixed blocks (e.g., 16 tokens).
- Each sequence has a block table mapping logical to physical blocks, and the attention kernel gathers through it.
- Allocate blocks on demand as tokens are produced. Waste is under one block per sequence and there is no external fragmentation.
- Reference counting and copy-on-write let shared prefixes, parallel samples, and beam search share blocks. A prefix cache keeps hashed blocks of finished requests in an LRU for reuse.
Avoiding OOM: no allocation in the hot path, and admission is gated on free blocks plus a watermark. If blocks run out mid-decode, preempt a low-priority sequence, either by swapping its blocks to host memory or by dropping them and recomputing later. Prefer evicting batch work, then the most recently admitted sequences.
6. Scaling, spikes, and updates
- Autoscale on leading indicators: queue depth, KV utilization, and TTFT p95, not CPU. Cold starts are slow because 140 GB of weights must load, so keep weights on local NVMe or a peer cache, maintain a warm pool, and pre-scale on the diurnal schedule.
- Spike handling, cheapest first: (1) preempt or pause batch work; (2) admission control with HTTP 429 and Retry-After per tenant quota; (3) degrade gracefully with lower max_tokens or a smaller model if the product allows.
- Atomic version updates: bring up the new pool, verify with golden prompts, canary traffic (1%, 10%, 100%), then flip the alias. Each request is pinned to the version resolved at admission, so in-flight requests finish on the old version and no request mixes versions. Drain the old pool afterward.
7. Failure handling and monitoring
- A node failure kills its in-flight streams. The gateway can retry from scratch for non-streamed calls, or resume by resubmitting prompt plus tokens already sent (prefix cache makes this cheap).
- Track TTFT, ITL, tokens/s per GPU, KV block utilization, preemption rate, and prefix-cache hit rate.
Common mistakes
- Using CPU or request count for autoscaling instead of KV and queue signals.
- Waiting to "fill a batch" instead of iteration-level admission.
- Ignoring prefill stalls on ITL.
- Contiguous max-length KV allocation.
- Forgetting cold-start time for large weights.
- Letting a request switch model versions mid-stream.