System Design Space
Knowledge graphSettings

Updated: March 24, 2026 at 11:23 AM

WebSocket protocol

medium

Duplex channel over HTTP Upgrade: connection establishment and real-time messaging.

This chapter matters because with WebSocket the architecture stops being a series of independent requests and becomes a system of long-lived connections and server-side state.

In practice, it helps you design realtime paths with fan-out, backpressure, reconnect, heartbeat, and graceful shutdown in mind, which is where reliability usually breaks first.

In interviews and design discussions, it lets you treat realtime systems not as 'HTTP, but faster,' but as a separate class of operational trade-offs.

Practical value of this chapter

Realtime channel design

Guides persistent-channel architecture with fan-out, backpressure, and state control in mind.

Connection lifecycle

Covers reconnect, heartbeat, and graceful shutdown for resilient realtime systems.

Scale implications

Explains impact on load balancing, session stickiness, and infrastructure resource usage.

Interview robustness

Improves realtime case answers with explicit failure handling and ops trade-off reasoning.

RFC

RFC 6455 (WebSocket Protocol)

Core specification for handshake, framing model, connection management, and close codes.

Перейти на сайт

WebSocket is an application protocol for persistent bidirectional client-server messaging. It reduces overhead from repeated polling and fits realtime workloads where latency, channel stability, and controlled fan-out are critical.

Core WebSocket properties

Upgrade from HTTP

Session starts with an HTTP/1.1 Upgrade request, then switches into WebSocket frame exchange.

Full-duplex channel

Client and server can send events independently without request-response coupling per message.

Long-lived sessions

Single channel can stay active for hours, reducing reconnect and repeated-handshake overhead.

Liveness control

Ping/pong and heartbeat policies detect broken links quickly and support controlled recovery.

Backpressure and fan-out

Burst broadcast needs queue control and adaptive send policy for slow or unstable clients.

WebSocket frame content visualization

Frame structure defines message type, payload length, and masking rules in client-server exchange.

WebSocket frame (simplified)

2+ bytes header + payload

FIN/RSV/Opcode

8 bits

MASK + Payload len

8 bits

Extended length (optional)

16 bits

Masking key (client -> server, optional)

32 bits

Payload data (variable)

32 bits

Browser clients usually mask payload data, while server-to-client frames are normally unmasked. Large payloads use extended 16/64-bit lengths beyond the base short-length range.

WebSocket connection lifecycle

Handshake and authentication

Client initiates HTTP Upgrade, server returns 101, and auth/policy checks are applied before opening the channel.

Steady bidirectional messaging

After opening, both sides exchange message frames, heartbeats, and subscription updates.

Overload and degradation

As fan-out grows, backlog and timeout pressure rises; rate limits and backpressure become critical.

Close and reconnect

Channel is closed with code/reason and client reconnects using backoff + jitter strategy.

Upgrade and bidirectional messaging

Session starts with HTTP Upgrade and then transitions into full-duplex event streaming, without re-establishing a request on each update.

HTTP → WebSocket Upgrade

Click a step or use the controls to replay the upgrade and message exchange

State

Click "Start" to see the transition process from HTTP to WebSocket.

ClientServer
Client
-
Server

The connection is open and ready for message exchange.

Details

Key headers and message examples will appear here.

WebSocket channel dynamics under load

Step through how fan-out, delivery latency, and reconnect rate evolve under realtime traffic growth.

StepInterval 1 (1 of 7)
p95 delivery (ms)Backlog (%)Reconnect rate (%)

Phase

Stable channel

Active connections

18.0k

Message stream

95 kpps

p95 delivery

42 ms

Backlog

18.0%

Reconnect rate

0.4%

Mitigation: Baseline heartbeat

What is happening: Connections are stable: messages are delivered with low latency and minimal reconnect churn.

Abbreviations

  • kpps (kilo packets/messages per second) — thousands of messages processed per second.
  • p95 delivery — delivery latency threshold under which 95% of messages complete.

Metric decoding

  • Fan-out — one event broadcast to many active subscribers at once.
  • Backlog — queued outbound messages waiting to be delivered to clients.

Related chapter

Load Balancing

How to distribute long-lived WebSocket sessions and avoid gateway hotspots.

Open chapter

How network and routing affect WebSocket

L4/L7 routing behavior

Incorrect balancing of long-lived sessions creates hotspots and uneven gateway resource usage.

Proxy/NAT idle timeouts

Stateful middleboxes can drop idle links, so heartbeat cadence must match infrastructure timeouts.

TLS termination cost

Large numbers of active WebSocket sessions increase edge TLS and proxy CPU consumption.

Loss in mobile networks

Packet loss and flapping connectivity trigger reconnect waves that can overload auth/session layers.

Fan-out path length

More hops between producer and websocket gateway increase tail delivery latency for client updates.

Where WebSocket is most effective

  • Chats, collaborative editors, and presence services
  • Live dashboards, alerts, and realtime monitoring
  • Trading and fintech feeds with frequent updates
  • Realtime gameplay event streams
  • WebRTC signaling and low-latency command channels

Why this matters in System Design

  • WebSocket changes load shape: active long-lived connections become a first-class capacity metric.
  • Realtime systems require explicit control of backlog growth, delivery latency, and reconnect storms.
  • Balancing strategy and stickiness decisions directly affect user-visible stability and tail behavior.
  • Correct heartbeat/retry policies significantly reduce incident blast radius in unstable networks.

Common mistakes

Ignoring backpressure and broadcasting blindly, which overloads slow clients and gateway nodes.

Skipping heartbeat/timeout policy tuning and causing reconnect storms during minor network flaps.

Keeping session state only in one node memory without sticky routing or shared state fallback.

Using WebSocket as a universal API replacement without clear SLA boundaries for request vs stream paths.

Related chapters

Enable tracking in Settings