SDE & MLE Prep

Real-Time Updates: Polling, SSE, WebSockets

How the server pushes fresh data (chat, live comments, notifications, dashboards, collaborative editing, driver locations).

TransportHowProsCons / use when
Short pollingclient asks every N strivial, works everywhere, statelesswasted requests, latency ≤ N s; fine for slow-changing data
Long pollingserver holds the request until data arrives (or ~30 s), client re-requestsnear real time over plain HTTPa request per message; reconnect storms
Server-Sent Eventsone long-lived HTTP response streaming text/event-streamsimple, server → client, auto-reconnect with Last-Event-ID, works over HTTP/2one direction only; good for feeds, live comments, LLM token streaming
WebSocketsupgraded TCP connection, full duplexlow latency both waysstateful connections: sticky routing, LB/proxy support, reconnection logic; chat, games, collaboration
WebRTCpeer-to-peer (STUN/TURN for NAT traversal)lowest latency, offloads serverscomplex; audio/video calls

The server side is the real design problem:

publishers -> pub/sub (Redis pub/sub, Kafka, NATS) -> gateway servers holding connections -> clients
                                                     ^
                     connection registry: user_id -> gateway (Redis, with TTL heartbeats)
  • Connections are stateful: track which gateway holds each user; gateways subscribe only to the topics their users need.
  • Plan for reconnects: clients resume from the last event id / sequence number and fetch what they missed from durable storage.
  • One gateway holds tens to hundreds of thousands of mostly idle connections (memory and file descriptors per connection); scale horizontally and drain connections gracefully on deploys.