System Design AI

WebSockets in System Design Interviews: When to Use It and What to Say

WebSockets are a protocol that upgrades an HTTP connection to a persistent, full-duplex channel, letting the client and server push data to each other at any time.

The Problem WebSockets Solve

Traditional request-response HTTP works great for CRUD operations, but it forces the client to poll for updates. In a real-time chat app, for example, polling every second wastes bandwidth and adds latency. The core problem is how to deliver timely updates from server to client without the client constantly asking.

A Concrete System: Real-Time Collaborative Document Editing

Imagine a Google-Docs-like editor where multiple users see each other's keystrokes instantly. The requirements are:

  • Sub-second latency for each edit.
  • Hundreds of concurrent users per document.
  • Ability to push updates to all participants.

A naive solution would be HTTP polling every 500 ms, but that is 120 requests per minute per user, and tens of thousands per minute for one document with hundreds of users, loading the backend and increasing cost. WebSockets give a single, long-lived connection per user, allowing the server to push edits the moment they arrive.

How WebSockets Work – Step by Step

  1. Handshake – The client sends an HTTP GET /chat with an Upgrade: websocket header.
  2. Server Response – The server validates the request and replies with 101 Switching Protocols; from then on the same TCP connection carries WebSocket frames instead of HTTP.
  3. Framing – Data is wrapped in lightweight frames. Each frame carries a small header (opcode, mask, length) and the payload.
  4. Full-Duplex – Either side can now send frames at any time. No more request-response cycles.
  5. Close – Either side can send a close frame to gracefully terminate the connection.

Small Worked Example

  • User A opens the editor. Browser initiates the handshake to wss://edit.example.com. The server accepts and returns 101.
  • The connection stays open. When A types "hello", the client sends a text frame containing the delta.
  • The server receives the frame, updates the document state, and broadcasts the same delta to User B and C via their open WebSocket connections.
  • Users B and C receive the frame immediately and render the new text.
  • When the user leaves, the client sends a close frame; the server cleans up the connection.

Classic Design Questions Where WebSockets Appear

Design QuestionWhere WebSockets FitReason for Use
Live Chat / MessagingBetween the chat service and each clientPush new messages instantly without polling
Real-Time DashboardsFrom analytics backend to the browserStream metrics as they are computed
Multiplayer Game ServerBetween game server and each playerSend state updates (positions, actions) with sub-second latency

In each of these, the WebSocket layer sits right after the API gateway or load balancer, feeding directly into the service that generates events.

Trade-offs and the Alternative Interviewer Will Probe

AspectWebSocketsHTTP Long-Polling / Server-Sent Events
Connection OverheadOne TCP connection per client; keeps sockets open, consuming file descriptors.Also one open connection per client; long-polling adds a new request after every response.
LatencyNear-instantaneous, as data is pushed.Polling interval adds latency; SSE is one-way only.
ScalabilityRequires load balancers that support the upgrade and long idle timeouts, a registry of which server holds each connection, and often a message broker (Kafka, Redis).Easier to scale with stateless HTTP servers; can use CDN caching.
ComplexityNeed to handle heartbeats, reconnection, ordering, and back-pressure.Simpler client logic; server just responds to requests.
Browser SupportWidely supported; fallback to polyfills if needed.SSE not supported in older IE; long-polling works everywhere.

Interviewers often ask: "What if we need to support millions of concurrent users?" Expect to discuss sharding connections across multiple gateway instances, using a publish-subscribe broker, and limiting idle connections with timeouts.

30-Second Pitch

"WebSockets upgrade an HTTP request to a persistent, full-duplex channel, letting the server push updates instantly. In a real-time collaborative editor, they replace costly polling with a single open socket per user, delivering sub-second latency. The trade-off is connection overhead and scaling complexity, so you need a registry of which gateway holds each connection and a message broker. If the interview asks for alternatives, compare against long-polling or Server-Sent Events, highlighting latency and scalability differences."

Common Mistakes Candidates Make

  • Treating WebSockets as a magic bullet – Claiming they solve all real-time problems without mentioning the need for connection management and scaling.
  • Skipping the handshake detail – Jumping straight to “persistent connection” and ignoring the HTTP upgrade step, which shows a gap in protocol knowledge.
  • Ignoring fallback strategies – Not mentioning how to handle browsers that cannot open a WebSocket, or what happens when a connection drops.
  • Over-estimating capacity – Saying a single server can hold millions of sockets without discussing OS limits or load-balancer constraints.
  • Missing security concerns – Forgetting to note that WebSocket connections inherit the same TLS security as HTTPS, and that authentication must be done before the upgrade.

How to Practice This

  1. Study the worked design for a WebSocket gateway and session registry in the System Design AI library.
  2. Draw your own diagram on the canvas: include the client, load balancer, WebSocket server, message broker, and storage layer.
  3. Rehearse the 30-second pitch until you can deliver it without notes, then answer follow-up questions about scaling and alternatives.

Questions people ask

When should I choose WebSockets over HTTP polling?

Use WebSockets when you need low latency, bi-directional communication and the client expects frequent updates, such as chat, live dashboards, or multiplayer games.

How do I scale WebSocket connections to millions of users?

Distribute connections across multiple load-balanced gateway instances, keep a registry of which gateway holds each connection, and funnel messages through a publish-subscribe system like Redis Streams or Kafka.

What are the main security considerations for WebSockets?

Authenticate before the upgrade, enforce TLS (wss://), validate origin headers, and apply rate limiting to prevent DoS attacks.

Can WebSockets work with serverless architectures?

Serverless functions are short-lived, so they don’t hold persistent sockets. You can still use WebSockets by routing the connection through a managed service (e.g., API Gateway WebSocket support) that forwards messages to Lambda for processing.