0
0
HLDsystem_design~15 mins

Long polling and Server-Sent Events in HLD - Deep Dive

Choose your learning style9 modes available
Overview - Long polling and Server-Sent Events
What is it?
Long polling and Server-Sent Events (SSE) are techniques used to keep a connection open between a client and a server to receive updates in real-time. Long polling involves the client sending a request and the server holding it until new data is available or a timeout occurs. SSE allows the server to push updates continuously over a single HTTP connection without the client repeatedly asking. Both methods help web applications get timely information without constant refreshing.
Why it matters
Without these techniques, web applications would need to repeatedly ask the server if new data is available, causing delays, wasted resources, and poor user experience. Real-time updates are crucial for chat apps, live scores, notifications, and dashboards. Long polling and SSE solve the problem of efficient, timely communication from server to client, making apps feel fast and responsive.
Where it fits
Before learning this, you should understand basic HTTP request-response communication and client-server architecture. After this, you can explore WebSockets for full-duplex communication and more advanced real-time systems like message queues and event-driven architectures.
Mental Model
Core Idea
Long polling and Server-Sent Events keep a communication line open so the server can send updates to the client as soon as they happen, avoiding constant asking.
Think of it like...
It's like waiting for a phone call instead of calling repeatedly to ask if there's news; long polling is waiting on the line until someone answers, and SSE is like having a radio broadcast you listen to continuously.
Client                      Server
  │                           │
  │--- Request (Long Poll) --->│
  │                           │
  │<--- Hold response ---------│ (waits until new data)
  │                           │
  │<--- Response with data ----│
  │                           │
  │--- New Request --------->  │ (repeat)


Client                      Server
  │                           │
  │--- Open SSE connection --->│
  │                           │
  │<=== Stream events --------│ (continuous updates)
  │                           │
  │                           │
