How to design presence and live cursors
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Online, offline and flaky links
- Telling the right people
- Live cursors in one document
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
Presence tells people who is online, who has the same document open, and where each person's pointer is right now. The answer turns on treating all three as soft signals that expire on their own: a heartbeat refreshes an entry that lapses when heartbeats stop, a check runs shortly after the entry would have lapsed so a brief drop goes unnoticed, and each change travels to the clients that have that user on screen rather than to everyone. Cursors are the extreme case of the same idea: throttled, bundled per tick into a single message for each viewer, held in a room's memory, and dropped without regret when a viewer disappears.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many people are online at once, and how often does someone's status change? | Heartbeat load scales with the online count and fan-out load with the rate of changes. The two figures decide whether presence is a small service or a fleet. | About 500 million members with millions online at the same time, thousands of status changes a second, and a change should reach the people watching in under 200 ms at p99. |
How quickly must offline show, and is a short delay acceptable? | If offline has to show the instant a connection closes, a phone in a tunnel flips on and off. A grace window changes the whole heartbeat design. | A drop of a few seconds must not flip someone offline and back. Showing offline tens of seconds after the last heartbeat is acceptable, along with a last-seen time. |
Who gets to see a user's status: their contacts, anyone, or the people sharing a document with them? | It decides what a client subscribes to. Watching the contacts shown on screen is a different fan-out problem from watching the viewers of one document. | Both: a contact list shows online dots for the people on it, and a document shows who has it open right now, with their cursors. |
How many people can have one document open, and how large can a group get? | Dozens of viewers can share one server process; tens of thousands means one owner sending to each of them, which is where fan-out breaks. | Dozens is typical for a document. Assume groups or rooms can reach tens of thousands, and they must not flood each client with a message per change. |
Do cursor positions need to be stored or replayed later? | A cursor that has to survive a restart needs a durable path; one that the next move supersedes a moment later should live in memory and nowhere else. | No. Cursor positions aren't stored anywhere. Someone who opens a document gets the current set from the room and then each change as it happens. |
Can one user be connected from several devices or tabs at once? | If so, online means at least one connection is alive, and a single tab closing should not mark the user offline while another tab is open. | Yes, treat it as optional for the first version: one presence entry per user, which a heartbeat from any of their connections refreshes. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Presence changes reach viewers in under 200 ms at p99 | In this design a change is pushed over the open connection rather than polled for, since polling fast enough to meet the target from millions of clients would be far heavier. The path from heartbeat to a watcher's screen is a publish to a topic and a delivery over that connection, with no database read on the way, and any batching tick has to fit inside the budget. |
A drop of a few seconds does not flip someone offline and back | Offline is decided by missing heartbeats over a window, not by a closed connection. The presence entry lives longer than the heartbeat interval, and the offline check waits for it to lapse before anything is published. |
Cursor positions are never stored: losing one costs nothing | Cursors skip the database and any durable queue. They live in the room server's memory, travel over a best-effort channel, and the join response is built from that memory. |
Millions of users online at once, with thousands of status changes a second | Heartbeat handling must be spread across many nodes, with the state held in a store they all share, so no node is special. Status changes go through pub/sub so the presence service does not know or care how many watchers there are. |
Rooms of tens of thousands of users do not flood every client | A client subscribes to what its screen shows, big groups read status on open instead of receiving each change, and a room bundles its changes per tick into a single message for each client rather than one per event. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Users online at once | The prompt says millions online out of about 500 million members. Assume 1% online at a time: 500 million × 1% = 5 million | About 5 million online at once | Heartbeat load and the presence store, below. |
Heartbeats a second and presence nodes | Assume one heartbeat per user each 30 s. 5 million online ÷ 30 s ≈ 167,000 heartbeats a second. At about 1,800 requests a second per node: 167,000 ÷ 1,800 ≈ 93 nodes for heartbeats, before GET reads of status are counted | About 167,000 heartbeats a second; roughly 93 nodes for heartbeats, more once status reads are added | The presence fleet, and why heartbeats must be spread across many nodes with state in a shared store. |
How long offline takes to show | With one heartbeat each 30 s and a margin of 5 s: the entry expires 30 + 5 = 35 s after the last heartbeat, and the offline check fires after 30 + 2 × 5 = 40 s. A drop of a few seconds is far shorter than 35 s, so the next heartbeat refreshes the entry and nothing is published | Offline shows about 40 s after the last heartbeat; a drop shorter than about 35 s is invisible | The trade-off between a stale offline and the heartbeat load above: halving the interval roughly doubles the heartbeats a second. |
Presence store size | Assume one entry of about 100 bytes per online user (user id, status, last heartbeat, expiry). 5 million × 100 bytes = 500 MB | About 500 MB for the online set | The presence store: small enough for an in-memory key-value cluster. Offline users have no entry; their last-seen time is what the offline event carried. |
Cursor messages in one busy document | Assume 50 viewers (dozens), each sending about 10 cursor updates a second: 50 × 10 = 500 inbound a second. Relaying each move to the other 49 viewers: 500 × 49 = 24,500 outbound a second. Grouping into one message per viewer per tick at 10 ticks a second: 50 × 10 = 500 outbound a second, each message carrying up to 49 cursors. Per client: 49 × 10 = 490 relayed messages a second without grouping, 10 with it | About 500 in and 24,500 out a second when relayed move by move; about 500 out a second when grouped per tick | Why the room server groups cursors per tick: outbound messages grow with the viewer count instead of its square. |
Events from one status change in a large group | Take a group of 100,000 members as an example. Pushing a status change to each member is 100,000 events per change. A cap of 500 members for pushing bounds it at 500 events; larger groups read status on open | 100,000 events per change without a cap; at most 500 with it | The push-or-fetch decision for groups, and the point at which to switch. |
Cost of one owner sending to a big room | Discord's Manifold library documents a cost of about 70 µs per send call between Erlang processes. One owner sending to a room of 30,000 sessions in sequence: 30,000 × 70 µs ≈ 2.1 s per event. If those sessions sit on 20 machines, sending once per machine is 20 sends from the owner, with the per-session work done in parallel on each machine | About 2.1 s per event from one owner; 20 sends from the owner instead of 30,000 when grouped by machine | Why a single room owner is the bottleneck in a huge room, and what splitting the send across nodes buys. |
Cursor send rates the throttle allows | The throttle in the scale assumptions runs from 16 ms to 1 s, default 100 ms. 1 s ÷ 16 ms ≈ 60 sends a second; 1 s ÷ 100 ms = 10 a second; 1 s ÷ 1 s = 1 a second | Between 1 and about 60 sends a second per client, 10 by default | The client-side throttle: the upper end multiplies the cursor traffic above by six. |
Core entities
| Entity | What it holds |
|---|---|
Presence entry | One record per online user in a key-value store with expiry. It exists while heartbeats keep arriving and disappears on its own when they stop. |
Room | The live state of one open document, held in the memory of the server that runs that document's process. |
Viewer | One connection inside a room, with its latest cursor. In memory and nowhere else: nothing here is written to a database. |
Subscription | What one connection is listening for: the user topics for the people on its screen, and the room for the document it has open. |
The API
| Endpoint | Notes |
|---|---|
| The client opens one persistent connection to a gateway; the gateway, not the client, emits a heartbeat for the user on a fixed interval while the connection is up, so a client that has gone quiet is noticed by the server side. Presence events for subscribed users arrive on this same connection. |
| A read of the presence store for the ids a screen shows. Called once when a view opens, after subscribing to those users' topics, so no change lands in the gap. For an offline user, last_seen is the time of their last heartbeat. |
| The gateway routes the join to the server running that document's room. The reply is the roster and the latest cursors from the room's memory, so a new viewer sees everyone at once. Join and leave events follow for later changes; a viewer that goes silent is removed by timeout and produces a leave like any other. |
| Throttled on the client to about 10 a second. The room keeps the latest position per viewer and sends each viewer one grouped message per tick, so a dropped update is simply superseded by the next one. No acknowledgment, no retry, no queue. |
The high-level design
Step 1 of 4
A gateway, heartbeats and a last-seen store
Start with the path that decides online. A client holds one persistent connection to a gateway. While the connection is up, the gateway emits a heartbeat for that user on a fixed interval, call it d seconds, and sends it to a presence service, which writes the user's entry to a key-value store with an expiry a little longer than the interval. GET /presence reads that store. A user is online while their entry exists. What's missing: when a user goes offline, nothing tells the people watching, and nothing yet distinguishes a phone in a tunnel from a closed laptop.
What breaks first
The deep dives
Online, offline and flaky links. A user's phone loses signal for a few seconds in a tunnel and then reconnects. What do their contacts see, and how does the presence service decide that someone has actually gone offline?
Telling the right people. Someone with thousands of contacts comes online, and separately a document has thousands of viewers. Who is told about each change, by what path, and how does a screen that has just opened avoid missing a change made while it was loading?
Live cursors in one document. Dozens of people have the same document open and all move their mouse at once. How does each pointer reach the other viewers, how many messages does that cost, and what do the others see when a tab closes without saying goodbye?
Concepts this guide uses
Sources
- LinkedIn Engineering: Now you see me, now you don't: LinkedIn's real-time presence platform
- systemdesign.one: Real-time presence platform system design
- Evan Wallace: How Figma's multiplayer technology works
- Liveblocks: @liveblocks/client API reference (documentation source on GitHub)
- Liveblocks: Presence
- Discord: Manifold, fast batch message passing between nodes for Erlang and Elixir (README)
- Alex Xu, System Design Interview, chat system chapter: reading notes