System DesignAdvancedarticle

WebSocket Fleet Management: Handling Millions of Connections

How to manage state for millions of long-lived WebSocket connections across multiple gateway nodes.

Sachin SarawgiApril 20, 20264 min read4 minute lesson

WebSocket Fleet Management

WebSocket systems look easy in local testing and become complex in production when you handle millions of long-lived connections across regions, devices, and flaky mobile networks.

The challenge is not only sending messages. It is managing connection lifecycle, routing correctness, backpressure, and failure recovery at fleet scale.

Core constraints of WebSocket at scale

Each active connection consumes:

  • file descriptor
  • memory for connection/session state
  • heartbeat and keepalive overhead
  • CPU for TLS and frame processing

Multiply by millions and your gateway layer becomes a stateful distributed system.

A proven high-level design:

  1. Edge/gateway layer terminates WebSocket connections
  2. Connection registry tracks user_id -> gateway_node + connection_id
  3. Message router resolves target recipients and dispatches
  4. Durable event log/queue for reliability and replay where needed
  5. Presence/heartbeat subsystem for online status

Redis can work for connection registry initially, but large deployments often evolve to sharded stores and streaming backplanes.

Connection affinity and load balancing

Use L4/L7 balancing with session affinity for upgrade handshake stability.

Important considerations:

  • avoid frequent rebalance that drops active sockets
  • scale by adding nodes and draining gracefully
  • keep connection distribution even across nodes

A node with hot tenants/channels can become overloaded despite similar connection counts.

Presence tracking model

Presence is eventually consistent, not transactional truth.

Typical approach:

  • gateway writes heartbeat timestamp per connection
  • background sweeper expires stale connections
  • user online state derived from any active connection

Represent:

  • user with multiple devices
  • multiple tabs per device
  • per-tenant/per-room presence scopes

Message routing patterns

For direct messages:

  • lookup recipient connection location
  • route to owning gateway node
  • enqueue if user offline (optional)

For fan-out channels:

  • maintain channel membership index
  • publish once to topic/backplane
  • interested gateway nodes push to local sockets

Avoid N x M cross-node chatter by partitioning topic ownership intelligently.

Delivery guarantees and ordering

WebSocket itself does not guarantee end-to-end business delivery semantics.

Define explicitly:

  • at-most-once vs at-least-once
  • per-conversation ordering vs global ordering
  • ack and retry behavior

If product needs durable delivery, pair WebSocket push with persistent store and message IDs.

Backpressure handling

Some clients read slowly or lose network quality. Without backpressure controls they can exhaust gateway memory.

Controls to implement:

  • max outbound buffer per connection
  • drop/coalesce low-priority events
  • disconnect chronic slow consumers
  • apply per-user/per-room message rate limits

Protect cluster health before preserving every low-value event.

Connection lifecycle and draining

Deployments and autoscaling should not hard-kill sockets.

Graceful node drain flow:

  1. stop accepting new upgrades
  2. notify clients with reconnect hint
  3. wait bounded drain window
  4. close remaining sockets cleanly

Clients should implement exponential backoff reconnect with jitter to avoid reconnect storms.

Multi-region architecture

For global products:

  • connect clients to nearest region for latency
  • keep conversation/topic ownership model clear across regions
  • replicate only required state, not all transient connection details

Cross-region real-time routing can be expensive; often you want region-local fan-out plus selective inter-region bridges.

Security and abuse controls

WebSocket endpoints need strong guardrails:

  • authenticated handshake with expiring tokens
  • authorization checks for channel subscribe/publish
  • payload size limits
  • per-IP and per-identity connection caps
  • bot/abuse detection signals

Do not trust client-sent room/user metadata.

Observability for WebSocket fleets

Track at minimum:

  • active connections per node/region
  • connect/disconnect rate
  • message e2e latency
  • dropped messages by reason
  • slow-consumer disconnect count
  • reconnect storm indicators

Without these, incidents become guesswork.

Common failure modes

  • thundering herd reconnect after deploy
  • centralized registry hotspot for large tenants
  • unbounded per-connection buffers
  • stale presence due to missed disconnect events
  • channel fan-out spikes overloading single nodes

Design for these from day one if your product depends on real-time UX.

Practical scaling milestones

  • <100k connections: Redis registry + stateless gateway often sufficient
  • 100k-1M: shard registry, tune kernel/network limits, introduce backplane partitioning
  • 1M+: region-aware routing, dedicated presence pipeline, strong operational automation

The architecture should evolve with traffic shape, not just connection count.

Final takeaway

WebSocket fleet management is a distributed control-plane problem disguised as a networking feature. Systems that succeed treat connection state, routing, and backpressure as first-class architecture with strict operational discipline.

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

More in System Design

Category-based suggestions if you want to stay in the same domain.