Presence: The Green Dot Is the Worst Data in the Product
Why Presence Is Uniquely Awful
| Churn | Extremely high — every lid open, lock, idle timeout, and network blip flips a bit |
|---|---|
| Value per change | Almost zero — nobody is waiting on a dot |
| Interest set | Potentially everyone, which is what makes the naive design quadratic |
Messages have the opposite profile: relatively rare, high value, and a bounded audience (a channel's members). You can afford to work hard per message. You cannot afford to work hard per presence flip, because there are orders of magnitude more of them.
The Naive Design and Its Arithmetic
Broadcast every presence change to every connected client in the workspace. Take a 10,000-user workspace where each person's state flips ~20 times a day:
10,000 users × 20 flips = 200,000 changes/day
200,000 changes × 10,000 recipients = 2,000,000,000 events/day — for one workspace, for a dot
And it is O(N²): doubling the workspace quadruples the traffic. Worse, the recipient throws away essentially all of it — a client showing 30 faces has no use for the other 9,970 updates. You are spending quadratic bandwidth to deliver data that is discarded on arrival.
Slack's Answer: Query + Subscribe, Scoped to the Screen
Slack runs dedicated Presence Servers — in-memory, with users hashed onto individual PS hosts. Clients reach them through their Gateway Server, which proxies presence queries over the existing WebSocket (no second connection).
Slack publishes that shape — users hashed to PS hosts, the GS as proxy, notifications “only for a subset of users that are visible in the app screen at any moment” — but not the wiring, so the following is reasoned from it. A state change originates at the Gateway Server: it is the thing that sees your socket open, sees it drop, and receives your client's “I've gone idle.” It reports that to the one Presence Server that owns your user ID — hash(U123), the same arithmetic as the Gateway and Channel Servers part. The presence_sub list a client sends is per-socket, so the natural place to hold it is the GS — which is exactly why it dies with the socket for free — and the GS then registers with each PS that owns one of the named users, once per user however many of its clients want that user. It is the CS/GS two-level tree again, with the Presence Server in the Channel Server's seat: PS maps a user to the Gateway Servers watching them, GS maps that user to the local sockets. Three mechanisms do the real work:
-
presence_query— pull, for what you're rendering now. When the client paints a member list or a DM sidebar, it asks for the current presence of exactly those user IDs. One round trip, bounded by what's visible, not by workspace size. -
presence_sub— push, but only for a set you named. The client sends the list of users it wants live updates for. Slack then sendspresence_changeevents only for those users. Crucially, the subscription lives exactly as long as the WebSocket. No leases to renew, no TTLs to tune, no orphaned subscriptions to garbage-collect — socket dies, subscription dies, atomically and for free. -
batch_presence_aware— coalesce what's left. A client that declares this flag receives batched events carrying ausersarray instead of one event per user. Fifty people coming online in the morning is one frame, not fifty.
The Principle Worth Extracting
Scroll a member list and your subscription set changes. The viewport is the subscription. Cost becomes proportional to what a human can actually perceive — a few dozen faces — which is constant no matter how large the company gets.
This is the same move as the Flannel part: Flannel made connect cost proportional to the working set; presence_sub makes presence cost proportional to the visible set. Both replace "everything about the workspace" with "the bit currently in front of a human."
Detecting "Away" Without Being Told
The second half of presence is the state nobody reports. Users close laptops, lose wifi, and walk away; almost none of them log out. So online-ness cannot be a flag someone sets — it has to decay:
- Connected is directly observable — the Gateway Server either holds your socket or it doesn't. A dropped TCP connection is the offline signal, arriving for free.
- Active vs. away is the client's judgement: the client watches for input and declares itself away after an idle period, or when the OS reports the screen locked. The server doesn't guess.
- The fallback is a lease. If a socket dies without a clean close — the common case on mobile networks — the server can't distinguish "gone" from "quiet." State must expire on a heartbeat/TTL basis rather than persist forever, which is why a presence dot occasionally lags reality by tens of seconds.
- Guarantees: Presence traffic to a client is bounded by what that client asked for, and a subscription can never outlive its connection — the socket's lifetime is the subscription's lifetime, so there is nothing to leak.
- Does not guarantee: That the dot is correct right now. A user who loses connectivity without a clean disconnect shows as online until their state decays, and with batching, updates arrive in groups rather than instantly.