DEV Community

desgh white
desgh white

Posted on

Rendering a Live Leaderboard at 10K Concurrent Viewers

A live leaderboard where positions change every few seconds sounds like a simple sorted list. At 10K concurrent viewers, the naive approach — push every change to every client — saturates your WebSocket server in minutes.

Server-side batching

Instead of broadcasting each individual score change, batch updates into 2-second windows and send one delta per window:

import asyncio, time

buffer: dict[str, int] = {}

async def flush_loop(broadcast_fn):
    while True:
        await asyncio.sleep(2)
        if buffer:
            snapshot = dict(buffer)
            buffer.clear()
            await broadcast_fn(snapshot)
Enter fullscreen mode Exit fullscreen mode

This cuts message count by 50–80x during peak activity.

Client-side animation

The client receives a dict of {player_id: new_score} and must reorder the list smoothly. Use FLIP (First, Last, Invert, Play):

function animateReorder(container) {
  const items = [...container.children];
  const first = items.map(el => el.getBoundingClientRect());
  items.sort((a, b) => +b.dataset.score - +a.dataset.score);
  items.forEach(el => container.appendChild(el));
  items.forEach((el, i) => {
    const dy = first[i].top - el.getBoundingClientRect().top;
    el.style.transform = `translateY(${dy}px)`;
    requestAnimationFrame(() => {
      el.style.transition = 'transform 0.4s ease';
      el.style.transform = '';
    });
  });
}
Enter fullscreen mode Exit fullscreen mode

A platform like www.esportes-da-sorte.net that runs live competitions with real-time rankings needs this FLIP approach — without it, the list jumps and the user loses track of their position.

Virtual scrolling

If the leaderboard has 10K entries, render only the visible viewport plus a buffer. The client holds the full sorted array in memory but only mounts DOM nodes for ~50 rows.

Takeaway

The bottleneck is not sorting. It is bandwidth. Batch on the server, animate on the client, virtualise the DOM.

Top comments (0)