System Design Fundamentals
Vote

0% completed

Difference Between Long-Polling, WebSockets, and Server-Sent Events

Key Differences

How to Choose

Quick Reference

Conclusion

Long-Polling, WebSockets, and Server-Sent Events all solve the same problem: getting updates from the server to the client without refreshing the page. They just solve it in three different ways. Here is each one in a single sentence:

  • Long-Polling: the client asks, and the server holds the request open until it has data to send. After every answer, the client asks again.
  • WebSocket: one persistent connection where both the client and the server can send messages at any time.
  • Server-Sent Events (SSE): one persistent HTTP connection where the server streams updates down to the client, one way only.
The three techniques side by side: repeated held requests, one two-way channel, and a one-way event stream
The three techniques side by side: repeated held requests, one two-way channel, and a one-way event stream

Key Differences

FeatureLong-PollingWebSocketServer-Sent Events
DirectionClient asks, server answersTwo-way, both sides anytimeOne-way, server to client
ConnectionNew request after every messageOne persistent connectionOne persistent connection
Built onPlain HTTPStarts as HTTP, then upgrades to its own protocolPlain HTTP
ReconnectionClient re-requests after every response or timeoutYou write the reconnect logic yourselfBrowser reconnects automatically
Overhead per messageFull HTTP request every timeVery smallSmall
Best forFallback, simple notificationsChat, games, collaborationFeeds, scores, streaming AI answers

How to Choose

Ask three questions, in order:

  1. Do the client and the server both need to send messages at any time? If yes, use WebSocket. Nothing else gives you a true two-way channel.
  2. Do updates only flow from the server to the client? If yes, use Server-Sent Events. It is simpler than WebSocket, it is plain HTTP, and the browser handles reconnection for you.
  3. Do you need something that works through old proxies and strict corporate networks, or a fallback when the other two fail? Use Long-Polling. It is the least efficient, but it works almost everywhere.
Choosing a technique: two-way traffic points to WebSocket, one-way streams point to SSE, and Long-Polling covers the rest
Choosing a technique: two-way traffic points to WebSocket, one-way streams point to SSE, and Long-Polling covers the rest

💡 In a system design interview, do not just name a technique. Say why it fits. "Chat needs messages in both directions with low latency, so I would use WebSockets, with long-polling as a fallback for restrictive networks." For a notifications feed: "Updates only flow to the client, so SSE gives me streaming over plain HTTP, and the browser reconnects on its own." Naming the trade-off is what earns the credit.

Quick Reference

  • Long-Polling

    • Connection: a new HTTP request after every message
    • Direction: client asks, server answers when it has data
    • Reconnection: the client re-requests every time
    • Use it for: fallbacks and simple notification delivery
  • WebSocket

    • Connection: one persistent, upgraded connection
    • Direction: both ways, at any time
    • Reconnection: your application handles it
    • Use it for: chat, multiplayer games, collaborative editing, trading
  • Server-Sent Events

    • Connection: one persistent HTTP connection
    • Direction: server to client only
    • Reconnection: automatic, built into the browser
    • Use it for: live feeds, scores, prices, progress, streaming AI answers

Conclusion

There is no single winner. Long-Polling is the most compatible, WebSocket is the most capable, and Server-Sent Events is the simplest for one-way streams. Match the technique to the direction of your data, and you will get the right answer in both production and interviews.

Vu Tong

Vu Tong

· 4 months ago

It's worth to mention that to avoid performance impact, the server should have some kind of asynchronous processing for incoming requests. If the server uses a traditional thread-per-request model and every client holds a long-lived connection, that will hurt the overall performance.

Show 2 replies
D

dominikcubic

· 3 years ago

In this description of AJAX polling, is the "requested webpage" the same as the "Client"?

  1. Client requests data from a server using regular HTTP.
  2. The requested webpage opens a connection to the server.
  3. The server sends the data to the client whenever there's new information available.
Show 1 reply
H

heybenross

· 3 years ago

When should each be used? What are the pros and cons of each?

Show 3 replies

Reading Progress

0%


Vote for new content

On This Page

Key Differences

How to Choose

Quick Reference

Conclusion