What if your app could magically get updates the moment they happen, without asking again and again?
Why Long polling and Server-Sent Events in HLD? - Purpose & Use Cases
Imagine you have a website that shows live sports scores. Without any special technique, the website must keep asking the server every few seconds, "Any new score?" This is like repeatedly knocking on a door to check if someone is home.
This constant asking wastes time and energy. The server gets overwhelmed with many repeated requests, and users may see delays or outdated scores. It feels slow and clunky, like waiting in line to ask the same question over and over.
Long polling and Server-Sent Events let the server tell the user immediately when there is new information. Instead of asking repeatedly, the user waits patiently, and the server sends updates as soon as they happen. This saves effort and makes updates feel instant.
while(true) { fetch('/score'); await new Promise(resolve => setTimeout(resolve, 5000)); }
const eventSource = new EventSource('/scores');
eventSource.onmessage = e => updateScore(e.data);This approach enables real-time updates that feel smooth and instant, improving user experience and reducing server load.
News websites use Server-Sent Events to push breaking news headlines instantly to readers without them refreshing the page.
Manual repeated requests cause delays and waste resources.
Long polling and Server-Sent Events let servers push updates instantly.
This makes apps faster, smoother, and more efficient.
Practice
Solution
Step 1: Understand long polling behavior
Long polling keeps the HTTP request open until the server has data, then closes it after sending the response.Step 2: Understand SSE behavior
SSE keeps a single HTTP connection open to continuously push updates without closing it.Final Answer:
Long polling closes the connection after each response; SSE keeps the connection open. -> Option AQuick Check:
Connection persistence = A [OK]
- Confusing SSE with WebSockets
- Thinking SSE uses multiple connections
- Believing long polling pushes continuously
Solution
Step 1: Identify HTTP method for SSE
SSE connections are initiated by clients using the GET method to request a stream from the server.Step 2: Confirm other methods are not used
POST, PUT, DELETE are used for data modification, not for opening SSE streams.Final Answer:
GET -> Option BQuick Check:
SSE connection method = GET [OK]
- Choosing POST because it sends data
- Confusing PUT or DELETE with SSE initiation
- Thinking SSE uses WebSocket handshake methods
while True: wait_for_event() send_response_to_client() client_reconnects()
What is the main downside of this approach compared to SSE?
Solution
Step 1: Analyze long polling loop
The server waits for an event, sends a response, then the client reconnects to wait again.Step 2: Identify overhead caused by reconnects
Each reconnect requires new HTTP handshake, increasing latency and server load.Final Answer:
It requires the client to repeatedly reconnect, causing overhead. -> Option DQuick Check:
Repeated reconnects = overhead [OK]
- Thinking long polling keeps connection open indefinitely
- Confusing TCP/UDP protocols in HTTP context
- Assuming long polling can't handle multiple clients
Solution
Step 1: Understand SSE connection behavior
SSE expects the server to keep the connection open to push updates continuously.Step 2: Identify issue with connection closing
If the server closes the connection early, the client stops receiving updates until it reconnects.Final Answer:
The server is closing the connection prematurely. -> Option CQuick Check:
Connection closed early = no updates [OK]
- Thinking client must reconnect after every message
- Confusing HTTP methods for SSE
- Assuming multiple GET requests improve SSE
A) Use long polling for all clients.
B) Use Server-Sent Events for all clients.
C) Use WebSockets for all clients.
D) Use HTTP polling every 5 seconds.Solution
Step 1: Evaluate long polling and SSE for scale
Long polling causes overhead reconnects; SSE keeps connections open but is unidirectional.Step 2: Consider WebSockets for chat
WebSockets allow two-way communication with a single persistent connection, ideal for chat apps.Step 3: Assess HTTP polling
Polling every 5 seconds causes high latency and unnecessary load.Final Answer:
WebSockets provide full-duplex communication, best for chat scalability. -> Option AQuick Check:
Best real-time chat = WebSockets [OK]
- Choosing SSE for two-way chat communication
- Thinking polling is efficient for real-time
- Assuming long polling scales better than WebSockets
Ready to prove it?
Pass the test and this pattern moves unknown
