
Resilience Considerations for Implementing Server-Sent Events over HTTP(S)
Best practices for building production-grade SSE systems that survive real-world network conditions
Introduction
Server-Sent Events (SSE) is a persistent HTTP connection over which a server pushes text-formatted events to a client. It operates over plain HTTP, requires no protocol upgrades, and works with existing load balancers, reverse proxies, and CDNs out of the box.
But simplicity at the protocol level does not translate to simplicity in production. Connections drop. Proxies buffer. Mobile networks switch from WiFi to cellular mid-stream. At scale, thousands of clients may reconnect simultaneously after an outage , creating thundering-herd effects that bring down the very infrastructure they're trying to reach. This post distills resilience best practices from real-world systems.
1. SSE's Built-in Resilience Primitives
The Last-Event-ID Header
When a server includes an
id: field with each event, the browser's EventSource API stores it. Upon reconnection, the client sends a Last-Event-ID header, signalling the last event it successfully processed . A well-behaved server uses this header to replay only the missed events.// Server sends:id: 42data: {"status": "processing"}
id: 43data: {"status": "complete"}
// On reconnect, client sends:GET /events HTTP/1.1Last-Event-ID: 43The retry: Field
The server can influence the client's reconnection backoff by sending a
retry: field (in milliseconds). A server under stress can instruct clients to back off further before reconnecting.Automatic Reconnection
The
EventSource API automatically attempts reconnection when a connection drops. All modern browsers follow the WHATWG spec and retry indefinitely with the server-specified or default interval. (Older Firefox versions circa 2014 only attempted once, but this was a bug long since fixed.) Despite this standardisation, production systems still implement custom reconnection logic for control over backoff, jitter, and catch-up behaviour.
2. Designing for Resumability
Assign Monotonic, Persistent Event IDs
Every event must carry a unique, ordered
id: field. This can be a monotonically increasing integer, a composite key like {stream_id}:{sequence_number} , or a timestamp-based identifier. The key constraint: the server must be able to reconstruct which events were sent after a given ID.Maintain a Server-Side Event Buffer
The server must retain a history of recently emitted events to replay them upon reconnection. Common approaches:
Approach | Pros | Cons |
|---|---|---|
In-memory ring buffer | Fast, simple | Lost on restart; memory-bound |
Redis Streams | Persistent, TTL-based eviction | Additional infrastructure |
Kafka/event log | Infinite replay, partitioned | Higher latency for small payloads |
Database | Queryable, durable | Write amplification at scale |
For LLM token streaming , the event store must be performant enough that a user who loses connection mid-generation doesn't restart their entire prompt.
Bound Your Replay Window
An unbounded replay is a denial-of-service vector. A reconnecting client with a stale
Last-Event-ID can trigger a query returning all historical events, spiking database load . Set a maximum replay window (e.g., last 5 minutes or last 1,000 events) and respond with a full-state snapshot if the client is too far behind.
3. Reconnection Strategies
Exponential Backoff with Jitter
A fixed retry interval means all disconnected clients reconnect simultaneously. Use exponential backoff with random jitter to spread reconnection attempts:
const baseDelay = 1000;const maxDelay = 30000;
function getRetryDelay(attempt: number): number { const exponential = Math.min(baseDelay * Math.pow(2, attempt), maxDelay); return exponential + exponential * Math.random();}Custom Reconnection on Error
function createResilientConnection() { let retryCount = 0; let lastEventTime = Math.floor(Date.now() / 1000);
function connect() { const source = new EventSource("/events");
source.addEventListener("message", (e) => { retryCount = 0; lastEventTime = Math.floor(Date.now() / 1000); processEvent(e.data); });
source.addEventListener("error", () => { source.close(); setTimeout(connect, getRetryDelay(retryCount++)); });
// Catch up on missed events fetch(`/events/since/${lastEventTime}`) .then(r => r.json()) .then(events => events.forEach(processEvent)); }
connect();}Distinguish Intentional Closure from Failures
When an
AbortSignal is triggered (the user navigated away), retrying makes no sense. Always check whether disconnection was intentional before entering the retry loop :if (signal?.aborted) throw error;
4. Proxy and Infrastructure Resilience
SSE connections are long-lived HTTP responses. Most HTTP infrastructure was designed for short-lived request/response cycles.
Disable Proxy Buffering
Reverse proxies like Nginx and ALBs often buffer responses before forwarding them. Explicitly disable buffering :
location /events { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ''; proxy_buffering off; proxy_cache off; proxy_read_timeout 86400s; proxy_send_timeout 86400s;}Server-side headers:
Content-Type: text/event-streamCache-Control: no-cache, no-transformX-Accel-Buffering: noSend Heartbeat Events
Many proxies close connections that appear idle. SSE comment lines (starting with
: ) serve as lightweight keepalives without triggering client-side event handlers:: heartbeatSend these every 15–30 seconds to survive aggressive proxy timeouts.
Use HTTP/2 or HTTP/3
Under HTTP/1.1, browsers enforce roughly 6 concurrent connections per domain . A single SSE connection consumes one slot. Under HTTP/2+, multiplexing raises this to ~100 concurrent streams, making SSE viable for applications that need multiple event channels.
5. Scaling SSE Horizontally
Persistent connections make horizontal scaling fundamentally different from stateless HTTP services.
Decouple Connection State from Business Logic
Shopify's BFCM architecture demonstrates the pattern: SSE gateway servers handle connection management, while business logic remains stateless behind a message bus (Kafka). This separation lets you scale compute independently of the connection layer.
Use a Pub/Sub Layer for Fan-Out
When multiple SSE server instances sit behind a load balancer, a client may reconnect to a different server. A shared pub/sub layer (Redis Pub/Sub, Kafka, NATS) ensures every server instance receives every event and can push it to any connected client.
Monitor Connection Counts Per Pod
Unlike stateless services, SSE servers consume resources proportional to connected clients, not request rate. Load test with realistic concurrent connection counts and monitor memory per connection, message throughput per instance, and connection churn.
6. Handling Reconnection Storms
When an SSE server restarts (e.g., during a deployment), all connected clients disconnect and attempt to reconnect simultaneously.
Graceful Draining
Before shutting down a server instance:
- Remove the instance from the load balancer
- Send a "please reconnect" event to connected clients
- Wait for a configurable drain period
- Terminate remaining connections
Server-Side Rate Limiting
Cap the rate of new SSE connections accepted per second. Return HTTP 503 with a
Retry-After header for excess connections:HTTP/1.1 503 Service UnavailableRetry-After: 5Stagger Retry Intervals
Use the
retry: field to assign different reconnection intervals to different clients. Include a randomised retry value between 2000–8000ms so clients naturally spread their reconnection attempts.
7. Cancellation and Cleanup
SSE is unidirectional, so the client cannot send commands back to the server over the same connection.
Out-of-Band Cancellation
For long-running streams (e.g., LLM token generation), implement cancellation via a separate HTTP endpoint and shared state store :
# Client sends: POST /cancel/{response_id}# Server-side generation loop checks:if redis_client.get(f"cancel:{response_id}") == "true": break # Stop generatingDetect Client Disconnection Server-Side
The server must detect disconnected clients to free resources. In Go, use
Request.Context() (the context is cancelled when the client disconnects); in Node.js, listen for the close event on the request object.Set Connection TTLs
Set a maximum connection lifetime (e.g., 1 hour) after which the server cleanly closes the connection. This prevents resource leaks from zombie connections and provides natural load rebalancing across the cluster.
8. Observability
Metric | Why |
|---|---|
Active connections | Capacity planning; correlates with memory |
Reconnection rate | Spikes indicate infrastructure issues |
Event delivery latency | Time from production to client receipt |
Last-Event-ID gap | Large gaps indicate slow consumers |
Heartbeat failures | Early proxy/network issue indicator |
Log every connection lifecycle event (connect, disconnect, reconnect with Last-Event-ID, error) with client identifiers to trace individual journeys through outages.
9. Security
Always Use HTTPS
SSE connections are long-lived and carry potentially sensitive data. TLS prevents intermediaries from buffering or modifying the stream.
Authenticate at Connection Time
The native
EventSource API does not support custom headers. Workarounds include cookie-based authentication, short-lived JWT tokens as query parameters, or polyfill libraries that allow Authorization headers .Validate Last-Event-ID
A malicious client could send a crafted
Last-Event-ID to trigger expensive replay queries or probe for events outside their access scope. Always validate that the claimed ID belongs to the authenticated user's stream and falls within the valid replay window.
Summary
Category | Best Practice |
|---|---|
Resumability | Monotonic event IDs; server-side event buffer; bounded replay window |
Reconnection | Exponential backoff with jitter; custom error handling; detect intentional closure |
Infrastructure | Disable proxy buffering; heartbeats; HTTP/2+ |
Scaling | Decouple connection state; pub/sub fan-out; monitor per-pod connections |
Storm mitigation | Graceful draining; rate limiting; staggered retries |
Cancellation | Out-of-band endpoint; detect disconnect; connection TTLs |
Observability | Active connections, reconnection rate, delivery latency, ID gaps |
Security | HTTPS; authenticate at connection time; validate Last-Event-ID |
SSE's strength is its simplicity. Its risk is also its simplicity. The gap between a working demo and a production system is filled by deliberate engineering: event IDs for resumability, backoff for reconnection, heartbeats for proxy compatibility, pub/sub for horizontal scale, and rate limiting for storm resilience. Implemented correctly, SSE delivers real-time data at scale with lower complexity than WebSockets and better infrastructure compatibility.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article