Simpler one-way push
You need server pushes but WebSockets are blocked or overkill.
Say it like this
Updates only flow server to client here, so I use server-sent events over plain HTTP and keep long polling as the fallback for networks that cut idle connections.
Basic solution
Server-sent events for one-way streams over plain HTTP; long polling as the universal fallback.
Server-sent events with replay on reconnect
One long-lived HTTP response carries events; heartbeats keep proxies happy, and Last-Event-ID lets the server replay from a short buffer after a drop.
How it works
- The client opens an ordinary HTTP GET to /events with the header Accept: text/event-stream. The server answers with status 200 and headers but never finishes the response body, so the connection stays open and becomes the channel for pushes.
- Whenever something happens for that user, the server writes a small text block onto the open response (an id line, an event line, a data line, then a blank line) and flushes it to the socket. In the browser the EventSource object parses each block and fires one message event per block.
- Every 15 to 30 seconds the server writes a comment line (a line starting with a colon, which the browser ignores). This is a heartbeat: it keeps bytes moving so proxies and load balancers do not treat the connection as idle and close it.
- If the connection drops, EventSource reconnects on its own after a short delay and sends a Last-Event-ID header carrying the id of the last block it received. The server keeps a short buffer of recent events per user, finds that id, and resends everything after it, so the client misses nothing across a blip.
- The stream only carries server-to-client data. When the client wants to send something (a message, an ack), it makes an ordinary POST to the API, exactly as it would without SSE.
Long polling, the fallback
- The client sends a GET that the server deliberately does not answer. The server holds the request open until an event arrives for that user or a timeout of about 30 seconds passes, then returns whatever it has and closes. The client immediately sends the next GET, so there is always one request waiting.
- Every event therefore costs a full HTTP request and response, including headers and a new round trip to re-establish the wait. It works through proxies that cut streaming connections, so it is the choice only when SSE is blocked, never the first choice.
Worked example
Delivery receipts and typing indicators in the chat app
- 10M users online, each receiving on average 0.2 events per second (receipts, presence, typing) and sending 0.02 messages per second.
- SSE: one open connection per user, 10M connections across the fleet; at 250K per server that is 40 connection servers. Outbound events: 2M per second, each about 150 bytes, so 300 MB per second of push traffic.
- Long polling for the same load: every event completes a request, so 2M requests per second plus a reconnect per event. Each reconnect carries 500 to 800 bytes of headers, so 1 to 1.6 GB per second of overhead on top of the payload, and the request rate is 10 times what the send path needs.
- Client sends: 200K POSTs per second either way, which the normal API fleet handles.
- Conclusion: SSE serves this at one connection per user and 200K requests per second for sends; long polling needs about ten times that request capacity for the same events.
Variants and when each wins
Server-sent events
- One open HTTP response per client that the server writes text events to; one-way only, with automatic reconnect and replay from Last-Event-ID built into the browser. It wins for feeds, notifications and receipts because it reuses ordinary HTTP infrastructure and multiplexes many streams over one HTTP/2 connection. Note the browser caps HTTP/1.1 SSE connections per origin at about six, so serve it over HTTP/2.
Long polling
- A GET the server holds until there is something to send, then the client asks again. It wins only when a corporate proxy or firewall breaks streaming connections, and only while events are rare (a few per minute per user), because each event costs a complete request.
WebSockets
- A persistent two-way connection with binary frames. It wins when the client sends often (chat typing, games, collaborative editing), at the cost of stateful servers that need sticky routing and a registry of who is connected where.
HTTP/2 or HTTP/3 streaming responses
- The same idea as SSE but with a framing you define, so you can send binary. It wins when you control both ends and need binary, for example a native app talking to your own server.
Recommendation: SSE for feeds, notifications, dashboards and receipts; WebSockets when the client talks back often; long polling only as the fallback the client switches to after SSE fails.
Failure modes
Idle timeout closes the stream
- Clients silently stop receiving events after about 60 seconds, and the only sign is that reconnect counts climb. It happens because a load balancer or proxy between client and server has an idle timeout shorter than the gap between two events, so it closes a connection that is healthy but quiet. Send a heartbeat comment every 15 to 30 seconds so bytes keep flowing, and raise the idle timeout on the balancer to a few minutes.
Reconnect storm after a deploy
- Right after you restart the stream servers, every client reconnects at the same instant and the fresh servers fall over under the surge. It happens because all connections were dropped together, so all reconnects arrive together. Drain servers one at a time rather than restarting the tier at once, send a retry field with a randomised delay, and have the client add its own jitter before reconnecting.
Events lost across reconnects
- A delivery receipt never shows up after a short network blip. It happens because the server had no buffer to replay from, so events sent while the connection was down are simply gone. Keep the last N events per user in memory or Redis and honour Last-Event-ID on reconnect; for anything that must never be lost, have the client also read from the durable inbox when it reconnects.
Buffering proxies
- Events arrive in bursts a minute late instead of one by one. It happens because a proxy or a compression layer collects response bytes into a buffer and only forwards them when the buffer fills. Disable response buffering and compression for the stream route (X-Accel-Buffering: no on nginx) and flush after every event.
Connection limit per origin
- A user opens a sixth tab and that tab never updates. It happens because HTTP/1.1 browsers allow only about six connections to one origin, and each SSE stream holds one open. Serve the stream over HTTP/2, which carries many streams on one connection, or share one stream between tabs through a shared worker.
Interviewer follow-ups
Answer out loud before you open each one.
When would you pick SSE over WebSockets?
When data flows almost entirely from server to client and the client sends rarely: feeds, notifications, order status, dashboards. SSE needs no special protocol support, reconnects on its own, and runs through the same HTTP infrastructure as the rest of the API. WebSockets earn their complexity when the client sends many small messages.
How does the client avoid missing events during a reconnect?
Every event carries an id and the browser sends Last-Event-ID on reconnect. The server keeps a short ring buffer per user, replays everything after that id, and if the id is older than the buffer it tells the client to do a full sync from the inbox or the API.
How many SSE connections can one server hold?
A few hundred thousand with an event loop runtime, since each idle connection is mostly a socket and a small buffer. Memory per connection, file descriptor limits and the balancer are the usual ceilings, so plan at 200K to 300K per server and measure.
What does long polling cost compared with SSE?
A full HTTP request per message, including headers, connection setup on HTTP/1.1, and a reconnect gap in which events queue. At one event every few seconds per user it multiplies request volume by the number of events; at one event per minute the difference barely matters.
Connections
- In study paths
- Real-time delivery
- Pairs with
- Push to the browser in real time · Find which server holds a user · Deliver to users who are offline · Spread traffic across servers
- Pulls against
- Keep servers stateless
- Used in
- Chat App (WhatsApp / Messenger)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Application Load Balancer | Load balancer | Cloud Load Balancing | Application Gateway, Azure Load Balancer | NGINX, HAProxy, Envoy |
| Amazon EC2 | Stream server | Compute Engine | Azure Virtual Machines | DigitalOcean Droplets, any VM or bare metal |
| Amazon ElastiCache | Replay buffer | Memorystore | Azure Managed Redis | Redis, Valkey, Memcached |
| Amazon EventBridge | Event bus | Eventarc | Event Grid | Knative Eventing |