- Published on
Polling vs Long Polling vs WebSockets vs SSE - System Design Guide
Table of Contents
- π· 1. Normal HTTP (Baseline)
- β Flow
- β Limitation
- π₯ FAANG Question
- π§ Script
- π· 2. Polling (AJAX Polling)
- β Idea
- β Problems
- π₯ FAANG Question
- π§ Script
- π· 3. Long Polling (Improved Polling)
- β Idea
- β Benefits
- β Problems
- π₯ FAANG Question
- π§ Script
- π· 4. WebSockets (Best for Real-Time)
- β Idea
- β Benefits
- β How it starts
- π₯ FAANG Question
- π§ Script
- π· 5. Server-Sent Events (SSE)
- β Idea
- β Features
- β Limitation
- π₯ FAANG Question
- π§ Script
- π· 6. Quick Comparison (Interview Gold)
- π₯ FAANG Question
- π§ Script
- π· 7. When to Use What (Practical)
- β Use Polling
- β Use Long Polling
- β Use WebSockets
- β Use SSE
- π₯ FAANG Question
- π§ Script
- π Final 20-sec Interview Answer
π· 1. Normal HTTP (Baseline)
β Flow
- Client β request
- Server β process
- Server β response (connection closes)
β Limitation
- No real-time updates (client must request again)
π₯ FAANG Question
Q: Why is HTTP not suitable for real-time apps? A: Because it is request-response based, server cannot push data anytime.
π§ Script
"HTTP is stateless and request-driven, so it cannot support real-time communication."
π· 2. Polling (AJAX Polling)
β Idea
- Client keeps asking server repeatedly (e.g., every 500ms)
β Problems
- Many empty responses
- High network overhead
- Wastes CPU + bandwidth
π Analogy: Like asking "Any update?" every second
π₯ FAANG Question
Q: What is the biggest drawback of polling? A: High overhead due to frequent unnecessary requests
π§ Script
"Polling repeatedly queries the server, causing high overhead and inefficiency."
π· 3. Long Polling (Improved Polling)
β Idea
- Client sends request
- Server holds it (waits) until data is available
- Sends response β client immediately reconnects
β Benefits
- Fewer empty responses
- Near real-time
β Problems
- Still HTTP overhead
- Frequent reconnections
- Not true real-time
π₯ FAANG Question
Q: How is long polling better than polling? A: It reduces empty responses by waiting until data is available
π§ Script
"Long polling keeps the request open until data is available, reducing unnecessary calls."
π· 4. WebSockets (Best for Real-Time)
β Idea
- Persistent connection
- Full-duplex (two-way) communication
- Single TCP connection stays open
β Benefits
- True real-time
- Low latency
- Low overhead after connection setup
β How it starts
- HTTP β WebSocket handshake β upgrade connection
π₯ FAANG Question
Q: Why are WebSockets ideal for chat apps? A: Because they allow bi-directional real-time communication with low latency
π§ Script
"WebSockets maintain a persistent connection enabling real-time two-way communication."
π· 5. Server-Sent Events (SSE)
β Idea
- Server β pushes data continuously
- Client cannot send back on same connection
β Features
- Persistent connection
- One-way (server β client)
β Limitation
- Not bidirectional
π₯ FAANG Question
Q: When would you use SSE over WebSockets? A: When only server β client updates are needed (e.g., live feeds)
π§ Script
"SSE is ideal for one-way real-time updates from server to client."
π· 6. Quick Comparison (Interview Gold)
| Feature | Polling | Long Polling | WebSockets | SSE |
|---|---|---|---|---|
| Real-time | β | β οΈ Near | β | β |
| Direction | ClientβServer | ClientβServer | π Both | β‘οΈ One-way |
| Connection | Short | Long (per request) | Persistent | Persistent |
| Overhead | High | Medium | Low | Low |
| Use case | Simple apps | Legacy systems | Chat, gaming | Notifications |
π₯ FAANG Question
Q: Which protocol would you choose for a real-time trading app? A: WebSockets β low latency + bidirectional
π§ Script
"For true real-time and bidirectional communication, WebSockets are the best choice."
π· 7. When to Use What (Practical)
β Use Polling
- Simple apps
- Low frequency updates
β Use Long Polling
- When WebSockets not supported
β Use WebSockets
- Chat apps
- Gaming
- Live collaboration
β Use SSE
- Notifications
- Live dashboards
- Stock price updates
π₯ FAANG Question
Q: Why not always use WebSockets? A:
- More complex
- Not always needed
- SSE is simpler for one-way data
π§ Script
"Choose WebSockets for full-duplex, SSE for one-way, and polling only for simple use cases."
π Final 20-sec Interview Answer
"Polling repeatedly requests data causing high overhead. Long polling improves this by holding requests until data is available. WebSockets establish a persistent full-duplex connection enabling real-time communication with low latency. SSE provides a simpler one-way streaming solution from server to client. The choice depends on whether we need bidirectional communication and real-time performance."