Presence and heartbeats
Who is online right now?
Chat shows green dots next to online friends and a 'typing...' hint. This feature is called presence, and it sounds trivial until you ask what 'online' means. A user closing the app politely can send an 'I am leaving' message. A user who walks into an elevator, runs out of battery or loses signal sends nothing. The server has to decide that they are gone from silence alone.
The tool for this is the heartbeat. While connected, the client sends a tiny message every 15 seconds. The server records last_seen for the user, usually in a fast store with an expiry (a TTL). If the key expires without being refreshed, the user is considered offline. A good rule is a timeout of two or three missed heartbeats: with a 15 second heartbeat, a 45 second expiry tolerates a single dropped packet without flapping.
on heartbeat(user):
SET presence:user EX 45 # refresh the 45 s lifetime
if previously offline: publish "user online"
# nothing to code for going offline:
# the key expires, and a subscriber to expiry events
# publishes "user offline"Scale shapes the design. Suppose 1 million users are online with a 15 second heartbeat. That is 1,000,000 / 15, about 66,700 writes per second to the presence store, which is modest for an in-memory store but too much for your main database. Telling every friend about every change is the harder part: a user with 500 friends going online fans out 500 notifications. Many apps only push presence to people who currently have that conversation or contact list open, and let everyone else fetch on demand.
Your turn
Choose a heartbeat interval and expiry for a chat app on mobile networks and explain how you balance battery, load and accuracy.
Stuck? Longer intervals save battery and server load but reduce accuracy.
What to remember
Presence is inferred from heartbeats that refresh an expiring record.
Set the timeout to a few missed heartbeats to avoid flapping.
Heartbeat writes scale with users divided by interval.
Treat presence as approximate and limit who is notified.