System Design Academy

Saving…

Lesson 9 of 10 in Module 10 of 54 lessons complete

Real-Time: Polling, Long Polling, SSE & WebSockets

Module 1, lesson 9Concept10 min

Definition

Polling

In plain English
Polling is when the client asks the server for updates on a fixed timer.
Think of it like
It is like a child asking "are we there yet?" every five minutes.

Long polling

In plain English
Long polling is when the server holds a request open until it has news or a timeout passes.
Think of it like
It is like waiting at the counter after asking the shopkeeper to tell you when stock arrives.

Server-Sent Events

In plain English
Server-Sent Events is a one-way stream where the server keeps pushing updates over one open HTTP response.
Think of it like
It is like tuning into a radio broadcast.

WebSocket

In plain English
A WebSocket is a long-lived, two-way connection where either side can send messages at any time.
Think of it like
It is like an open phone call instead of swapping letters.

Why it exists

Your ticket queue changes at unpredictable times. Checking every 50 milliseconds wastes requests. Checking every minute leaves users looking at an old position.

Choose how updates travel based on the delay users can tolerate and whether messages need to flow in both directions.

How it works

ApproachDirectionDelayWasted requestsComplexityServers between client and app
PollingClient asks, server repliesUp to intervalOftenLowOrdinary HTTP
Long pollingClient asks, server waitsLow after updateSome timeoutsMediumMatch request time limits
SSEServer to client after initial requestLowFewMediumForward events without waiting to collect more
WebSocketBoth directionsLowFewHigherAllow WebSocket setup and long idle periods
  1. Ask

    Client requests updates with its last known position.

  2. Wait

    Server holds the request until data arrives or timeout expires.

  3. Reply

    Server returns an update or an empty result.

  4. Repeat

    Client opens the next request, using retry delay after errors.

The client requests a WebSocket upgrade, and the server replies 101 Switching Protocols. The server sends a new order; the client sends order bumped.

A classic HTTP/1.1 upgrade starts a two-way WebSocket conversation.

The client requests a WebSocket upgrade, and the server replies 101 Switching Protocols. The server sends a new order; the client sends order bumped.

Examples

Stream a ticket queue position

Your concert queue page displays position 4,512 and only needs incoming updates.

SSE keeps one HTTP response open for queue updates. Number each event so reconnecting clients can ask for missed updates. The server must save those events to replay them.

Blank lines separate SSE events; this event reports position 4,512.
HTTP
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache

id: 18
event: position
data: {"position":4512}

The insight

If data flows one way, SSE is simpler than WebSockets.

Trade-offs

Pros

  • Deliver updates promptly.
  • Avoid frequent empty requests.

Cons

  • Connections consume resources.
  • Connection routing adds work.
  • Clients need recovery logic.

Use when

  • Updates are frequent or unpredictable.
  • Seconds of delay affect decisions.

Avoid when

  • Changes are rare and delay is acceptable.
  • Data is needed only when opening a screen.

Quick check

A ticket-queue page shows your position; the browser never sends anything back. Simplest fit?
Main scaling cost of WebSockets?

Takeaways

  • Polling is easy to operate but can waste requests.
  • SSE = server → client stream.
  • WebSocket carries messages both ways but requires managing connections that stay open.

Try the quick check above whenever you feel ready.

Previous: gRPC & Protocol Buffers