CacheU
High Level Design

Long Polling vs Server-Sent Events (SSE)

A deep dive into Long Polling and Server-Sent Events (SSE) as real-time communication techniques used as alternatives to WebSockets, including architecture, request flows, performance trade-offs, scaling considerations, and real-world use cases.

Long Polling vs Server-Sent Events (SSE)

Introduction

Many modern applications require real-time communication between clients and servers.

Examples include:

ApplicationReal-Time Requirement
Chat applicationsInstant message delivery
Stock trading appsLive price updates
Social mediaNotifications
Online gamingState updates
Monitoring dashboardsReal-time metrics

The traditional HTTP request-response model was not designed for continuous real-time communication.

Standard HTTP works like this:


Client → Request → Server
Server → Response → Client
Connection closes

If the client wants updates, it must repeatedly send requests.

This leads to inefficient patterns like polling.

To solve this, several techniques evolved:

  1. Polling
  2. Long Polling
  3. Server-Sent Events (SSE)
  4. WebSockets

This article focuses on Long Polling and SSE, which are widely used alternatives to full-duplex WebSockets.


The Problem with Traditional Polling

Traditional polling repeatedly checks the server for updates.

Example:

Diagram
sequenceDiagram participant Client participant Server Client->>Server: Request updates Server-->>Client: No updates Client->>Server: Request updates Server-->>Client: No updates Client->>Server: Request updates Server-->>Client: New data

Problems with polling:

ProblemExplanation
High network overheadMany useless requests
High server loadServer handles unnecessary queries
Increased latencyClient waits for next polling interval
Inefficient resource usageCPU and bandwidth wasted

Example:

If a client polls every 2 seconds, most requests return no data.

This inefficiency led to Long Polling.


Long Polling

What is Long Polling?

Long Polling is a technique where the client sends a request and the server keeps the connection open until new data becomes available.

Instead of responding immediately, the server waits for updates.

Workflow:

Diagram
sequenceDiagram participant Client participant Server Client->>Server: Request updates Note over Server: Wait for event Server-->>Client: Send new data Client->>Server: New request

This significantly reduces unnecessary requests.


Long Polling Architecture

Diagram
flowchart LR Client --> LoadBalancer LoadBalancer --> AppServer AppServer --> EventSource

Components:

ComponentRole
ClientSends long-lived requests
Load balancerRoutes requests
Application serverHolds request until event
Event sourceTriggers response

Long Polling Request Lifecycle

  1. Client sends request to server
  2. Server keeps request open
  3. If event occurs → server responds
  4. Client immediately sends a new request

Diagram:

Diagram
sequenceDiagram participant Client participant Server Client->>Server: Request updates alt Event occurs Server-->>Client: Send data else Timeout Server-->>Client: Empty response end Client->>Server: New request

Advantages of Long Polling

AdvantageExplanation
Works with HTTPNo special protocols
Firewall friendlyUses normal HTTP
Lower latency than pollingServer responds immediately
Simple implementationEasy to add to existing apps

Limitations of Long Polling

LimitationExplanation
High connection churnNew request after every response
Server resource usageMany open connections
Inefficient for massive scaleHard to handle millions of clients
Increased latency during reconnectsSmall delays occur

For large systems, this approach becomes expensive.


Server-Sent Events (SSE)

What is SSE?

Server-Sent Events allow the server to push updates continuously to the client over a single HTTP connection.

Unlike Long Polling:

  • Connection remains open
  • Server streams events
  • Client receives updates continuously

Workflow:

Diagram
sequenceDiagram participant Client participant Server Client->>Server: Open SSE connection Server-->>Client: Event 1 Server-->>Client: Event 2 Server-->>Client: Event 3

The connection stays alive indefinitely.


SSE Architecture

Diagram
flowchart LR Client --> LoadBalancer LoadBalancer --> AppServer AppServer --> EventStream

The server maintains a persistent HTTP connection with the client.


SSE Communication Flow

Diagram
sequenceDiagram participant Client participant Server Client->>Server: GET /events Server-->>Client: event: message Server-->>Client: data: update1 Server-->>Client: event: message Server-->>Client: data: update2

The server sends event streams.


SSE Message Format

SSE uses a simple text format.

Example:

event: message
data: Hello World

event: notification
data: New message received

Fields:

FieldPurpose
eventEvent type
dataMessage payload
idEvent ID
retryReconnection time

SSE Client Example (JavaScript)

const eventSource = new EventSource("/events");
 
eventSource.onmessage = function(event) {
    console.log("New data:", event.data);
};

The browser automatically maintains the connection.


Advantages of SSE

AdvantageExplanation
Persistent connectionNo repeated requests
Lower overheadSingle long-lived connection
Automatic reconnectionBuilt into browser API
Efficient streamingContinuous event delivery
Simpler than WebSocketsUses HTTP

Limitations of SSE

LimitationExplanation
One-way communicationServer → Client only
Limited browser support historicallyOlder browsers lacked support
Connection limitsBrowsers limit open connections
Not ideal for bidirectional appsChat apps may require WebSockets

Long Polling vs SSE

Comparison:

FeatureLong PollingSSE
Connection modelRepeated requestsPersistent connection
DirectionServer → ClientServer → Client
LatencyMediumLow
Network overheadHigherLower
Implementation complexityMediumLow
ScalabilityModerateBetter

Architecture Comparison

Long Polling:

Diagram
flowchart LR Client --> Server Client --> Server Client --> Server

Repeated connections.

SSE:

Diagram
flowchart LR Client --> PersistentConnection --> Server

Single connection.


Scaling Long Polling

To scale long polling:

StrategyExplanation
Load balancersDistribute connections
Async serversNon-blocking request handling
Event queuesDecouple event processing
Horizontal scalingAdd more servers

Example architecture:

Diagram
flowchart LR Clients --> LoadBalancer LoadBalancer --> AppServers AppServers --> EventQueue

Scaling SSE

SSE scaling strategies:

StrategyExplanation
Event streaming systemsPublish updates
Distributed message brokersDeliver events
Horizontal scalingMore edge nodes
Connection multiplexingEfficient streams

Architecture:

Diagram
flowchart LR Clients --> CDN CDN --> EdgeServers EdgeServers --> EventStream EventStream --> EventQueue

Real-World Use Cases

Long Polling:

ApplicationExample
Legacy chat systemsEarly messaging apps
Notification systemsSimple alerts
Monitoring dashboardsPeriodic updates

SSE:

ApplicationExample
Stock market feedsLive price updates
Real-time analytics dashboardsMetrics streaming
Social media notificationsActivity feeds
Sports score updatesLive match scores

SSE vs WebSockets

Although SSE is powerful, WebSockets offer full-duplex communication.

FeatureSSEWebSockets
DirectionServer → ClientBidirectional
ProtocolHTTPCustom protocol
ComplexityLowerHigher
Use caseStreaming updatesInteractive apps

Choosing the Right Approach

Decision table:

Use CaseRecommended
Live dashboardsSSE
NotificationsSSE
Chat applicationsWebSockets
Legacy HTTP systemsLong Polling
Event streamingSSE

Summary

Real-time communication is essential for modern applications.

Long Polling and Server-Sent Events provide efficient alternatives to traditional polling.

Key takeaways:

ConceptExplanation
Long PollingClient waits for server updates via held request
SSEServer streams events over persistent HTTP connection
PollingInefficient repeated requests

Compared to polling, both approaches:

  • Reduce latency
  • Lower server load
  • Enable real-time data delivery

SSE is generally preferred when unidirectional streaming updates are needed, while Long Polling remains useful for legacy systems or environments without SSE support.