KatInterview

Architecting a Streaming LLM Chat Backend at Scale

System DesignModel answer
MediumModel answerSystem Designllm-servingkv-cachecontinuous-batching

Last updated

Design a backend that streams model-generated tokens to many concurrent users, covering the API, GPU inference fleet, KV-cache memory management, autoscaling and multi-turn session state.

Build a Streaming Chat Backend for a Large Language Model

You are asked to design the server side of a conversational assistant. Users type a prompt, and the system streams the reply back token by token. A very large number of people will be chatting at once, and the experience should feel responsive.

What the service must do

  1. Accept a user message and stream the generated reply back incrementally.
  2. Remember earlier turns so that a conversation can continue across many requests.

Qualities it must have

  1. A short wait before the first token appears (time-to-first-token) and a fast, steady token rate afterwards.
  2. High availability and graceful behavior when machines fail.
  3. Careful use of accelerator memory for long context windows.

Cover the following in your answer

  • Interface: how a client starts, consumes, resumes and cancels a streamed reply.
  • Components: load balancing, gateway, orchestration, routing, inference workers and where the attention cache lives.
  • Growth and bursts: how the fleet reacts to traffic spikes and how GPUs are allocated and shared.
  • Memory and throughput techniques: for example paged cache allocation and continuous batching, and why they matter.
  • Conversation state: how a follow-up message behaves if it lands on a different machine than the previous turn.

State your assumptions (user counts, prompt and reply lengths, model size, latency goals) and back them with rough numbers.

Hints

Hint 1

Work out the per-token attention cache size for a large model (layers x KV heads x head dim x 2 x bytes). It shows memory, not raw compute, limits how many conversations fit on a node.

Hint 2

Compare static batching with batching at every decoding step, and contrast reserving one contiguous max-length buffer per request with allocating small fixed-size blocks on demand.

Hint 3

Separate durable state (the conversation database) from soft state (the cache). If the cache is only an optimization, losing it costs recomputation but never correctness, which simplifies routing and failover.