Skip to main content
Primitives/Primitive #18
PRIMITIVE #18Core Distributed Systems Component

WebSocket, Server-Sent Events (SSE) & Long Polling

1. What It Is & Why It Exists

The Core Problem: Real-Time Server-to-Client Push

Standard HTTP/1.1 is inherently request-response: the server cannot push data to a client without the client explicitly initiating a request.

  1. Short Polling (Anti-Pattern): The client queries GET /notifications every 1 second. Over 95%95\% of responses return empty arrays ([]), wasting massive bandwidth, SSL handshakes, and CPU cycles.
  2. Long Polling (Hanging HTTP): The client opens an HTTP request that the server holds open until new data arrives. After each push, the connection closes, requiring a continuous renegotiation of TCP + TLS handshakes.

The First-Principles Solution: Persistent Streaming Protocols

Persistent real-time protocols establish long-lived TCP/HTTP connections, allowing servers to stream events to clients with sub-millisecond delivery latency.

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

2. Comprehensive Protocol Comparison Matrix

ProtocolDirectionalityTransport LayerFraming OverheadNative ReconnectionFirewall & Proxy TraversalBest Production Fit
WebSocketFull-Duplex (Bi-directional)TCP (ws://, wss://)2–10 BytesApplication-level logicMay require proxy upgrade headers (Connection: Upgrade)Instant messaging, trading order books, interactive whiteboards
Server-Sent Events (SSE)Uni-directional (Server →\to Client)HTTP/2 or HTTP/1.1 (text/event-stream)UTF-8 text framingAutomatic native browser retry (Last-Event-ID)Flawless (Standard HTTPS Port 443)LLM token streaming (ChatGPT), live score updates, metric dashboards
Long PollingHalf-Duplex (Emulated push)HTTP Request/ResponseFull HTTP Header (>500 B> 500\text{ B})Application-level loopFlawlessFallback for legacy corporate firewalls blocking WebSockets
WebTransport ()Multi-Stream Full-DuplexUDP / Microsecond datagramsNative session migrationModern edge networks (Cloudflare)Next-gen real-time audio/video metadata and gaming

3. Distributed WebSocket Gateway Architecture & Routing Backplane

In a clustered environment with multiple gateway servers, Client 1 is connected to Gateway A, while Client 2 is connected to Gateway B. When Client 1 sends a message to Client 2:

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

DynamoDB Connection Registry Schema

  • PK = USER#<user_id>, SK = CONN#<connection_id>
  • Attributes: gateway_ip, connected_at, ( expiration timestamp).

Part 2: Production Deep-Dive Locked1 Coin = 24 Hours

Unlock Complete Architecture & Production Runbooks

Your Balance:40 Coins

You have explored the free architectural preview (~49%). Spend 1 Coin to unlock the remaining 4 production deep-dive sections for a full 24 hours.

Sections Included in This 24-Hour Pass:
4. Critical Edge Cases & Distributed Failure Modes
5. Production Pitfalls & Anti-Patterns (The "Gotchas")
6. AWS Cloud Service Implementation & Production Patterns
7. Production Tuning & Sizing Runbook
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure