Frontend System Design · Lesson 44 of 86 · 4 min
Scaling WebSocket Connections
This is a classic system design question. Get this right, and you demonstrate a grasp of stateful vs. stateless systems, a critical distinction for senior roles.
After this, you'll be able to explain why scaling WebSockets is different from scaling HTTP, and you'll have a specific, defensible architecture to propose: a load balancer with sticky sessions in front of a server cluster communicating via a pub/sub backplane.
Why You Can't Scale WebSockets Like HTTP
Scaling a stateless HTTP API is straightforward. A load balancer can distribute incoming requests to any available server in a pool using a simple round-robin strategy. Each request is independent.
WebSockets are stateful. The connection is persistent. When a client sends a message, it expects a response or action from the same server it's connected to. If a load balancer sends a subsequent packet from that client to a different server, that server has no context for the connection, and the connection breaks.
This is the core problem: the client is "stuck" to a single server. So how do you run more than one server?
Step 1: Sticky Sessions at the Load Balancer
The first piece of the puzzle is configuring your load balancer for "sticky sessions," also known as session affinity. This tells the load balancer to route all packets from a given client to the same destination server for the duration of their session.
This is typically done by hashing the client's IP address or using a cookie set after the initial connection.
The trap: Candidates often forget this or suggest a simple round-robin load balancer. This is an immediate red flag. Without sticky sessions, your WebSocket connections will be unstable and constantly drop as they get routed to different servers that don't share their state.
In an interview, mention that for WebSockets, you might prefer a Layer 4 (TCP) load balancer over a Layer 7 (HTTP) one. After the initial HTTP upgrade handshake, the connection is just a TCP stream. A Layer 4 balancer is simpler and more performant as it doesn't need to inspect application-level data.
Step 2: The Pub/Sub Backplane
Sticky sessions ensure a client can talk to its server. But what if that server needs to broadcast a message to clients connected to other servers? A chat message sent by User A (on Server 1) needs to reach User B (on Server 2). Server 1 has no direct knowledge of the connections on Server 2.
This is where a publish/subscribe (pub/sub) message bus comes in. It acts as a central communication channel, or "backplane," for all your WebSocket servers.
The flow looks like this:
- A client sends a message to its connected server (e.g., Server A).
- Server A receives the message and publishes it to a channel on the pub/sub system (e.g., a Redis channel named
chat-room-123). - All other servers (B, C, D...) are subscribed to that same channel. They receive the message from the pub/sub system.
- Each server then forwards the message to all of its own connected clients that care about it.
Common choices for a pub/sub system are Redis Pub/Sub (fast, simple) or Apache Kafka (more durable, complex). For most real-time chat or notification use cases, Redis is a perfectly good starting point.
The Complete Architecture
Putting it all together, a scalable WebSocket architecture has two key components working in tandem: a sticky load balancer distributing connections and a pub/sub system enabling cross-server communication.
This design allows you to add or remove WebSocket servers (horizontal scaling) without disrupting the overall system. Each server remains a self-contained unit managing its own connections, using the pub/sub backplane to coordinate with the rest of the cluster.
How It Works in Code
This Node.js example shows a single server instance that uses ioredis to connect to a Redis pub/sub backplane. You can run multiple instances of this code on different ports, and they will communicate with each other through Redis.
// server-scaled.ts
import { WebSocketServer, WebSocket } from 'ws';
import Redis from 'ioredis';
// Each server needs a unique ID and a port
const SERVER_ID = process.env.SERVER_ID || 'server-1';
const PORT = parseInt(process.env.PORT || '8081');
const REDIS_CHANNEL = 'global-chat';
const wss = new WebSocketServer({ port: PORT });
const clients = new Map<string, WebSocket>();
let clientCounter = 0;
// One client for publishing messages to Redis
const redisPublisher = new Redis();
// A separate client for subscribing to channels
const redisSubscriber = new Redis();
// Subscribe this server instance to the global channel
redisSubscriber.subscribe(REDIS_CHANNEL, (err) => {
if (err) {
console.error(`Failed to subscribe to Redis channel: ${err.message}`);
} else {
console.log(`${SERVER_ID} subscribed to "${REDIS_CHANNEL}"`);
}
});
// Handle messages received from the Redis channel
redisSubscriber.on('message', (channel, message) => {
if (channel === REDIS_CHANNEL) {
console.log(`${SERVER_ID} received message from Redis: ${message}`);
// Broadcast the message to all clients connected to THIS server instance
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
});
wss.on('connection', (ws) => {
const clientId = `${SERVER_ID}-client-${clientCounter++}`;
clients.set(clientId, ws);
console.log(`${clientId} connected. Total on this server: ${clients.size}`);
ws.on('message', async (message: Buffer) => {
const messageText = message.toString();
console.log(`${clientId} sent message: ${messageText}`);
const payload = JSON.stringify({
sender: clientId,
text: messageText,
});
// Publish the message to Redis for all other servers to receive
await redisPublisher.publish(REDIS_CHANNEL, payload);
});
ws.on('close', () => {
clients.delete(clientId);
console.log(`${clientId} disconnected. Total on this server: ${clients.size}`);
});
ws.on('error', (error) => {
console.error(`${clientId} error:`, error);
clients.delete(clientId);
});
});
console.log(`${SERVER_ID} WebSocket server started on port ${PORT}`);
To see this in action, you need a running Redis instance. Then, in two separate terminals:
SERVER_ID=server-A PORT=8081 npm startSERVER_ID=server-B PORT=8082 npm start
Now, if you connect a WebSocket client to port 8081 and another to 8082, a message sent by one will be received by the other.
Try it
Get the code running with two server instances as described above. Use a command-line tool like wscat to connect one client to ws://localhost:8081 and another to ws://localhost:8082. Send a message from one client and watch it appear on the other. Then, kill one of the server processes and observe how its connected client disconnects.
Next: We move from the backend to the frontend with Responsive Images: srcset, sizes and picture.
Lock it in — apply this lesson under interview conditions.