Presence and Last Seen at Scale: Designing Online Status for a Chat App

How online status, last seen and typing indicators work at scale: heartbeats, TTL keys in Redis, subscribing only to relevant contacts, and privacy controls.

Published November 27, 2026

The small "online" label and "last seen today at 10:42" look trivial, but presence is one of the noisiest features in a messaging system. Every connect, disconnect and app switch is an event, and naively broadcasting each one to all of a user's contacts would generate more traffic than the messages themselves. Good presence design is about being slightly lazy on purpose.

The concept

Presence is derived from connections. When a device connects, the gateway marks the user online; heartbeats keep the mark fresh; when the connection closes or heartbeats stop, the user becomes offline and their last-seen timestamp is recorded. Storing presence as a key with a TTL in Redis makes this self-healing: if a gateway crashes without cleaning up, keys expire on their own.

Distribution is where scale bites. Instead of pushing presence changes to every contact, clients subscribe to presence only for the conversations visible on screen, typically the open chat and perhaps the top of the chat list. Presence is eventually consistent and lossy by design: a status that is a few seconds stale is fine, so updates can be debounced, batched and sent with at-most-once delivery.

Presence pipeline

gatewaypublish changerenderConnect / heartbeatevery 30 sPresence serviceSET online EX 60Subscribersonly open chatsUIonline / last seen
Presence flows only to the few clients currently looking.

Why on-demand subscription matters

Broadcast to all contacts≈300 events per status changeSubscribers with chat open≈2 events
Fan-out per status change for a user with 300 contacts (illustrative).

Ephemeral signals and how to treat them

SignalDeliveryStorageStaleness tolerated
Online / offlineAt-most-once push to subscribersRedis key with TTLA few seconds
Last seenFetched when chat opensRedis / DB timestampMinutes
Typing indicatorEphemeral, at-most-onceNot storedExpires after ~5 s
Recording voiceEphemeral, at-most-onceNot storedExpires after ~5 s

Subscription flow

Bob appPresence serviceAlice app (chat open)subscribe presence:42connect → onlineBob onlineheartbeats stopTTL expiresBob last seen 10:42
Subscription-based presence with TTL expiry.

Practical examples

Presence with Redis TTL keys

Online if the key exists; last seen written on expiry or disconnect.

# on connect and every heartbeat (gateway)
SET presence:42 online EX 60
# on clean disconnect
DEL presence:42
SET lastseen:42 1764238920
PUBLISH presence-changes 42:offline
# query
EXISTS presence:42

Debounce flapping status

Phones switching apps rapidly should not flip status every second.

const pending = new Map()
function onStatusChange(userId, status) {
  clearTimeout(pending.get(userId))
  pending.set(userId, setTimeout(() => {
    pending.delete(userId)
    publishPresence(userId, status)       // only if still in this state after 3 s
  }, status === 'offline' ? 3000 : 0))
}

Typing indicator message

Sent at most every few seconds while typing; receivers hide it after 5 s without a refresh.

{ "type": "typing", "conversation_id": "u42:u77", "state": "composing" }

Common mistakes

  • Broadcasting every status change to every contact.
  • Using reliable, persisted delivery for typing indicators and presence pings.
  • Relying on clean disconnects; crashed apps and dead networks never send one, so TTLs are required.
  • Ignoring privacy settings such as "last seen: nobody", which must be enforced server-side.
  • Updating last-seen in the primary database on every heartbeat.

Key takeaways

  • Derive presence from connections and heartbeats; store it with TTLs.
  • Subscribe to presence only for chats currently on screen.
  • Treat presence and typing as lossy, at-most-once signals.
  • Debounce flapping and enforce privacy rules on the server.

FAQ

How accurate is "online"?

Typically accurate within seconds to a minute depending on heartbeat and TTL settings, which is enough for users.

How is last seen hidden from some people?

The server checks the viewer against the user's privacy setting before returning the timestamp; the client should never receive data it should not show.

Do large groups show presence?

Usually not per member, because it would generate too many events. Apps often show only typing indicators in groups.

More Architecture articles