Design an Online Presence Service
Understanding the Problem
🟢 What is a presence service? Presence is the green dot next to a name: online, away, offline, and sometimes "last seen 5 minutes ago." A presence service tracks which users are connected right now, works out a status for each one across all their devices, and tells the people who care when that status changes. It's one of the most-read pieces of data in a chat app, and almost none of it needs to be stored forever.
You might be asked to "design online/offline status for a Slack-like app using heartbeats" or to "design a real-time presence platform" that several products share (chat, a document editor showing who's viewing, a support tool showing which agents are available). It's the same system. A platform version exposes the same three operations (report presence, read presence, subscribe to changes) as an API to other teams, so we'll design it that way from the start.
Functional Requirements
We'll agree with our interviewer that messaging itself already exists, including a persistent WebSocket connection from each client.
Core Requirements
- Users should be marked online when they open the app on any device, and offline when they close it, lose their connection or go quiet.
- Users should be able to see the presence of other users (online, away or offline, plus "last seen") in their DM list, channel member lists and profiles.
- Users should receive real-time updates when the presence of someone they're looking at changes.
Below the line (out of scope)
- Custom status text and emoji ("🌴 On vacation"). That's a profile field with an expiry; it doesn't need real-time machinery beyond what we build.
- Typing indicators. Similar shape, much shorter-lived, usually sent through the messaging path.
- Do-not-disturb schedules and notification suppression.
Non-Functional Requirements
Ask your interviewer how fast "online" and "offline" need to show up, because the two have very different costs. For our design: 50M daily active users, up to 20M online at once, with about 30M concurrent connections since many people are logged in on a laptop and a phone.
Core Requirements
- The system should show a user as online within a few seconds of connecting, and offline within about a minute of an unclean disconnect (immediately for a clean one).
- The system should handle 30M connections, their heartbeats, and ~2M presence updates delivered per second at peak.
- The system should return presence for a batch of a few hundred users in under 50ms.
- The system should be highly available. Presence can be briefly stale, but it shouldn't go dark, and statuses shouldn't flicker.
Below the line (out of scope)
- Strong consistency. If Bob sees Alice as online for two seconds after she left, that's fine.
- Durable history of who was online when. Only "last seen" is persisted.
The math. With heartbeats every 30 seconds, 30M connections send 1M heartbeats per second. The presence state itself is small: per user, a status, a few connection IDs and a timestamp, on the order of 100 bytes, so 50M users is about 5 GB. It all fits in memory.
The fan-out is where it gets big. Suppose a user's status changes 20 times a day (laptop connects, phone connects, goes idle, comes back, disconnects...). 50M × 20 = 1B changes a day, about 12k/sec on average and maybe 35k/sec in the morning login wave. If each change is delivered to 50 people who have that user on screen, that's 35k × 50 ≈ 1.75M pushes per second.
Now imagine we pushed every change to every member of the user's workspace instead. A 100k-member workspace with 20 changes per member per day is 2M changes a day, each sent to 100k people: 2×10^11 messages a day, over 2M per second, for one company. Presence fan-out grows with the square of the group size.
The core insight: presence is cheap to store and expensive to broadcast. The design isn't about where to keep a status; it's about only telling people who are actually looking, and telling them less often.