π The Classic Request-Response Model (and Its Limitations)
How Standard Web Apps Work
In a typical web app:
- A client (browser/app) sends a request to the server.
- The server processes it (DB access, computation, etc.).
- The server sends back a response.
- The connection closes.
This cycle is simple and efficient for most applications.
π But hereβs the key problem:
Once the response is done, the server cannot send fresh data to the client unless the client asks again.
Example: A Stock Market App
Suppose you have a simple stock application:
- π§βπ» Clients A, B, C connect and request current stock prices.
- π‘ The server responds β and bam! connection closes.
- π Later, prices change on the server.
- But clients A, B, C still only have old (stale) data.
This becomes a real-time problem:
π How does the server tell clients that data has changed?
π Solution 1: WebSockets
WebSockets let you keep a persistent full-duplex connection open between clients and servers.
What Does This Mean?
Instead of:
Client β Server β Response β Connection closes
WebSockets keep the connection open:
Client β Server β Client β Server
This allows:
- The server to push updates anytime.
- The client to send data anytime.
- Both sides talk without closing the connection.
How It Works (Simple Diagram)
Client Server
| β WebSocket handshake β |
| |
| β Accept & open channel β |
| |
| β Updates can flow both β |
| |
Once the connection is open, either side can send data.
Pros of WebSockets
β
Real real-time updates
β
Low latency
β
Full duplex (two-way communication)
Cons of WebSockets
β Hard to scale β itβs stateful (server must remember every connected client)
β If you have millions of connections, scaling horizontally becomes expensive
β Servers must synchronize updates among themselves in clustered systems
π Solution 2: Polling
Polling is the simplest alternative to WebSockets.
What Is Polling?
Instead of keeping a connection alive, the client asks the server again and again:
Client: βAny new updates?β
Server: βNope.β
Client: βAny new updates?β
Server: βYes β here you go!β
Simple Polling Example
Letβs say the client checks every 2 seconds:
0s β βGive me new dataβ
2s β βGive me new dataβ
4s β βGive me new dataβ
β¦
If new data appears at 3.5s, the client will only get it at the next poll (4s).
π That means the maximum delay is equal to your poll interval β 2 seconds in this example.
Pros of Polling
β
Easy to implement
β
Works with load balancers and many servers
β
Stateless β each request is independent
Cons of Polling
β Not truly real-time
β Can waste requests if no new data
β Frequent polling may still add network load
π Solution 3: Long Polling
Long polling is an optimized form of polling.
What Is Long Polling?
Instead of responding immediately, the server holds the request open until:
- New data arrives, or
- A timeout expires
Then it responds with data in one shot.
Example: Long Polling for 5 Seconds
Client β Server: βAny updates?β
Server: Hold request for 5 seconds
If updates come within 5s:
Server β Client: Latest updates
Then client immediately re-requests.
Pros of Long Polling
β
Fewer requests than short polling
β
More βreal-timeβ feel than simple polling
β
Still stateless
Cons of Long Polling
β Can still hold server resources
β Not as instant as WebSockets
β Server must manage held requests
π Comparing the Approaches
| Technique | Real-Time | Scalability | Server Load | Complexity |
|---|---|---|---|---|
| Polling | Moderate (delayed) | π₯ Easy | π₯ Medium | π’ Easy |
| Long Polling | Good | π₯ Good | π₯ Medium | π‘ Moderate |
| WebSockets | Excellent | π» Hard | π» High | π‘ Moderate |
π§ Real-World Considerations
Do You Always Need Full Real-Time?
Not always.
For example, in a stock chart app:
- You might only need fresh price updates, not two-way communication.
- Buying/selling can still happen via regular POST API routes.
That means:
- WebSockets might be overkill.
- Polling or long polling might be perfectly fine.
Why Polling Works Well with Load Balancers
When you scale with many backend servers and a load balancer:
- Polling requests get distributed across servers,
- You avoid being tied to one server connection,
- If a server goes down, your next poll goes to another healthy server.
π My Final Thoughts
Real-time systems arenβt magic β theyβre about choosing the right tool for the job:
πΉ Need instant push updates? β WebSockets
πΉ Need lightweight, scalable updates? β Polling / Long Polling
πΉ Want a mix of both? β Start with polling, evolve as needed
Every choice has trade-offs. Understanding the fundamental communication patterns helps you make the best architectural decision β and prevents unnecessary complexity early on.
Top comments (0)