KatInterview

Design a Streaming LLM Output Architecture

System DesignModel answer
HardModel answerSystem Designsystem-designstreamingllm

Last updated

Design a scalable backend architecture for real-time AI token streaming featuring resume capability, low latency, prompt cancellation, and fault isolation.

Scenario

You are tasked with building the backend architecture for a high-concurrency generative AI application (such as an interactive document canvas or code generation platform). When a user submits a prompt, the platform must stream the model's text response back to the browser in real time.

Users must be able to interrupt generation at any moment, edit earlier steps to create branching chat threads, and automatically reconnect to active streams if their device loses network connectivity.

Requirements

Functional Requirements

  • Low-Latency Streaming: Deliver generated output chunks to the web client with minimal delay as they are emitted by model inference nodes.
  • Immediate Cancellation: Abort inference runs upstream as soon as a user clicks "Stop", preventing wasteful GPU utilization.
  • Connection Recovery & Resume: If a client disconnects mid-generation, it must be able to reconnect and seamlessly pick up output from the exact index it last saw.
  • Thread & State Persistence: Store incomplete and completed user/assistant interactions securely, ensuring state consistency across cancellations, crashes, and branches.

Scale & Non-Functional Constraints

  • Scale: 10 million Daily Active Users (DAU), up to 150,000 active concurrent streams at peak output.
  • Latency Targets: Time-to-First-Token (TTFT) $p_{95} < 800\text{ms}$; Inter-token gap $p_{95} < 50\text{ms}$.
  • Availability: 99.99% operational SLA for retrieving past conversation threads.
  • Cost & Efficiency: Hard upper bound of 200ms to propagate cancellation commands from the client to the active GPU inference process.

Discussion Points

  1. Protocols & Contracts: Compare Server-Sent Events (SSE), WebSockets, and chunked HTTP transfer encoding. Draft the API schemas for both client-gateway communication and gateway-orchestrator-worker pipelines.
  2. System Architecture: Map out the end-to-end topology from reverse proxies and edge gateways down to session buffers, orchestration layers, and storage engines.
  3. Data Schema & Ephemeral State: Plan the storage models for persistent conversations, message nodes, and active streaming tokens.
  4. Reliability & Edge-Case Handling: Detail recovery protocols for dropped connections, worker node crashes, and request deduplication.
  5. Observability: Highlight core operational metrics needed to monitor latency degradation, token rates, and stream lifecycle health.

Hints

Hint 1

Decouple the lifetime of the inference stream from the client connection lifetime.

Hint 2

Use sequence numbers to turn stream delivery into an append-only log, enabling effortless resume using headers like Last-Event-ID.

Hint 3

Propagate cancellation signal via gRPC context cancellation or Pub/Sub rather than waiting for HTTP connection timeouts.