Build-Up - 7 Steps
1
FoundationBasic HTTP Request-Response Model
🤔
Concept: Understanding how a client asks a server for data and waits for a response.
In the web, a client (like a browser) sends a request to a server asking for information. The server processes this request and sends back a response. This is a simple one-time exchange: the client asks, the server answers, and the connection closes.
Result
The client gets the data it asked for, but if it wants new data later, it must ask again.
Understanding this basic model is essential because long polling and SSE build on it by changing how and when the server responds.
2
FoundationLimitations of Simple Polling
🤔
Concept: Why repeatedly asking the server for updates is inefficient and slow.
Simple polling means the client sends requests at regular intervals (e.g., every 5 seconds) to check for new data. This wastes resources because many requests return no new data. It also causes delays because the client only learns about updates after the next poll.
Result
High network traffic and slower user experience due to unnecessary requests and delayed updates.
Knowing these problems motivates the need for better methods like long polling and SSE.
3
IntermediateHow Long Polling Works
🤔Before reading on: do you think the server immediately responds to a long polling request or waits? Commit to your answer.
Concept: Long polling keeps the client request open until the server has new data or a timeout occurs.
The client sends a request to the server. Instead of responding immediately, the server holds the request open until new data is available or a timeout happens. When data is ready, the server sends it and closes the connection. The client then immediately sends a new request to wait for more data.
Result
The client receives updates as soon as they happen, reducing unnecessary requests and delays.
Understanding that the server delays its response changes the usual request-response pattern and improves real-time communication.
4
IntermediateServer-Sent Events (SSE) Basics
🤔Before reading on: do you think SSE requires the client to send repeated requests or just one? Commit to your answer.
Concept: SSE uses a single HTTP connection where the server continuously sends updates to the client.
The client opens a connection to the server using a special request. The server keeps this connection open and streams data to the client whenever new events occur. The client listens to this stream and processes updates in real-time without sending new requests.
Result
A continuous flow of updates from server to client with less overhead than repeated requests.
Knowing SSE uses a persistent connection helps understand how it achieves efficient real-time updates.
5
IntermediateComparing Long Polling and SSE
🤔Before reading on: which do you think uses fewer connections over time, long polling or SSE? Commit to your answer.
Concept: Both methods reduce polling overhead but differ in connection usage and data flow style.
Long polling repeatedly opens and closes connections, holding each open until data is ready. SSE opens one connection that stays open, streaming multiple events. SSE is simpler for continuous updates, while long polling works better with older browsers or when server push is limited.
Result
Choosing the right method depends on browser support, server capabilities, and update frequency.
Understanding the tradeoffs between connection management and data flow guides better system design.
6
AdvancedHandling Failures and Reconnection
🤔Before reading on: do you think SSE automatically reconnects after a connection loss? Commit to your answer.
Concept: Both long polling and SSE need strategies to handle network failures and reconnect smoothly.
In long polling, the client sends a new request after each response or timeout, naturally reconnecting. SSE includes built-in support for automatic reconnection with configurable delays. Both require handling edge cases like dropped connections, server restarts, and message ordering to maintain a seamless user experience.
Result
Robust real-time communication that recovers from interruptions without user action.
Knowing how reconnection works prevents common bugs and improves reliability in production.
7
ExpertScaling and Performance Considerations
🤔Before reading on: do you think holding many open connections on the server is cheap or expensive? Commit to your answer.
Concept: Long polling and SSE require server resources to keep connections open, impacting scalability and performance.
Each open connection consumes memory and CPU on the server. For many users, this can overwhelm traditional servers. Techniques like event-driven servers, connection pooling, load balancing, and using specialized protocols or services help manage this load. Choosing between long polling and SSE also affects resource usage and latency.
Result
A scalable real-time system that balances user experience with server capacity.
Understanding resource costs of open connections is key to designing systems that work well under heavy load.
Under the Hood
Long polling works by the server holding HTTP requests open until data is ready, then responding and closing the connection. The client immediately sends a new request to wait again. SSE uses a single HTTP connection kept open indefinitely, where the server streams text-based event data separated by special delimiters. The client parses these events as they arrive. Both rely on HTTP/1.1 persistent connections and require the server to efficiently manage many open sockets, often using asynchronous or event-driven programming models.
Why designed this way?
These methods were designed to work within the constraints of HTTP and browsers before WebSockets were widely supported. They provide a way to push data from server to client without requiring new protocols or breaking firewalls. Long polling mimics server push by delaying responses, while SSE formalizes a streaming approach over HTTP. Alternatives like WebSockets offer full-duplex communication but need more complex support and infrastructure.
┌─────────────┐       ┌─────────────┐
│   Client    │       │   Server    │
└─────┬───────┘       └─────┬───────┘
      │ Request (Long Poll)    │
      │──────────────────────▶ │
      │                       │
      │ <--- Hold open --------│ (wait for data)
      │                       │
      │ <--- Response ---------│ (send data)
      │                       │
      │ Request (new poll)     │
      │──────────────────────▶ │


┌─────────────┐       ┌─────────────┐
│   Client    │       │   Server    │
└─────┬───────┘       └─────┬───────┘
      │ Open SSE connection    │
      │──────────────────────▶ │
      │                       │
      │ <=== Stream events ====│ (continuous data)
      │                       │
      │ (connection stays open)│
