Video in progress

During a football final, one score update has to reach 3 million viewers within 1 to 2 seconds. How do you build such a global broadcast?

I take the broadcast onto a separate path from per-user delivery: the event is serialized once, tree-shaped fan-out sends one copy to each gateway, and the gateway distributes it locally to its own subscribers. On top of that: conflation, pre-scaling, and routing the follow-up "load the full state" requests to a CDN cache.

📖 Answer

The per-user model (routing table, inbox, a separate message for each user) doesn't work here: that's 3 million writes and 3 million routing decisions for one goal. Broadcast is different by nature: everybody gets the same bytes.

Tree fan-out.

score-service ──> broadcast topic (NATS/Redis/Kafka, 1 message)
                     │
          ┌──────────┼──────────┐
       gateway-1  gateway-2 ... gateway-30   (each receives 1 copy)
          │
     100 thousand local subscribers (from an in-memory list)

30 messages leave the center, not 3 million. Inside the gateway, fan-out is just writing one ready buffer to many sockets.

Serialize once. I turn the JSON into bytes once on the gateway, not per subscriber, and reuse that buffer (retainedDuplicate() in Netty, one ready-made TextMessage in Spring). permessage-deflate hurts here: compressing separately for each connection turns one broadcast into 100,000 times the CPU work. For this channel I turn it off and keep the payload small myself.

Split the writes. Writing to 100,000 sockets sequentially keeps an event loop busy for a few hundred milliseconds. I split subscribers across several event loops or batches and write in parallel, and for slow clients a "drop the old message" policy: if the score updates, the previous score isn't needed.

Conflation. If there are 5 events in one second (a goal, a card, stats), the client gets one aggregated state per 200 to 500 ms window. For a sports score the latest state matters, not the intermediate ones.

The second wave. The most dangerous part isn't the broadcast but what comes after it: 3 million apps at the same instant say "load the details page". So the push carries the needed data itself, and extra requests go to static JSON in the CDN cache, and the client sends them with 0 to 3 seconds of jitter.

Pre-scaling. The time of the final is known. I don't wait for the autoscaler: the gateways and the broker are brought up to the needed size an hour ahead, and connections are spread evenly among them in advance.

An alternative: for such mass, public data, serve SSE through a CDN: many CDNs fan a single origin stream out to many clients. For a public broadcast that needs no auth, this is often the cheapest way.

The measure of success is the latency histogram from client acks: p50 and p99 of "event emitted → visible on screen". I try that in a game day before the final with 3 million synthetic connections.