Course contentsShow
← Node.js
Lesson 1159 of 1,98010. Real-time Communication and WebsocketsFree lesson

Handling Socket Reconnection Storms

You will implement jittered connection delays to prevent thundering herd problems during infrastructure recovery.

In this lesson, you will learn how to prevent a "thundering herd" of clients from crashing your server by implementing jittered connection delays during infrastructure recovery.

The Thundering Herd Problem

When a WebSocket server or load balancer goes down, thousands of connected clients lose their connection simultaneously. If every client is programmed to reconnect immediately—or worse, on a fixed interval like exactly every 5 seconds—they will all attempt to re-establish a connection at the exact same millisecond. This creates a massive spike in traffic that can crash your server again before it even has a chance to stabilize.

Introducing Jitter

To solve this, we introduce jitter: a randomized delay added to your reconnection logic. Instead of every client retrying at exactly 5000ms, we add a random offset to that duration. This spreads the reconnection load across a wider window of time, allowing your infrastructure to recover gracefully.

Implementing Jittered Reconnection

In your Node.js client logic, you can implement a simple exponential backoff combined with a random jitter factor:

function getReconnectionDelay(attempt) {
  const baseDelay = 1000; // 1 second
  const maxDelay = 30000; // 30 seconds
  
  // Exponential backoff: 1s, 2s, 4s, 8s...
  const exponentialDelay = Math.min(maxDelay, baseDelay * Math.pow(2, attempt));
  
  // Add jitter: randomize by up to 20% of the delay
  const jitter = Math.random() * 0.2 * exponentialDelay;
  
  return exponentialDelay + jitter;
}

// Usage in your socket error handler
socket.on('disconnect', () => {
  const delay = getReconnectionDelay(retryCount++);
  setTimeout(connect, delay);
});

By adding Math.random(), you ensure that even if 10,000 clients lose connection at once, they will spread their retry attempts over a range rather than hitting your connection listener in one single, destructive wave. The server perceives this as a manageable stream of traffic rather than a burst of millions of requests per second.

Key Takeaway: Never use fixed intervals for reconnection logic; always add randomized jitter to your backoff strategy to smooth out the recovery load and protect your infrastructure.