LESSON 06 · Online status

Treat presence as a hint

Use leases and heartbeats so brief network gaps do not make status flicker.

SYSTEM VIEW

The message path

Diagram view
Ben shows—
Ben’s leases—
All renewals—

Watch Ben’s phone and laptop keep short leases alive, then see who should hear when his status changes.

  1. Ben’s phone and laptop each send the Presence service a heartbeat, which renews that device’s lease for 75 s: a record that expires unless refreshed. Ben shows online while at least one of his leases is current.

    Lease
    expires unless renewed
    Online if
    any lease is current

    What changed

    • Heartbeats from each device
    • Both leases renewed
    • Ben shows online
  2. Beats go every 30 s and a lease lasts 75 s. A beat from Ben’s phone is lost with 45 s left on its lease; the next arrives with 15 s to spare and renews it to 75 s, so Ben’s dot never flickers.

    Heartbeat
    every 30 s
    Lease
    75 s

    What changed

    • One beat lost
    • Renewed with 15 s left
    • No flicker
  3. Ben’s phone loses power right after a beat. No logout clears its lease, so the phone counts as online until the lease expires, up to 75 s later. Ben still shows online: his laptop’s lease is current.

    Logout
    cleared at once
    Power loss
    online up to 75 s

    What changed

    • Ben’s phone: power lost
    • Phone lease expired
    • Online by the laptop
  4. With Ben’s phone back on, 4.5 million connected devices beating every 30 s make about 150,000 renewals a second. So leases usually live in an in-memory store such as Redis, not the message database. A restart costs little: the next beats rebuild them.

    Connected
    4.5M devices
    Store
    in memory, e.g. Redis

    What changed

    • Ben’s phone back on
    • 150,000 renewals/s
    • Leases kept in memory
  5. Ana opens her chat with Ben. The Presence service answers with his status, then sends her meaningful changes only while the chat stays open. Small conversations and contact lists get presence this way.

    What changed

    • Ana views Ben
    • Subscribed while open
  6. Sending every status flip to all 100,000 members of a channel can cost more than its messages. Instead, a member who opens the list fetches a snapshot and subscribes only while looking. Past its large_threshold, Discord leaves offline members out of the first list.

    Channel
    100,000 members
    Discord
    offline left out

    What changed

    • Ana
    • 100,000-member channel
    • Snapshot on open
  7. Presence never decides whether a message is stored: Ben can vanish between a status check and a send, so persist first, try live, catch up from storage. If nobody acts on the dot, “active today” removes much of the live dot’s load: try both.

    What changed

    • Channel members
    • Presence: a hint, never a gate
    • Live dot or active today
    Status shown

Space plays · ← → step · Home resets

THE IDEA

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.

DECISION POINT

A person’s phone loses signal for eight seconds in a tunnel, but their laptop stays connected. Should the contact list show them offline?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Presence should tolerate transient disconnects and multiple devices.
  • Presence is advisory; durable delivery does not depend on a green dot.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What does a presence lease represent?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.