WebSockets
WebSockets power real-time features: chat applications, live notifications, stock tickers, collaborative editing, and online gaming.
Quick answer: WebSockets
WebSockets is covered here as a practical system design concept. WebSockets power real-time features: chat applications, live notifications, stock tickers, collaborative editing, and online gaming. Use it to understand the APIs idea, where it appears in production systems, and how to discuss it clearly in interviews.
Focus on the problem being solved, the main design choice, the failure mode, and the tradeoff you would defend in front of a senior engineer. This page is part of the DosiBridge system design knowledge graph, so it links related concepts, interview problems, tradeoffs, and references instead of leaving the topic isolated.
WebSocket is a communication protocol that provides full-duplex, persistent connections between a client and server over a single TCP connection. Unlike HTTP (request-response), WebSocket allows both sides to send data at any time without the overhead of establishing a new connection for each message.
Why This Matters
WebSockets power real-time features: chat applications, live notifications, stock tickers, collaborative editing, and online gaming. Understanding when to use WebSockets vs HTTP polling vs SSE is a key system design decision.
The Building Blocks
- Full-duplex: Both client and server can send messages independently at any time.
- Persistent connection: After the initial HTTP upgrade handshake, the connection stays open. No repeated TCP/TLS handshakes.
- Low overhead: WebSocket frames have 2-14 bytes of header vs HTTP's 100+ byte headers per request.
- When to use: Real-time chat, live updates, collaborative editing, gaming, financial data streaming.
- When NOT to use: Standard CRUD APIs, infrequent updates, one-way data (use SSE instead).
Under the Hood
A WebSocket connection starts as a normal HTTP request with an Upgrade header. The server responds with 101 Switching Protocols, and the connection is upgraded to WebSocket. From this point, both sides can send messages freely. The connection persists until explicitly closed by either side.
Scaling WebSockets is harder than scaling HTTP because connections are stateful. You need sticky sessions or a pub/sub system (Redis) so any server can push messages to any connected client.
How Companies Actually Do This
Slack uses WebSockets for real-time messaging — every message, typing indicator, and presence update is delivered via WebSocket.
Figma uses WebSockets for real-time collaborative design, syncing cursor positions and design changes between users.
Binance streams real-time cryptocurrency prices to millions of connected clients via WebSocket.
Common Pitfalls
- Using WebSockets for everything — simple API calls should use HTTP
- Not handling reconnection — networks are unreliable, connections drop
- Not implementing heartbeats — idle connections may be silently closed by firewalls/proxies
Interview Questions Worth Practicing
- When should you use WebSockets vs HTTP long polling vs SSE?
- How do you scale WebSocket connections across multiple servers?
- How do you handle reconnection and message ordering?
- What happens when a WebSocket server goes down?
The Tradeoffs
- WebSocket vs HTTP Polling: WS is real-time but stateful; polling is simpler but wastes bandwidth.
- WebSocket vs SSE: WS is bidirectional; SSE is server-to-client only but simpler and auto-reconnects.
- Scalability: Each WebSocket connection consumes a TCP socket. A single server can handle 100K-1M connections; beyond that, you need horizontal scaling with a message broker.
Related Topics
Original DosiBridge notes for WebSockets
This section is written specifically for this page, not copied from a general system-design checklist. The useful way to study WebSockets is to connect the concept to an integration boundary where bad contracts become customer-facing outages. That framing changes the design discussion from definitions to operating choices.
What makes this topic distinct
- Contract Evolution: For WebSockets, decide the contract evolution before choosing tools; otherwise the design looks clean but fails when websockets changes.
- Idempotency: For WebSockets, decide the idempotency before choosing tools; otherwise the design looks clean but fails when websockets changes.
- Pagination: For WebSockets, decide the pagination before choosing tools; otherwise the design looks clean but fails when websockets changes.
- Error Shape: For WebSockets, decide the error shape before choosing tools; otherwise the design looks clean but fails when websockets changes.
- Consumer Compatibility: For WebSockets, decide the consumer compatibility before choosing tools; otherwise the design looks clean but fails when websockets changes.
Production review question
If a team adopts WebSockets, what observable signal proves it helped instead of only making the architecture diagram more fashionable?
Practical metric to track
Track websockets health as a named dashboard or review item: combine one user-facing metric, one saturation metric, and one correctness metric. For WebSockets, this keeps the discussion grounded in behavior that can be measured after deployment instead of generic advice that sounds good in an interview but never gets verified.