← Back to System Design, page 2

system-design-0028 · Medium fresher

Compare short polling, long polling, Server-Sent Events and WebSockets for real-time updates.

Short polling asks the server every few seconds and wastes requests. Long polling holds each request open until there is news, then the client reconnects. Server-Sent Events keep one HTTP response open for a one-way stream of server-to-client events with automatic reconnect. WebSockets upgrade to a full-duplex connection for low-latency two-way messages, at the cost of stateful connections to manage.

Full answer

Short polling

The client sends GET /notifications?since=... every N seconds. Simple and works everywhere. But most responses are empty, and updates arrive up to N seconds late. With 1 million open apps polling every 5 seconds, that is 200,000 requests per second, mostly for nothing.

Long polling

The client sends a request; the server holds it open until an event arrives or a timeout (say 30 seconds), then responds. The client immediately sends the next request. Latency is low, and it uses plain HTTP, so it passes most proxies. Costs: one held request per client, reconnect overhead after every event, and care needed to not miss events between requests (use a since cursor).

Server-Sent Events (SSE)

The browser opens new EventSource('/stream'). The server responds with Content-Type: text/event-stream and keeps writing lines like:

id: 1042 event: score data: {"match": 7, "runs": 182, "wickets": 4}

Each event ends with a blank line. One-way (server to client). Built-in reconnect, and the browser sends Last-Event-ID so the server can resume. Text only. Works over HTTP/2 well. A good fit for live cricket scores, notification badges, progress of a long report, and streaming text responses.

WebSockets

The client sends an HTTP request with Upgrade: websocket; after the 101 Switching Protocols response, the same TCP connection carries messages in both directions with tiny framing overhead. Best for chat, multiplayer games, collaborative editing and live classroom tools where the client also sends frequent messages.

Costs: each connection is stateful and pinned to one server, so you need a connection gateway tier, a way to route messages to the server holding a user's socket (for example a pub/sub channel per user), heartbeats to detect dead connections, and reconnect logic with resume. Some corporate proxies interfere with them.

Choosing

NeedChoice
Updates every few minutes, simplest possibleShort polling
Real-time, restrictive networks, simple serverLong polling
Server-to-client stream onlySSE
Frequent two-way messages, lowest latencyWebSockets

Mobile apps often combine a socket while in the foreground with push notifications (APNs, FCM) when in the background.

By level

Fresher: Explain each technique in simple terms and pick WebSockets for chat and SSE or polling for simple notifications. Choosing correctly between one-way and two-way needs is the main test.

Mid: Compare overhead and latency with numbers, explain reconnect and resume, and describe a connection gateway with pub/sub routing. Expect polling cost to be shown with numbers.

Senior: Discuss scaling millions of connections, load balancer settings for long-lived connections, graceful drain on deploys, back-pressure, presence, and fallbacks for restricted networks. The senior answer covers graceful drain of connections during deploys.

Key points

  • Short polling is simple but wasteful and delayed
  • Long polling holds a request until an event, then reconnects
  • SSE is a one-way event stream with built-in reconnect and Last-Event-ID
  • WebSockets give full-duplex, low-latency messaging but need stateful connection management
  • Mobile apps add push notifications for background delivery

Common mistakes

  • Choosing WebSockets for one-way updates where SSE is simpler
  • Forgetting heartbeats and reconnect logic
  • Missing events between long poll requests without a cursor
  • Ignoring how to route a message to the server holding the connection

Follow-ups

  • How would you send a message to a user connected to one of 500 WebSocket servers?
  • How does SSE resume after a dropped connection?
  • How many concurrent connections can one server hold, and what limits it?

Tips

  • Order the options from simplest to most capable
  • Do the polling arithmetic for a large user count
  • Mention the connection routing problem for WebSockets

Evaluation rubric

  • Correct mechanics of all four
  • Explains costs, especially of polling and WebSockets
  • Picks correctly for one-way versus two-way needs
  • Mentions reconnect and routing concerns