0
0
HLDsystem_design~3 mins

Why Long polling and Server-Sent Events in HLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could magically get updates the moment they happen, without asking again and again?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
while(true) {
  fetch('/score');
  await new Promise(resolve => setTimeout(resolve, 5000));
}
After
const eventSource = new EventSource('/scores');
eventSource.onmessage = e => updateScore(e.data);
What It Enables

This approach enables real-time updates that feel smooth and instant, improving user experience and reducing server load.

Real Life Example

News websites use Server-Sent Events to push breaking news headlines instantly to readers without them refreshing the page.

Key Takeaways

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.