LESSON 12.4
Huge audiences and hot channels
Fanning one event out to millions of viewers, coalescing and sampling high-rate updates, tiered fan-out through edge servers, and protecting the system when everyone reconnects at once.
6 min read
Earlier lessons covered pushing updates to users. This one is about the extreme: one channel with millions of listeners, or an event stream far faster than anyone can read.
Cost is messages times viewers
The number that matters is deliveries per second: events per second multiplied by recipients.
- Group chat: 10 msg/s × 50 members
- 500
- Big stream chat: 1,000 msg/s × 100,000 viewers
- 100 million
- Viral stream: 5,000 msg/s × 2,000,000 viewers
- 10 billion - impossible to deliver everything
At the top end, the design question changes from "how do we deliver everything?" to "what should each viewer actually see?".
Coalesce, sample, summarize
Three techniques cut deliveries without hurting the experience:
- Coalesce state. For values where only the latest matters (viewer counts, likes, scores, a cursor position), publish the current value on a timer, say every 500 ms, instead of on every change.
- Sample streams. For chat in a huge room, deliver a representative subset, such as 20 messages a second. Slow mode and per-user rate limits reduce what arrives in the first place.
- Summarize. Replace raw events with aggregates: "12k reactions" rather than 12,000 reaction events.
Prioritize what the individual viewer cares about: their own messages, replies to them, and messages from moderators should always be delivered, even when everything else is sampled.
Tiered fan-out
One publisher can't open a connection to two million clients. Fan-out is done in tiers, like a tree:
The event is published once, delivered to a few hundred edge servers, and each edge server writes it to its own tens of thousands of connections. Each edge server subscribes to a channel only if at least one of its clients is watching it, so quiet channels cost nothing. For the largest events, an extra tier of relay servers sits between pub/sub and the edges.
Reconnect storms
When a server or region restarts, every client it held reconnects at once. If they all retry immediately, they overload the connection servers, and every reconnect also triggers work behind them: authentication, subscriptions, fetching missed messages.
- Jittered backoff on the client spreads reconnects over tens of seconds.
- Admission control on servers accepts new connections at a bounded rate.
- Cheap resumption: clients reconnect with their last-seen id and get a compact catch-up, or just the current state, instead of replaying everything.
Try it
Live Polling Platform HLD
Design live results for a poll with a huge audience.