Network & PerformanceBaseline · Widely

WebSockets, SSE, Reconnection Jitter & Consumer Backpressure

Mental Model

Use SSE for server-to-client feeds and WebSocketStream when duplex frames need Streams API backpressure. Pair reconnects with full-jitter backoff and AbortController to prevent retry storms.

Interactive Simulator

Stream Backpressure & Full-Jitter Reconnect

Trace how consumer backpressure throttles socket reads and how full-jitter backoff spreads reconnects.

Edge Server
ReadableStream
UI Consumer
Step 1 / 3: 1. Consumer Stalls (desiredSize <= 0)

UI renderer pauses reader.read() during a heavy frame, filling the ReadableStream highWaterMark.

if (controller.desiredSize <= 0) socketReader.pause();
Internal State / Memory
desiredSize
0
tcpWindow
shrinking
heapMb
42

Under the Hood

Legacy WebSocket.onmessage pushes every arriving TCP frame onto the main-thread event loop regardless of whether the UI can render it, growing heap memory without limit. Wrapping transport in a ReadableStream connects consumer desiredSize directly to TCP flow control.

  1. Transport Split: SSE (text/event-stream) rides standard HTTP/2 multiplexing with native Last-Event-ID resume, whereas WebSockets upgrade to a dedicated full-duplex TCP framing layer.
  2. Backpressure Propagation: When a consumer pauses reader.read(), desiredSize hits zero and WebSocketStream stops reading from the OS socket buffer, shrinking the TCP receive window.
  3. Full-Jitter Backoff: Computing random(0, min(cap, base * 2^attempt)) spreads 100,000 disconnected clients uniformly across the retry window instead of spiking at fixed powers of two.
JS
async function connectWithJitter(url, signal, attempt = 0) {
  try {
    const res = await fetch(url, { signal, headers: { Accept: 'text/event-stream' } });
    return res.body.pipeThrough(new TextDecoderStream());
  } catch (err) {
    if (signal.aborted) throw err;
    const delay = Math.random() * Math.min(30000, 500 * 2 ** attempt);
    await new Promise((r) => setTimeout(r, delay));
    return connectWithJitter(url, signal, attempt + 1);
  }
}

Problem Genealogy

  • 2011 (Raw WebSocket & Fixed setInterval): Clients buffered unbounded onmessage events in JS arrays and retried every 1000ms, crashing tabs on bursts and taking down edge proxies after deploys.
  • 2015 (HTTP/2 + SSE Adoption): Unidirectional feeds moved to EventSource, eliminating custom heartbeat frames and sharing one TCP connection per origin.
  • 2024 (WebSocketStream & fetch Streams): Native WHATWG streams unified socket reads with ReadableStream backpressure and AbortSignal teardown.

Production Pitfalls

Production Pitfall
  • Synchronized Retry Storms: Fixed or unjittered exponential backoff causes every dropped client to reconnect at the exact same second, turning a 5-second deploy blip into a multi-minute outage.
  • EventSource 6-Connection Limit on HTTP/1.1: Opening multiple EventSource tabs against an HTTP/1.1 proxy exhausts the browser's 6-connection-per-origin pool and stalls API fetch calls.
When Not to Use
  • When not to use WebSockets: Do not deploy WebSockets for server-to-client notifications, LLM token streaming, or live tickers; use HTTP/2 SSE to avoid stateful load-balancer stickiness and custom auth-header workarounds.

Connections & Logical Next Steps

Network & PerformanceWebSockets, SSE, Reconnection Jitter & Consumer Backpressure
What This Unlocks
Leaf concept

Seen in Architecture Autopsies