Definition
- Short polling : the client requests updates on a fixed interval; simple but wastes requests and adds latency.
- Long polling : the client makes a request that the server holds open until an update or timeout, then reconnects.
- SSE (Server-Sent Events) : one-way server-to-client push over a single long-lived HTTP connection with auto-reconnect.
- WebSocket : full-duplex bidirectional connection where both client and server can send at any time.
- Push notification : platform-specific delivery (APNs, FCM) that reaches a device even when the app is closed.
When Rajesh wants to know if his cheque cleared, he can keep phoning National Bank of India every few minutes : that is polling, the client pulling. Or the bank can send him an SMS the instant it clears : that is push, the server initiating. Polling wastes everyone's time if nothing changed; push delivers news immediately but requires the bank to keep a way to reach Rajesh.
Understanding the Concepts
What is short polling?
Short polling is the simplest pull: the client requests on a fixed interval. It is trivial to build and works everywhere, but it wastes requests when nothing changed and adds latency up to the polling interval.
package com.bank.realtime;
public class ShortPollingClient {
public void pollForUpdates() throws InterruptedException {
while (true) {
Update u = httpClient.get("/transfers/status?since=" + lastSeen);
if (u != null) handle(u);
Thread.sleep(5000); // fixed interval: wasted calls + up-to-5s latency
}
}
}
What is long polling?
Long polling improves on short polling: the client makes a request and the server holds it open until there is an update or a timeout, then the client immediately reconnects. This gives near-real-time delivery with ordinary HTTP.
package com.bank.realtime;
public class LongPollingServer {
public Response longPoll(String accountId, long timeoutMs) {
long deadline = System.currentTimeMillis() + timeoutMs;
while (System.currentTimeMillis() < deadline) {
Update u = checkForUpdate(accountId);
if (u != null) return Response.ok(u); // event found, return immediately
sleep(100); // brief sleep before re-checking
}
return Response.noContent(); // timeout, client reconnects
}
}
What is SSE (Server-Sent Events)?
SSE is a true push over a single long-lived HTTP connection where the server streams events to the client one way. It is simple, works over HTTP, and auto-reconnects, ideal for one-directional feeds like a live transaction ticker.
package com.bank.realtime;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
public class SseController {
@GetMapping(value = "/transactions/stream",
produces = "text/event-stream")
public SseEmitter stream(String accountId) {
SseEmitter emitter = new SseEmitter();
registry.register(accountId, emitter);
return emitter; // server pushes when events occur
}
}
What is WebSocket?
WebSocket is a full-duplex connection where both client and server can send at any time. It is ideal for interactive, bidirectional use like chat or live trading dashboards.
package com.bank.realtime;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
public class TradingWebSocket {
public void onMessage(WebSocketSession session, TextMessage msg) {
Order order = parse(msg); // client sends order
OrderResult result = execute(order);
session.sendMessage(new TextMessage(toJson(result))); // server sends result
}
}
What are mobile push notifications?
Mobile push notifications are delivered through platform services like APNs (Apple) or FCM (Google), which can reach a device even when the app is closed. Essential for fraud alerts and time-sensitive banking notifications.
How do you choose between them?
| Mechanism | Direction | Latency | Complexity | Best for |
|---|---|---|---|---|
| Short polling | Pull | Up to interval | Low | Rare, latency-tolerant |
| Long polling | Pull | Near-real-time | Medium | HTTP-compatible real-time |
| SSE | Push (one-way) | Real-time | Medium | Transaction ticker |
| WebSocket | Push (bidirectional) | Real-time | High | Chat, live trading |
| Push notification | Push (platform) | Seconds | Medium | Fraud alerts, closed app |
What about connection cost at scale?
Push connections consume server memory per client and need a presence registry. For millions of mostly idle clients, push notifications or long-polling can be cheaper than holding millions of open WebSockets.
What should a senior engineer know?
A senior engineer considers not just the latency need but the scale and cost of connections. They also know that SSE reconnects automatically (built into the spec), while WebSocket reconnection must be implemented in the client, and that load balancing WebSockets requires sticky routing to the server holding the connection.
Visual Overview
Push vs Pull Spectrum ◄─── PULL (client asks) ──────────── PUSH (server delivers) ───► Short Long SSE WebSocket Mobile Push Polling Polling (one-way) (bidirect.) (APNs/FCM) ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ req │ │ req │ │ ← │ │ ↔ │ │ ← │ │ res │ │wait │ │ │ │ │ │ │ │ req │ │ res │ │ ← │ │ ↔ │ │ ← │ │ res │ │ req │ │ │ │ │ │ │ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ Wasteful Near-RT One-way Full-duplex App closed Simple HTTP-only Auto-reco Interactive Platform svc Banking use cases: Balance check -> Short polling (rare, latency-tolerant) Transfer status -> Long polling or SSE (near-real-time) Live trading -> WebSocket (bidirectional, fast) Fraud alert -> Push notification (reaches closed app)
Key Takeaway
Push and pull describe who initiates data delivery, and I choose along that spectrum based on latency, update frequency, connection cost, and direction. Short polling is simple but wastes requests. Long polling gives near-real-time over HTTP. SSE streams one-way events over a persistent connection. WebSocket gives full-duplex for interactive use. Mobile push notifications reach a closed app.
At National Bank of India, each mechanism serves a different need. The balance check uses short polling because it is rare and latency-tolerant. Transfer status uses SSE for a real-time one-way feed. The live trading dashboard uses WebSocket for bidirectional interaction. Fraud alerts use push notifications because Priya must be reached even when the app is closed.
The senior consideration is connection cost at scale. Every open push connection consumes server memory, so for millions of mostly idle clients, push notifications or long-polling can actually be cheaper than holding a million open WebSockets. Matching the mechanism to both the latency need and the scale is what makes the delivery layer efficient.
Remember one sentence: use polling for rare updates, SSE for one-way real-time feeds, WebSocket for bidirectional interaction, and push notifications to reach closed apps.
---