Skip to content

Real-time Protocols — Detailed#

sequenceDiagram
  participant C as Client
  participant S as Server

  Note over C,S: Long Polling
  C->>S: GET /events (held open)
  Note over S: event happens
  S-->>C: 200 response with event
  C->>S: GET /events (re-poll immediately)

  Note over C,S: SSE
  C->>S: GET /events  (Accept: text/event-stream)
  S-->>C: data: event 1\n\n
  S-->>C: data: event 2\n\n
  Note over S: same connection forever

  Note over C,S: WebSocket
  C->>S: HTTP Upgrade: websocket
  S-->>C: 101 Switching Protocols
  C->>S: frame
  S->>C: frame
  Note over C,S: full duplex

Comparison#

Long polling SSE WebSocket gRPC streaming
Direction server → client (1 event/conn) server → client bidirectional uni or bidi
Transport HTTP/1.1 HTTP/1.1 + 2 TCP after upgrade HTTP/2
Re-connect client polls EventSource auto-reconnects manual manual
Binary base64 or no no (text only) yes yes (protobuf)
Proxy / firewall friendly best very good good (port 443 + Upgrade) varies
Backpressure implicit (next poll) none application-level flow control built-in
Use case legacy clients, simple pushes dashboards, news feeds, notifications chat, collab, games service-to-service streams

Connection model#

flowchart TB
  subgraph Client
    A[App]
    SDK[WS / SSE / gRPC SDK]
  end
  subgraph Edge
    LB[L7 LB / proxy]
    GW[Gateway holding persistent conns]
  end
  subgraph Backend
    H[Hub / Channel Service]
    PB[Pub/Sub bus]
    SVC[Domain services]
  end
  A --> SDK --> LB --> GW
  GW --> H
  H --- PB
  SVC --> PB
  PB --> H --> GW --> SDK

    classDef client fill:#dbeafe,stroke:#1e40af,stroke-width:1px,color:#0f172a;
    classDef edge fill:#cffafe,stroke:#0e7490,stroke-width:1px,color:#0f172a;
    classDef service fill:#fef3c7,stroke:#92400e,stroke-width:1px,color:#0f172a;
    classDef datastore fill:#fee2e2,stroke:#991b1b,stroke-width:1px,color:#0f172a;
    classDef cache fill:#fed7aa,stroke:#9a3412,stroke-width:1px,color:#0f172a;
    classDef queue fill:#ede9fe,stroke:#5b21b6,stroke-width:1px,color:#0f172a;
    classDef compute fill:#d1fae5,stroke:#065f46,stroke-width:1px,color:#0f172a;
    classDef storage fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#0f172a;
    classDef external fill:#fce7f3,stroke:#9d174d,stroke-width:1px,color:#0f172a;
    classDef obs fill:#f3e8ff,stroke:#6b21a8,stroke-width:1px,color:#0f172a;
    class SDK client;
    class LB,GW edge;
    class A,H,SVC service;
    class PB queue;

Scale numbers#

  • Single WS gateway: 50k–200k concurrent connections (kernel tuning matters).
  • SSE same range; less per-connection memory.
  • Long polling far worse — thread/conn churn unless using async stack.

Choosing one#

  • One-way push, browser-friendly, simple → SSE.
  • Bidirectional, low-latency, chat-like → WebSocket.
  • Service-to-service → gRPC streaming.
  • You need to traverse every legacy proxy → long polling fallback.

Many systems (Slack, ChatGPT, Discord) use WebSocket as primary + long-polling fallback for restrictive networks.

Operational pitfalls#

  • Sticky load balancer or message bus: each WS / SSE connection is pinned; cluster needs pub/sub fan-out (Redis pub/sub, NATS, Kafka).
  • Idle timeouts: L4 LBs (AWS NLB) close idle TCP at 60–350 s — send heartbeats.
  • Server reboot / deploy: thundering reconnect when 200k clients reconnect simultaneously — use jitter + staged rollouts.
  • Auth on upgrade: pass a short-lived token in the initial handshake; rotate.

Glossary & fundamentals#

Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.

Tag Concept What it is Page
HLD Load balancer / GSLB L4/L7 traffic distribution and failover load-balancer
HLD Pub/Sub & message brokers topics, consumer groups, delivery semantics pub-sub-pattern
HLD Idempotency & retries safe re-execution, backoff + jitter idempotency-retries
HLD Resilience patterns timeout, retry, breaker, bulkhead, backpressure resilience-patterns
HLD HTTP / TLS protocols HTTP 1.1/2/3, QUIC, TLS 1.3 http-protocols
HLD Realtime protocols WS / SSE / polling / gRPC streaming realtime-protocols
LLD Async models futures / async-await / coroutines / actors async-models
LLD Structural patterns Adapter, Decorator, Facade, Proxy, Composite structural-patterns