KatInterview

Browser-Only Streaming Chat App with a Stateless Relay

System DesignModel answer
MediumModel answerSystem Designsystem-designstreamingsse

Last updated

Design a streaming AI chat product where conversation content lives only in the browser, a stateless relay protects provider keys, and refresh begins a fresh chat.

You are asked to design a conversational AI assistant for the web, with the client side as the main focus. The product must obey these rules:

  1. Messages and conversation history exist only inside the user's browser. No server-side database may hold them.
  2. Replies from the assistant must appear incrementally, token by token.
  3. Reloading the page begins an entirely new conversation. Nothing is restored.
  4. Users must be able to sign in, and the client must handle their credentials without ever exposing the secrets used to call the model provider.

Walk through your design and make sure it covers:

  • Architecture and data flow: the browser app, an edge or relay layer, and the upstream model provider, plus the full path of one chat turn.
  • Streaming: how tokens are delivered to the UI, and the trade-offs between Server-Sent Events and WebSockets.
  • Session handling without a database: where conversation state and auth tokens live, and how a reload resets the conversation.
  • Security: sign-in flow (OAuth/OIDC with PKCE), token lifetimes and scopes, protection of provider keys, and CORS, CSP and XSS concerns.
  • Rate limiting and abuse control when there is no durable server-side store.
  • Failure handling and retries: dropped connections mid-stream, 429 and 5xx responses, timeouts, expired tokens, and histories that grow too large for the context window.
  • Trade-offs of the no-database approach: privacy and cost benefits versus lost continuity, bigger request payloads, and weaker analytics or personalization.

State any scale assumptions you make and justify the choices you consider most important.

Hints

Hint 1

Separate the conversation state (memory only, wiped on reload) from the authentication state (which may survive a reload so users are not forced to sign in again).

Hint 2

Native EventSource cannot send POST bodies or Authorization headers. Think about what you would use to read an event stream from a POST request, and what must happen if an error occurs after the 200 status has already been sent.

Hint 3

No database does not mean no counters. Consider short-lived, expendable rate-limit state, claims embedded in short-lived tokens, per-user concurrent stream caps, and hard limits on body size and output length.

Hint 4

Because the client resends the full history on each turn, think about how token cost grows with conversation length and how you would trim or summarize history on the client.