How it works
A connected device periodically renews a short-lived lease, a record that expires unless refreshed, by sending a heartbeat to a presence service. A logout can clear that device immediately; an abrupt disconnect expires after a grace period. An account is online when at least one eligible device has a current lease. Presence is approximate because heartbeats, routing, and observers are never perfectly simultaneous.
Suppose devices send a heartbeat every 30 seconds and leases last 75 seconds, so a missed beat or two causes no flicker. A phone that loses power still shows online for up to 75 seconds. With 4.5 million connected devices, that is about 150,000 renewals per second, so leases usually live in an in-memory store such as Redis, not the message database. Losing them in a restart costs little, since the next heartbeats rebuild them.
Publish meaningful status changes to people currently viewing a small conversation or contact list. Sending every status flip to every member of a huge group can be much more expensive than message traffic. Large audiences can fetch a snapshot when they open the view, then subscribe only while interested. Discord’s gateway reflects this: past its large_threshold setting, it stops sending offline members in a server’s initial member list.
Do not let presence decide whether to store a message. A recipient can disappear between a status check and a send. Persist first, attempt live delivery, and use the stored record for later catch-up. If people do not act on the dot, a coarser “active today” removes much of this load.
A person’s phone loses signal for eight seconds in a tunnel, but their laptop stays connected. Should the contact list show them offline?
No. The account still has a live laptop lease. Even without it, a short grace period can avoid a misleading offline/online flicker during a transient phone gap.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Presence should tolerate transient disconnects and multiple devices.
- Presence is advisory; durable delivery does not depend on a green dot.