Skip to content
System Design Explorer
networking

WebSockets & Persistent Connections

By Ankur NayakReviewed

WebSockets keep a single, long-lived, two-way connection open between client and server, so the server can push messages the moment they happen instead of waiting for the client to poll. They power chat, live updates and real-time dashboards.

WebSockets & Persistent Connections diagramComponents: Message Service, Session Registry (Redis), Connection Server, User B. Connections: Message Service to Session Registry (where is B?); Message Service to Connection Server (deliver); Connection Server to User B.Message ServiceServiceSession RegistryRedisConnection ServerRealtime gatewayUser BClient
Pushing to a connected user

How it works

HTTP is request/response: the server can only answer when asked. A WebSocket starts as an HTTP request with an Upgrade header and then becomes a full-duplex channel over the same TCP connection, defined by RFC 6455. Either side can send frames at any time.

At scale, persistent connections change the architecture. A dedicated tier of connection servers (often called gateways) holds millions of open sockets and does little else. They must know which user is on which server, so a routing layer or session registry maps user IDs to connection servers.

Mobile apps often use the same idea over plain TCP or MQTT, and fall back to platform push (APNs, FCM) when the app is in the background and the connection is closed.

Key points

  • Server push without polling; one connection per client
  • Connection servers are stateful: track user → server
  • Heartbeats detect dead connections behind NATs and proxies
  • Clients must reconnect with backoff and resume from a cursor

When to use it, and pitfalls

Use it for

  • Chat, presence and typing indicators
  • Live location, scores, collaborative editing
  • Any feature where latency of seconds is too slow

Watch out for

  • Deploys drop every connection on a server; drain gradually and reconnect with jitter
  • Load balancers must support long-lived connections (L4 or WebSocket-aware L7)
  • Messages sent while a client is reconnecting must be buffered or re-fetched

Trade-offs

WebSockets vs. long polling vs. server-sent events

WebSockets for two-way, high-frequency traffic; SSE for one-way server updates.

Pros: WebSockets: lowest latency, bidirectional; SSE: plain HTTP, automatic reconnect

Cons: WebSockets: stateful servers, harder to load-balance; Long polling: wasteful, higher latency

Used in

Frequently asked questions

How many WebSocket connections can one server hold?

It depends on memory and how busy the connections are. Mostly idle connections are cheap: in 2012 WhatsApp reported over 2 million concurrent TCP connections on a single FreeBSD server running Erlang.

What happens to messages while a client is disconnected?

The server stores them (an inbox or an offset into a log) and the client fetches what it missed after reconnecting. For mobile apps in the background, the server also sends a platform push notification to wake the user.

Sources