Skip to content

WebSocket Scaling Deep Dive

Scale WebSocket servers horizontally with load balancing, shared state, and the Pub/Sub pattern.

What you'll learn

  • Horizontal scaling with sticky sessions or shared backends
  • Redis Pub/Sub for cross-server message broadcasting
  • Connection draining and graceful shutdown
  • Memory and connection limit management

Concept

WebSocket Scaling Deep Dive

At scale (100K+ connections), you need multiple servers. The challenge: WebSocket connections are stateful - a client connects to one specific server.

The Multi-Server Problem

If Alice is on Server A and Bob on Server B, Server A must forward messages to Server B. Solutions: Redis Pub/Sub, message brokers, or server mesh.

Load Balancing

WebSocket upgrade requests need sticky sessions (cookie/IP hash) OR a shared connection registry via Redis. Without sticky sessions, any server can handle any connection.

Redis Pub/Sub Pattern

Each server subscribes to Redis channels for each room. To broadcast, publish to Redis. All servers receive and deliver to local connections. This decouples broadcasting.

Graceful Shutdown

On deploy, drain connections: stop accepting new, send close frames with reconnect hint, wait for clients to reconnect, then shut down. This prevents connection drops.

Example

              class MockRedis { constructor() { this.ch = new Map(); } publish(c, m) { (this.ch.get(c)||[]).forEach(f=>f(m)); } subscribe(c, f) { if (!this.ch.has(c)) this.ch.set(c, []); this.ch.get(c).push(f); } }
const redis = new MockRedis();
class WSServer { constructor(n) { this.n = n; this.rooms = new Map(); } join(id, r) { if (!this.rooms.has(r)) this.rooms.set(r, new Set()); this.rooms.get(r).add(id); redis.subscribe(r, m => this.deliver(r, m)); } broadcast(r, m) { redis.publish(r, JSON.stringify({...m, from:this.n})); } deliver(r, d) { const m = JSON.parse(d); const s = this.rooms.get(r); if (s) console.log(`${this.n} delivering to ${s.size}:`, m); } }
const a = new WSServer('A'); const b = new WSServer('B');
a.join('alice', 'general'); b.join('bob', 'general');
a.broadcast('general', {text:'Hello'});
            

MockRedis simulates Pub/Sub. Server A broadcasts by publishing to Redis. Both servers receive and deliver to local connections.

Try it

  • JSON Diff

    Compare message formats across servers - Redis Pub/Sub helps debug delivery.

Common mistakes

The mistake

Using sticky sessions without a shared registry.

The fix

Sticky sessions break if a server goes down. Use Redis so any server can handle reconnection.

The mistake

Not implementing graceful shutdown.

The fix

Sudden disconnects cause reconnection storms. Drain connections by sending close frames.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors