What it is
Server-Sent Events (SSE), defined in the WHATWG HTML standard, is a way for a server to push a stream of text events to a client over a normal HTTP response. The browser API is EventSource: it sends a GET with Accept: text/event-stream, the server answers 200 with Content-Type: text/event-stream and keeps writing to the response instead of ending it.
Why it exists
Many features only need the server to tell the client that something changed: prices, notifications, build logs, LLM token streams, dashboards. Polling for that wastes requests and adds delay; WebSockets add a second protocol and more machinery than a one-way feed needs. SSE uses plain HTTP and adds reconnect and resume on top.
How it works
The stream. Each event is a few field: value lines followed by a blank line:
id: 42
event: price
data: {"symbol":"ACME","price":101.5}
data may span several lines; event names a type the client can listen for; id sets the last event id; retry: 3000 tells the client how long to wait before reconnecting. Lines starting with : are comments, used as heartbeats. The format is UTF-8 text only.
Reconnect and resume. If the connection drops, EventSource waits the retry delay and reconnects automatically, adding Last-Event-ID with the last id it received. A server that can look events up by id replays what was missed and continues. A 204 or a non-event-stream response stops reconnection.
Server side. Each open stream is a long-lived response. Event-loop servers (Node.js, Go, async frameworks) hold many cheaply; thread-per-request servers tie up a thread per stream. Servers subscribe to a pub/sub channel or event log and write new events to the relevant streams. For resumption across servers, events go into a shared, ordered log (Redis Streams, Kafka, a database table keyed by id) with enough retention to cover typical disconnects.
Polling and long polling, for comparison. Short polling sends a request every n seconds: load = clients ÷ n, latency up to n. Long polling holds each request until there is news (or a timeout), then the client asks again: latency drops to about one network trip, load becomes about one request per update per client.
When to use it
- Server-to-client feeds: notifications, live scores and prices, progress and log streaming, LLM response streaming.
- When you want standard HTTP: existing authentication (cookies, headers via fetch-based clients), proxies, CDNs, HTTP/2 multiplexing.
- When automatic reconnect and resume matter more than client-to-server latency.
When not to
- Frequent client-to-server messages or binary data: WebSockets (or WebTransport).
- Very rare updates where holding connections costs more than an occasional poll, or mobile apps in the background (use push notifications).
- Servers or proxies that cannot stream responses without buffering.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Short polling | Simplest, stateless, cacheable | Load ∝ clients × rate; staleness up to the interval |
| Long polling | Low latency, works everywhere | A full HTTP exchange per update per client; timeouts to tune |
| SSE | One response per client, built-in reconnect and resume, plain HTTP | One-way, text only; long-lived connections to manage |
| WebSockets | Two-way, binary, lowest per-message overhead | Separate protocol, own reconnect/resume logic, harder through some proxies |
Alternatives
- WebSockets for bidirectional messaging.
- Streaming fetch (
ReadableStreamover a chunked or HTTP/2 response) for custom formats, at the cost of writing reconnect yourself. - WebTransport for multiplexed streams and datagrams over HTTP/3.
- Web Push / mobile push for notifications when the page or app is not open.
Failure modes
- Lost events on reconnect: the reconnect lands on a server that never saw the missed events, or history was not kept.
- Idle timeouts: proxies and load balancers close quiet streams; without heartbeats, clients churn.
- Buffering: a proxy or compression layer buffers the response, so events arrive late in bursts.
- Connection limit on HTTP/1.1: browsers allow six connections per host shared across tabs; extra streams block.
- Reconnect storms: after a deploy every client reconnects at once; send a
retry:value and add server-side jitter or gradual draining. - Thread exhaustion on servers that hold a thread per open stream.
In production
Serve SSE over HTTP/2 or HTTP/3. Disable buffering for the stream route (for nginx, proxy_buffering off or the X-Accel-Buffering: no response header) and avoid compression that waits for full blocks. Send comment heartbeats every 15–30 s, below the shortest idle timeout on the path. Give every event a monotonically increasing id and keep a shared log with a retention window; if a client asks for an id older than the window, send a "reload" event so it fetches a fresh snapshot. Use an async server, cap streams per client, and drain gradually on deploys.
Common misconceptions
- “SSE is just long polling.” Long polling ends the response after each update; SSE keeps one response open for many events.
- “SSE needs special servers.” Any server that can flush a streaming HTTP response works.
- “EventSource can’t send headers, so SSE can’t be authenticated.” Cookies work with EventSource, and fetch-based SSE clients can send any header.
- “WebSockets are always better for real time.” For one-way feeds SSE is simpler and resumes on its own.
Interview angle
When a design needs live updates, compare the options out loud: polling (cost = clients ÷ interval), long polling, SSE and WebSockets, and pick by direction and frequency. For SSE, mention Last-Event-ID resumption backed by a shared log, heartbeats against idle timeouts, HTTP/2 for connection limits, and what happens when a server with many open streams is redeployed.
Model assumptions
The request load is simulated; the exchanges are scripted. In the simulation each request is one HTTP request costing about 20 ms of worker time (lognormal, cv 0.4) on two API servers with 8 workers each (about 800 req/s together). The traffic levels are derived, not emergent: 2,400 tabs, updates about every 10 s, polling every 4 s (600 req/s), long polling one request per tab per update (240 req/s), and SSE streams recycled about every 5 minutes (about 8 req/s). Held requests and open streams are assumed cheap (an event-loop server) and are not simulated, so latency metrics for long polling and SSE do not include the holding time. The scripted packets use 15 ms between tabs and the load balancer and 2 ms inside the data centre, and a 500 ms retry delay to keep the animation short; browsers default to a few seconds. Numbers are illustrative.