FSD Frontend System Design

Part V — The Craft · Lesson 10.14

YouTube Live Stream Chat UI

In one lineHundreds of messages a second into a fixed-size window — buffer, cap, virtualize, and pin to bottom carefully.

Where you've seen itLive chat during a big match, where the feed is a blur and the browser still stays smooth.

  • Time18 min
  • DiagramBuffer and flush pipeline + the bounded window
  • Chapter10 · Low Level Design

Diagram

diagram

Lessons in this diagram2.4 Web Sockets

diagram
diagram
PIN TO BOTTOM — only when the user is already there

  atBottom = scrollHeight - scrollTop - clientHeight < 40px

  atBottom  → auto-scroll on each flush
  scrolled  → STOP auto-scrolling, show "↓ Jump to latest (128)"
    up
              user is reading history; moving them is hostile

  Also: pause auto-scroll on hover, so a mouse-over freezes the feed.

How it works

  1. Never setState per message. Push into a plain array and flush on a timer or animation frame.
  2. Cap the rendered window to a few hundred messages, dropping the oldest on append.
  3. Virtualize if rows vary in height or the window is large (10.7).
  4. Track atBottom and only auto-scroll when true; otherwise show a jump-to-latest button with an unread count.
  5. Handle the extras that make it real: dedupe by message id, moderation deletions, rate-limit spam per user, and a "chat paused" state under extreme load — which is exactly what YouTube does.
js
const buffer = useRef([]);
useEffect(() => {
  socket.onmessage = (e) => buffer.current.push(JSON.parse(e.data));
  const id = setInterval(() => {
    if (!buffer.current.length) return;
    setMessages((prev) => [...prev, ...buffer.current].slice(-MAX));  // cap
    buffer.current = [];
  }, 100);
  return () => clearInterval(id);
}, []);

Trade-offs

DecisionConsequence
Flush every 100ms10 renders/sec, imperceptible delay
Flush per messageSmooth data, unusable UI
Cap at 300 messagesBounded memory, history lost
VirtualizeHandles any size, more complexity
Auto-scroll alwaysDestroys the reading experience

Accessibility (8.3): a live region announcing every message is unusable. Announce nothing by default, offer a "pause and read" mode, and keep the scroll container keyboard-focusable so it can be scrolled without a mouse.

Interview angle

"Design live chat for a stream with 500k viewers."

  • Client side: buffer and flush on a frame, cap the window, virtualize, and pin to bottom only when the user is already there.
  • Transport: SSE or WebSocket per viewer, fanned out from a pub/sub layer — and at extreme volume the server samples messages rather than sending all of them.
  • Say the quiet part: nobody can read 300 messages a second, so sampling and "chat paused" are features, not degradations.

Recap

  • Buffer in a ref, flush on a tick — one render per frame, not per message.
  • Bound the window; memory must not scale with stream duration.
  • Auto-scroll is conditional on the user already being at the bottom.