Real-Time Communication

Intermediate · 14 min read · High-Level Design, Services & Communication

Short polling, long polling, Server-Sent Events and WebSockets — and scaling persistent connections.

HTTP is fundamentally client-initiated: the client asks, the server answers. But many features need the server to push data the moment something happens — a new chat message, a ride's live location, a stock price, a notification, collaborative edits.

Short polling

The client asks for updates at a fixed interval.

Long polling

The client sends a request; the server holds it open until there's new data (or a timeout, e.g., 30 s), then responds. The client immediately sends another request.

Server-Sent Events (SSE)

A standard browser API (EventSource) for a one-way stream from server to client over a single long-lived HTTP response.

WebSockets

A full-duplex, persistent connection. It starts as an HTTP request with an Upgrade header, then switches to a lightweight framed protocol where either side can send messages at any time.

Comparison

Scaling persistent connections

A single well-tuned server can hold hundreds of thousands of idle connections (memory per connection is the main limit — often a few KB to tens of KB). The hard part isn't holding connections — it's routing a message to the server that holds the recipient's connection.

Webhooks: server-to-server push

For pushing events to other systems (not browsers), the standard is a webhook — your system makes an HTTP POST to a URL the consumer registered (e.g., Stripe notifying a merchant that a payment succeeded).

Interview questions

  • You're designing live notifications for a social app. WebSockets or SSE?
  • How do you deliver a chat message to a user connected to a different WebSocket server?
  • What happens when a WebSocket gateway with 200,000 connections crashes, and how do you handle it?
  • Why might long polling still be used today?

Practice

Write your answer before opening the suggested one. If you mark it "Partly" or "Missed it", it comes back in your review deck.

Further reading

Key takeaways

  • Short polling is simple but wasteful; long polling gives near-real-time over plain HTTP.
  • SSE is the simplest option for server-to-client streams, with built-in reconnection.
  • WebSockets provide full-duplex, low-overhead messaging but make connections stateful.
  • Scaling persistent connections is about routing: a connection registry or pub/sub between gateways.
  • Persist first, push second: clients must be able to resync after disconnects.

Previous: API Design: REST, gRPC, GraphQL · Next: Message Queues & Event Streaming