Skip to content

Lesson 6 of 9 · 21m

Presence: The Simple Feature That Is Never Simple

Online status is a distributed-systems problem — leases, multiple devices, privacy, and why last-seen beats boolean online state.

Presence looks like a boolean and behaves like a distributed consensus problem. A user can have three devices, one dead TCP connection, one backgrounded mobile app, and a browser tab that has not finished reconnecting. Which one is "online"?

Presence is a lease, not a fact

The workable model is: clients renew presence while connected; absence means no recent renewal. Store expiration, not truth.

ZADD presence:room:42 1732924830000 u_712:web
EXPIRE presence:room:42 90
ZADD presence:lastseen u_712 1732924830000

A sorted set lets you expire entries by score: remove anything older than now - 60s, or have each member key carry a TTL if your data model uses individual keys. Sweepers are required because crashes do not send goodbye messages.

The client heartbeat should be cheaper than application traffic. If users are actively sending events, those events can refresh the lease too.

Identity is user plus device plus connection

u_712 is not enough. One user may be online on desktop and offline on mobile. Use a connection or device-qualified member ID:

u_712:web:conn_9f2
u_712:ios:conn_21c

Then derive product semantics. Maybe the user is online if any device is active. Maybe the room roster lists devices separately. Maybe mobile push means "reachable" even when no socket exists. These are product decisions; the data model should not make them impossible.

Join/leave events are lossy

Never build authoritative presence by incrementing on join and decrementing on leave. Disconnects are missed, messages are duplicated, and servers restart. Treat join/leave as hints for latency and UI smoothness. Rebuild truth from leases and periodic reconciliation.

A robust flow is:

connect      -> validate auth -> create lease -> broadcast joined (best effort)
heartbeat    -> renew lease
disconnect   -> delete lease if graceful
sweeper      -> remove expired leases -> broadcast left after grace period

The grace period matters. Otherwise Wi-Fi blips make users flap online/offline every few seconds, and presence becomes noise.

Presence is a privacy surface

Exact last-seen timestamps reveal sleep schedules, work habits, and location patterns. Many products deliberately reduce precision: "active now," "active today," or nothing at all. Decide who can observe presence, whether invisible mode exists, and whether presence inside private rooms leaks membership.

Do not fan out full presence rosters on every change. Send deltas to subscribers, and snapshot on join. Large rosters need pagination or sampling; nobody needs live updates for 50,000 avatars.

Read-your-write still matters

After connecting, a user expects to appear in the roster immediately. If presence updates take a sweeper cycle to become visible, the UI feels broken. Return the local join synchronously and merge it with the shared snapshot while leases propagate.

Field notes

  • Store last_seen separately from online membership. "Online" is derived and volatile; last-seen is useful product data.
  • Clock skew breaks naive expiration comparisons. Use server timestamps consistently and avoid trusting client clocks.
  • Presence traffic scales with connections even when no one does anything. Coalesce roster updates aggressively.
  • Test browser tab sleep. Timers are throttled, sockets close unpredictably, and mobile browsers are worse.

Was this lesson helpful?

Sign in to rate