advanced 2 min answer

A chat platform uses publish/subscribe to deliver messages to connected clients. What breaks when a channel has a million members, and what changes?

pubsubfan-outscalepresencediscordfailure-analysis
Show the full answer Hide the answer

What breaks

Fan-out amplification. One message published to a channel with a million members becomes a million deliveries. At ordinary channel sizes this is fine; at this size, a single message typed by one person saturates the delivery path for everyone.

Presence is worse. Membership changes with every connect, disconnect and status change, and naive presence is O(members) per change. One user going idle producing a million notifications is untenable, and presence changes far more often than messages.

Subscription state. Maintaining a million subscriptions per channel, across many channels, is itself a scaling problem in the pub/sub layer.

What changes

1. Fan-out strategy chosen by channel size. Small channels push directly to every connected member; large channels invert to a pull or subscribe-on-read model, where the message is written once and connected clients read from it. The user-visible behaviour is identical; the mechanism is completely different, selected by a threshold on membership.

2. Only deliver to connected members. A large fraction of members are not online. Writing to them is waste, and lazy materialisation removes it.

3. Presence sampled and approximate above a threshold. Precise presence for small groups; an approximate count for large ones, which nobody can perceive the difference in. This is a product decision that makes the engineering tractable.

4. Aggregation nodes between publisher and connections, so a message fans out through a tree rather than from a single point.

5. Per-channel rate limiting, since a hot channel is also a hot key and must not consume the delivery capacity of everything else.

The generalisable lesson

Pub/sub is a delivery mechanism, not a scaling strategy. At high fan-out, the question is not which broker to use but whether the message should be copied to recipients at all — and the mature answer is a hybrid selected by fan-out size, with the threshold monitored so channels that grow past it are reclassified.

The same shape appears in social timelines, notification systems and live-event chat: push for the many small cases, pull for the few enormous ones.