Design WhatsApp: Real-Time Chat System Design
Step 2 β Replace Polling with WebSocket
A WebSocket replaces polling with a persistent, bidirectional TCP channel. Once open, the server can push messages at any time without waiting for the client to ask.
Request Flow
Client establishes persistent connection
On app open, both users send an HTTP Upgrade request. The WebSocket server accepts, and each phone holds one open TCP connection.
// One-time handshake: GET /ws Upgrade: websocket β HTTP/1.1 101 Switching Protocols // Now: full-duplex TCP. No per-message handshake overhead.
Server pushes messages instantly
User A sends a frame. The WebSocket server looks up User B's connection and pushes the message frame directly. Delivery in <50ms.
// 6 bytes frame overhead vs 600 bytes HTTP:
{ type: "msg", to: "userB", text: "Hey!" }
// Server pushes to userB's open socket instantlyWhy We Need This
Polling: O(users Γ poll_rate) requests/second regardless of message volume.
WebSocket: O(active_messages) frames/second. 99.9% of time: idle connections using ~50KB RAM each β zero CPU.
Key Insight
WebSocket cuts message latency from 1β3s to <50ms and eliminates 99% of HTTP overhead. The tradeoff: servers become stateful β each scales with concurrent connections, not just RPS.
Step 2 of 7
