Codeplant IT Solution
Codeplant IT Solution
Custom Software & AI Engineering

Architecting Real-Time Web Applications with WebSockets, Redis Pub/Sub & Node.js

Category: Web Architecture | Reading Time: 2 min read

A deep technical blueprint for scaling real-time web applications to 100,000+ concurrent connections. Master WebSocket connection pooling, Redis Pub/Sub horizontal clustering, heartbeat ping/pong protocols, and reconnection backoff.

Traditional HTTP request-response cycles fail when applications require sub-second collaborative experiences: live chat, algorithmic auction bidding, multi-player whiteboarding, delivery driver telemetry, and dynamic financial ticker feeds.

WebSockets establish persistent, full-duplex TCP connections between client browsers and backend servers. However, scaling WebSockets beyond a single server introduces severe distributed systems challenges. When User A connects to Server 1 and User B connects to Server 2, how do messages route seamlessly between them without cross-server connection dropouts?

Solving multi-node real-time scalability requires pairing high-concurrency Node.js WebSocket gateways with a clustered Redis Pub/Sub message broker.

1. Core Industry Challenges & Architectural Dilemmas

  • Horizontal Socket Cluster Synchronization: Routing broadcast messages to users connected across disparate cluster instances without broadcast storms.
  • Memory Consumption per Socket: Linux kernel socket buffers and Node.js event listeners consuming 20KB to 50KB RAM per open connection.
  • Connection Stampedes on Server Restarts: Thousands of clients reconnecting simultaneously following a deployment, taking down backend gateways.
  • Load Balancer Session Stickiness & Upgrade Handshakes: Ensuring proxy servers (NGINX, AWS ALB) support persistent HTTP 101 Switching Protocols upgrades.

2. Technical Comparison & Architectural Trade-offs

Connection VolumeInfrastructure TopologyBroker ArchitectureMemory Footprint
1k - 10k SocketsSingle Node.js ProcessIn-Memory Event Emitter~250MB RAM
10k - 50k SocketsNode.js Cluster (PM2)Local Redis Instance~1.2GB RAM
50k - 250k SocketsKubernetes Auto-Scaled PodsRedis Cluster / Valkey~6GB RAM across pods
250k+ SocketsDistributed Anycast GatewaysApache Kafka / Redis SentinelDistributed Mesh

3. Production Implementation Blueprint & Code Pattern

Connecting Node.js WebSocket server instances via Redis Pub/Sub ensures seamless message distribution across distributed server instances:

// Node.js + Redis Pub/Sub Multi-Server WebSocket Gateway
const { WebSocketServer } = require('ws');
const Redis = require('ioredis');

const pub = new Redis(process.env.REDIS_URL);
const sub = new Redis(process.env.REDIS_URL);
const wss = new WebSocketServer({ port: 8080 });

// Subscribe to global room channels
sub.subscribe('chat:global', () => console.log('Subscribed to Redis chat:global'));

sub.on('message', (channel, message) => {
  // Broadcast Redis message to all clients connected to this specific Node instance
  wss.clients.forEach(client => {
    if (client.readyState === 1) client.send(message);
  });
});

wss.on('connection', (ws) => {
  ws.on('message', (data) => {
    // Publish incoming message to Redis to distribute to all other servers
    pub.publish('chat:global', data.toString());
  });
});