Think of a cricket score app 🏏.

β€œHow does the server push real-time updates to clients?β€œ

1️⃣ Polling

The client repeatedly asks: > β€œAny new updates?” at a fixed interval.

Flow

Client                      Server
 
Request --------------->
 
               Current Score = 100
 
<--------------- Response
 
(wait 5 sec)
 
Request --------------->
 
               Current Score = 100
 
<--------------- Response
 
(wait 5 sec)
 
Request --------------->
 
               Current Score = 120
 
<--------------- Response

Example

Client calls: GET /score every 5 seconds.

Pros

  • Simple
  • Easy to implement
  • Works everywhere

Cons

  • Lots of unnecessary requests
  • High server load
  • Updates are delayed

If polling interval = 5 sec Server updated at 10:01 - Client sees at 10:05 Latency can be up to polling interval.

System design Use Cases

  • Monitoring pages - Dashboard refresh every minute

Not suitable for chat apps


2️⃣ Long Polling

Instead of immediately responding: Server waits until new data arrives.

Pros

  • Almost real-time
  • Atleast better then Polling

Cons

  • Connection repeatedly opens/closes
  • Scalability - Doesn’t scale as well for millions of users - creates http overhead

System design Use Cases

  • Notification systems
  • Before WebSockets became common

3️⃣ SSE (Server-Sent Events)

SSE keeps one HTTP connection open. Server continuously pushes updates.

Flow

Client ---------------------> Connect
 
Client <--------------------- Event
 
Client <--------------------- Event
 
Client <--------------------- Event
 
Client <--------------------- Event

Connection remains open.

Example

Browser:

const eventSource =
    new EventSource("/events");

Server:

data: Score = 100
 
data: Score = 120
 
data: Score = 150

Characteristics

FeatureSSE
DirectionServer β†’ Client
ProtocolHTTP
Persistent ConnectionYes
Auto ReconnectYes
Browser SupportExcellent

Pros

  • Simpler than WebSockets
  • Uses HTTP
  • Automatic reconnection
  • Great for live feeds

Cons

  • One-way communication
  • Client cannot push data through same connection

Interview Use Cases

  • Stock prices
  • Live sports score
  • News feed
  • Monitoring dashboard

4️⃣ WebSockets

WebSocket creates a persistent TCP connection.

After connection establishment:

Client <---------> Server

Both sides can send messages anytime.


Handshake

Starts as HTTP:

GET /chat
Upgrade: websocket

Server:

101 Switching Protocols

Now protocol becomes WebSocket.


Flow

Client  <=================> Server
 
         Chat Message
 
Client  <=================> Server
 
         Typing Event
 
Client  <=================> Server
 
         Read Receipt

Full duplex communication.


Characteristics

FeatureWebSocket
DirectionTwo-way
PersistentYes
Real-timeYes
Protocol UpgradeYes

Pros

  • Lowest latency
  • Full duplex
  • Efficient
  • Ideal for real-time apps

Cons

  • More complex
  • Stateful connections
  • Load balancing becomes harder

Interview Use Cases

  • Chat applications
  • WhatsApp
  • Slack
  • Multiplayer games
  • Trading platforms
  • Collaborative editors

Quick Comparison

FeaturePollingLong PollingSSEWebSocket
Real-time❌⚠️ Almostβœ…βœ…
Persistent Connection❌⚠️ Temporaryβœ…βœ…
DirectionClient β†’ ServerClient β†’ ServerServer β†’ ClientBidirectional
HTTP Basedβœ…βœ…βœ…Starts with HTTP
Server Push❌⚠️ Sort ofβœ…βœ…
ComplexityLowMediumMediumHigh
ScalabilityPoorBetterGoodBest for realtime

Easy Interview Rule

If interviewer asks:

User refreshes every minute

➑️ Polling

Notification service

➑️ Long Polling or SSE

Live cricket score

➑️ SSE

Reason:

  • Server pushes updates
  • Client rarely sends data
  • Simpler than WebSocket

WhatsApp / Slack

➑️ WebSocket

Reason:

  • User sends and receives messages continuously
  • Bidirectional communication required