HTTP cannot push. That is the whole problem.
HTTP is request-response: the client asks, the server answers, connection closes. A chat message arrives, a wicket falls, a stock ticks: the server knows, the client does not, until it asks. Every real-time technology is a workaround for this one fact.
The four options, oldest to newest
- Short polling: client asks every N seconds "anything new?" Simple, works everywhere, but ~99% of responses are empty and latency is up to N seconds. At 1M clients polling every 5s, that is 200k RPS of mostly "no".
- Long polling: client asks; the server holds the request open until there is data or a timeout (~30s), then the client immediately re-asks. Near-real-time with plain HTTP; but each message still costs connection churn, and proxies/firewalls sometimes kill long-held connections.
- Server-Sent Events (SSE): one persistent HTTP connection over which the server streams events as text. One-way only (server to client), auto-reconnect built into the browser. Perfect for scores, notifications, price tickers, live blogs.
- WebSocket: starts as HTTP, then "upgrades" the same connection to a persistent, full-duplex channel: both sides send frames anytime, with tiny per-message overhead (no headers per message). The standard for chat, multiplayer games, collaborative editing, live cursors.
Why WebSockets are hard to scale
HTTP servers are stateless: any server can take any request. WebSocket servers are stateful: the connection lives on one specific server, which must remember it. At 1M concurrent connections:
- Spread connections across many WS servers (one tuned box holds tens of thousands of connections; the limits are memory and file descriptors, not CPU).
- Cross-server delivery: user A (server 1) messages user B (server 2). Server 1 publishes to a shared bus (Redis pub-sub / Kafka); every WS server subscribes and delivers to its own local connections. This is the piece interviewers probe.
- Presence/connection state: which user is connected where: Redis with heartbeat TTLs.
- Dead connections: clients vanish silently (phone in a tunnel). Heartbeat/ping-pong frames every ~30s detect and reap them.
- Reconnection + missed messages: on reconnect, the client sends "last message I have is ID 4,521" and the server backfills. Without this, tunnels eat messages.
Choosing (the honest guide)
- Server-to-client updates, want simple and firewall-friendly: SSE.
- Two-way, high-frequency (chat, games, collab): WebSocket.
- Updates every few minutes, small scale: short polling is legitimate engineering; do not gold-plate.
- Mobile push when the app is closed: none of these; that is FCM/APNs (see the notifications lesson).
Worked example: live cricket score for 2M fans
One-way updates only: SSE from edge servers. Score updates published to Redis pub-sub; 200 edge-connected SSE servers subscribe and fan out to their local clients. 2M connections / 200 servers = 10k connections each: comfortable. Total origin traffic: one event, not 2M requests.
Common interview traps
- WebSocket with no cross-server delivery plan.
- Connection state kept only in server memory.
- No heartbeat/reconnect/backfill story.
- Using WebSocket where SSE or polling would do.
Interview Q&A
Q: A user's chat works on WiFi but loses messages on mobile data. Why?
A: Mobile networks silently drop idle TCP connections. Missing heartbeats mean the client does not notice the dead socket, and there is no backfill on reconnect. Fix: ping/pong every ~30s and a last-seen-message-ID handshake on reconnect.
Q: Why not just use Kafka between clients and servers?
A: Kafka is a server-side log, not a client protocol: browsers cannot speak it, and per-client offsets for millions of users is the wrong tool. Pattern: WebSocket/SSE at the edge, Kafka as the internal backbone between services.
HTTP push nahi kar sakta. Poori problem yehi hai.
HTTP request-response hai: client poochta hai, server jawab deta hai, connection band. Chat message aaya, wicket giri, stock tick hua: server jaanta hai, client nahi, jab tak wo poochhe nahi. Har real-time technology isi ek fact ka workaround hai.
Chaar options, purane se naye
- Short polling: client har N second poochta hai "kuch naya?" Simple, har jagah chalta hai, par ~99% responses khaali hain aur latency N second tak hai. 1M clients har 5s poll karein to 200k RPS, zyadatar "no".
- Long polling: client poochta hai; server request ko hold karta hai jab tak data na aaye ya timeout (~30s), phir client turant dobara poochta hai. Plain HTTP se near-real-time; par har message par connection churn, aur proxies/firewalls kabhi kabhi lambi connections kaat dete hain.
- Server-Sent Events (SSE): ek persistent HTTP connection jis par server events stream karta rehta hai. Sirf one-way (server to client), browser mein auto-reconnect built-in. Scores, notifications, price tickers, live blogs ke liye perfect.
- WebSocket: HTTP se shuru, phir wahi connection "upgrade" ho jaata hai persistent, full-duplex channel mein: dono taraf kabhi bhi frames bhejo, per-message overhead minimal (har message par headers nahi). Chat, multiplayer games, collaborative editing, live cursors ka standard.
WebSockets scale karna mushkil kyun hai
HTTP servers stateless hain: koi bhi server koi bhi request le sakta hai. WebSocket servers stateful hain: connection ek specific server par zinda hai, use yaad rakhna padta hai. 1M concurrent connections par:
- Connections ko kayi WS servers mein baanto (ek tuned box tens of thousands connections rakhta hai; limit memory aur file descriptors ki hai, CPU ki nahi).
- Cross-server delivery: user A (server 1) ne user B (server 2) ko message bheja. Server 1 shared bus (Redis pub-sub / Kafka) par publish karta hai; har WS server subscribe karke apne local connections ko deliver karta hai. Yehi hissa interviewers probe karte hain.
- Presence/connection state: kaun user kahan connected: Redis mein heartbeat TTLs ke saath.
- Dead connections: clients chupchaap gayab ho jaate hain (tunnel mein phone). Har ~30s heartbeat/ping-pong frames unhe detect aur reap karte hain.
- Reconnection + missed messages: reconnect par client bhejta hai "mere paas last message ID 4,521 hai" aur server backfill karta hai. Iske bina tunnels messages kha jaati hain.
Kya choose karein (imaandaar guide)
- Server-to-client updates, simple aur firewall-friendly chahiye: SSE.
- Two-way, high-frequency (chat, games, collab): WebSocket.
- Updates har kuch minute mein, chhota scale: short polling bhi legitimate engineering hai; gold-plating mat karo.
- App band ho to mobile push: ye sab nahi; wo FCM/APNs hai (notifications lesson dekho).
Worked example: 2M fans ke liye live cricket score
Sirf one-way updates: edge servers se SSE. Score updates Redis pub-sub par; 200 edge-connected SSE servers subscribe karke apne local clients ko fan-out karte hain. 2M connections / 200 servers = 10k connections each: comfortable. Origin traffic: ek event, 2M requests nahi.
Common interview traps
- WebSocket, par cross-server delivery ka plan nahi.
- Connection state sirf server memory mein.
- Heartbeat/reconnect/backfill ki koi story nahi.
- Jahan SSE ya polling kaafi ho wahan WebSocket.
Interview Q&A
Q: User ka chat WiFi par chalta hai par mobile data par messages kho jaate hain. Kyun?
A: Mobile networks idle TCP connections chupchaap drop karti hain. Heartbeats missing hain to client ko dead socket ka pata nahi chalta, aur reconnect par backfill nahi hai. Fix: har ~30s ping/pong aur reconnect par last-seen-message-ID handshake.
Q: Clients aur servers ke beech Kafka hi kyun nahi?
A: Kafka server-side log hai, client protocol nahi: browsers uski bhasha nahi bolte, aur millions users ke per-client offsets galat tool hai. Pattern: edge par WebSocket/SSE, services ke beech internal backbone ke roop mein Kafka.