WebSocket Reconnection Backoff Simulator

WebSocket Reconnection Backoff Simulator

animation
http
app-protocols
Try Open the WebSocket reconnection backoff Simulator panel, change one input, then press Play reconnection simulation.
Observe After pressing Play reconnection simulation, watch how the WebSocket reconnection backoff Simulator results update.
Explain Explain why the WebSocket reconnection backoff Simulator results changed after you press Play reconnection simulation.

Technical boundaries. Fixed delay places every client at 5 seconds. Exponential delay is capped before adding random jitter up to 1000 milliseconds, and peaks use 1-second bins.

Visualize how exponential backoff with jitter spreads reconnection attempts:

viewof numClients = Inputs.range([10, 500], {
  value: 100,
  step: 10,
  label: "Number of clients"
})

viewof baseDelay = Inputs.range([500, 5000], {
  value: 1000,
  step: 100,
  label: "Base delay (milliseconds)"
})

viewof maxDelay = Inputs.range([10000, 120000], {
  value: 60000,
  step: 5000,
  label: "Max delay (milliseconds)"
})

viewof attemptNumber = Inputs.range([0, 6], {
  value: 1,
  step: 1,
  label: "Reconnection attempt"
})
{
  // Fixed delay approach
  const fixedDelayTime = 5000;
  const fixedClients = Array(numClients).fill(fixedDelayTime);

  // Exponential backoff with jitter
  const exponentialClients = Array.from({length: numClients}, () => {
    const expDelay = Math.min(baseDelay * Math.pow(2, attemptNumber), maxDelay);
    const jitter = Math.random() * 1000;
    return expDelay + jitter;
  });

  // Create histogram bins
  const binWidth = 1000; // 1 second bins
  const maxTime = Math.max(...exponentialClients, fixedDelayTime) + binWidth;
  const numBins = Math.ceil(maxTime / binWidth);

  const fixedBins = Array(numBins).fill(0);
  const expBins = Array(numBins).fill(0);

  fixedClients.forEach(t => fixedBins[Math.floor(t / binWidth)]++);
  exponentialClients.forEach(t => expBins[Math.floor(t / binWidth)]++);

  const fixedPeak = Math.max(...fixedBins);
  const expPeak = Math.max(...expBins);

  const timeLabels = Array.from({length: numBins}, (_, i) => `${i}s`);

  return html`
    <div style="font-family: Arial, sans-serif; padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #2C3E50;">
      <p style="margin-top: 0; margin-bottom: 0.8em; color: #2C3E50; font-weight: 700; font-size: 1.1em;">Reconnection Storm Comparison</p>
      <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0;">
        <div style="background: white; padding: 15px; border-radius: 6px; border-top: 3px solid #E67E22;">
          <div style="font-size: 0.9em; color: #7F8C8D; margin-bottom: 10px;">Fixed Delay (5s)</div>
          <div style="font-size: 1.8em; font-weight: bold; color: #E67E22;">${fixedPeak}</div>
          <div style="font-size: 0.9em; color: #7F8C8D; margin-bottom: 10px;">clients/second (peak)</div>
          <svg width="200" height="80" style="border: 1px solid #e0e0e0; border-radius: 4px;">
            ${fixedBins.map((count, i) => `
              <rect x="${i * (200/numBins)}" y="${80 - (count/fixedPeak * 70)}"
                    width="${200/numBins - 1}" height="${count/fixedPeak * 70}"
                    fill="#E67E22" opacity="0.8"/>
            `).join('')}
          </svg>
          <div style="font-size: 0.8em; color: #7F8C8D; margin-top: 5px;">All ${numClients} clients hit at ${(fixedDelayTime/1000).toFixed(1)}s</div>
        </div>
        <div style="background: white; padding: 15px; border-radius: 6px; border-top: 3px solid #16A085;">
          <div style="font-size: 0.9em; color: #7F8C8D; margin-bottom: 10px;">Exponential Backoff + Jitter</div>
          <div style="font-size: 1.8em; font-weight: bold; color: #16A085;">${expPeak}</div>
          <div style="font-size: 0.9em; color: #7F8C8D; margin-bottom: 10px;">clients/second (peak)</div>
          <svg width="200" height="80" style="border: 1px solid #e0e0e0; border-radius: 4px;">
            ${expBins.map((count, i) => `
              <rect x="${i * (200/numBins)}" y="${80 - (count/expPeak * 70)}"
                    width="${200/numBins - 1}" height="${count/expPeak * 70}"
                    fill="#16A085" opacity="0.8"/>
            `).join('')}
          </svg>
          <div style="font-size: 0.8em; color: #7F8C8D; margin-top: 5px;">Spread over ${(Math.max(...exponentialClients)/1000).toFixed(1)}s window</div>
        </div>
      </div>
      <div style="background: white; padding: 15px; border-radius: 6px;">
        <strong style="color: #16A085;">Peak load reduction: ${((fixedPeak - expPeak) / fixedPeak * 100).toFixed(1)}%</strong><br/>
        <span style="color: #7F8C8D; font-size: 0.9em;">
          Fixed delay creates thundering herd. Exponential backoff distributes load evenly.<br/>
          Attempt ${attemptNumber}: Delay range ${(baseDelay * Math.pow(2, attemptNumber) / 1000).toFixed(1)}s - ${(Math.min(baseDelay * Math.pow(2, attemptNumber), maxDelay) / 1000).toFixed(1)}s
        </span>
      </div>
    </div>
  `;
}

Back to the chapter