Day 05 Mastery

Scaling WebSockets

Redis adapter, sticky sessions, load balancing, production deploy. Part of the free WebSockets in 5 Days course at Precision AI Academy.

~1 hour Hands-on Precision AI Academy

Today's Objective

During the WebSocket handshake, the initial HTTP request must reach the same server as subsequent WebSocket frames.

01

Why Scaling WebSockets Is Hard

A single Node.js WebSocket server can handle thousands of connections, but as you scale horizontally (multiple servers), a problem emerges: Socket.io events are in-memory. If user A is on Server 1 and user B is on Server 2, they can't communicate by default because Server 1 doesn't know about Server 2's connections.

The solution is a shared message broker — typically Redis — that all servers subscribe to. When Server 1 emits an event, Redis delivers it to Server 2, which forwards it to its connected clients.

02

Redis Adapter Setup

Socket.io with Redis Adapter
Socket.io with Redis Adapter
const { Server } = require('socket.io');
const { createAdapter } = require('@socket.io/redis-adapter');
const { createClient } = require('redis');

async function createServer() {
  const pubClient = createClient({ url: process.env.REDIS_URL });
  const subClient = pubClient.duplicate();

  await Promise.all([pubClient.connect(), subClient.connect()]);

  const io = new Server(httpServer);
  io.adapter(createAdapter(pubClient, subClient));

  return io;
}
// Now io.emit() automatically reaches ALL server instances
03

Sticky Sessions for Load Balancers

During the WebSocket handshake, the initial HTTP request must reach the same server as subsequent WebSocket frames. Configure your load balancer to use sticky sessions (also called session affinity) based on a cookie or IP hash.

nginx — Sticky Sessions
nginx — Sticky Sessions
upstream websocket_servers {
  ip_hash; # Sticky session by IP
  server ws-server-1:3000;
  server ws-server-2:3000;
  server ws-server-3:3000;
}

server {
  location /socket.io/ {
    proxy_pass http://websocket_servers;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
ℹ️
Production checklist: Redis adapter + sticky sessions + connection limits per process + monitoring connection count + graceful shutdown that drains connections before stopping.

Supporting References & Reading

Go deeper with these external resources.

Docs
Scaling WebSockets Official documentation for websockets.
GitHub
Scaling WebSockets Open source examples and projects for Scaling WebSockets
MDN
MDN Web Docs Comprehensive web technology reference

Day 5 Checkpoint

Before moving on, confirm understanding of these key concepts:

Course Complete
Return to Course Overview