Myth Busters - 4 Common Misconceptions
Quick: Does long polling keep the connection open forever until the client closes it? Commit to yes or no.
Common Belief:Long polling keeps the connection open indefinitely until the client decides to close it.
Tap to reveal reality
Reality:Long polling holds the connection only until new data arrives or a timeout occurs, then closes it. The client must send a new request to continue receiving updates.
Why it matters:Believing the connection stays open forever can lead to design errors and resource leaks on the server.
Quick: Does SSE allow the client to send data to the server over the same connection? Commit to yes or no.
Common Belief:SSE supports two-way communication, so the client can send data back to the server on the same connection.
Tap to reveal reality
Reality:SSE is a one-way channel from server to client only. The client must use separate HTTP requests to send data to the server.
Why it matters:Misunderstanding this can cause developers to try using SSE for bidirectional communication, leading to failed implementations.
Quick: Is SSE supported by all modern browsers equally? Commit to yes or no.
Common Belief:All modern browsers fully support SSE without issues.
Tap to reveal reality
Reality:Most modern browsers support SSE, but some (like Internet Explorer and older Edge versions) do not. Polyfills or fallback methods like long polling are needed for full compatibility.
Why it matters:Assuming universal support can cause real-time features to break for some users.
Quick: Does using long polling eliminate all network overhead compared to simple polling? Commit to yes or no.
Common Belief:Long polling completely removes network overhead because it waits for data before responding.
Tap to reveal reality
Reality:Long polling reduces overhead but still involves opening and closing connections repeatedly, which adds some overhead compared to a persistent connection like SSE.
Why it matters:Overestimating efficiency can lead to scaling problems under heavy load.
Expert Zone
1
Long polling can cause head-of-line blocking on HTTP/1.1 servers if too many connections are held, requiring careful tuning or HTTP/2 usage.
2
SSE events can include custom event types and IDs, enabling clients to resume streams after disconnections without data loss.
3
Network intermediaries like proxies and firewalls may buffer or close idle SSE connections, requiring heartbeat messages to keep them alive.
When NOT to use
Avoid long polling and SSE when full-duplex communication is needed; use WebSockets instead. Also, for very high-frequency updates or binary data, WebSockets or specialized protocols like MQTT are better. If browser support is limited, fallback to long polling or polling.
Production Patterns
In production, SSE is often used for live notifications, news feeds, or stock tickers where data flows mostly server-to-client. Long polling is used in legacy systems or when SSE is unsupported. Both are combined with load balancers and event-driven servers (like Node.js or Nginx with push modules) to handle many concurrent users efficiently.
Connections
WebSockets
WebSockets build on the idea of persistent connections but allow two-way communication, unlike SSE and long polling.
Understanding SSE and long polling clarifies why WebSockets were developed and when to choose each method.
Event-driven Programming
Long polling and SSE rely on event-driven server architectures to efficiently manage many open connections.
Knowing event-driven models helps grasp how servers handle waiting for data without blocking resources.
Radio Broadcasting
SSE is similar to radio broadcasting where one source continuously sends information to many listeners.
This cross-domain link shows how continuous one-way data streams work in both technology and media.
Common Pitfalls
#1Keeping long polling connections open indefinitely without timeout.
Wrong approach:Server holds the HTTP request open forever until the client disconnects.
Correct approach:Server sets a reasonable timeout (e.g., 30 seconds) to close the connection if no data arrives, prompting the client to reconnect.
Root cause:Misunderstanding that long polling requires a timeout to avoid resource exhaustion.
#2Using SSE without handling reconnection logic on the client.
Wrong approach:Client opens SSE connection once and does not listen for connection errors or retries.
Correct approach:Client uses built-in SSE event handlers to detect disconnections and automatically reconnect after a delay.
Root cause:Assuming SSE connections are permanent and never fail.
#3Sending large binary data over SSE as text.
Wrong approach:Server encodes binary data as text and sends it via SSE without compression or chunking.
Correct approach:Use WebSockets or other protocols designed for binary data instead of SSE.
Root cause:Not recognizing SSE is designed for text/event streams, not binary data.
Key Takeaways
Long polling and Server-Sent Events enable real-time server-to-client updates by keeping HTTP connections open longer than usual.
Long polling holds a request until data is ready, then closes it, requiring repeated requests; SSE keeps one connection open and streams events continuously.
Both methods improve efficiency and user experience compared to simple polling but have different tradeoffs in connection management and browser support.
Proper handling of connection timeouts, reconnections, and server resource usage is critical for building reliable and scalable real-time systems.
Choosing between long polling, SSE, and WebSockets depends on the application's communication needs, browser compatibility, and server capabilities.