Skip to content

Real-time & chat · 3 of 6

Step 1 · The pattern

Online is a guess

Ana and Ben each keep a connection open and send a small heartbeat every few seconds (sped up here). While heartbeats arrive, the presence service shows them as online to their four contacts. Chat goes through the same fan-out.

  • heartbeat / message
  • online / delivered

In short

Presence: how it works and how it breaks

The green dot next to a name is a guess. A connected client sends a small heartbeat every few seconds, and the server calls the user online while heartbeats keep arriving. When they stop, it marks the user offline and sends that change to everyone who can see them. That fan-out is the expensive part: one flip costs one update per contact. A phone on a weak connection drops and returns again and again, and each flip is announced, so presence can produce more traffic than the chat it decorates while carrying far less value. Two habits keep it cheap. First, wait a grace period before announcing offline: a client that reconnects inside the window was never announced as gone, so nothing is sent. Second, treat presence as the first thing to batch or switch off when the system is under load.

How this comes up in interviews

"Design the online indicator for a chat app. How does the server decide a user is online, and how do their contacts find out when that changes?"

Then they ask

  1. Phones on a weak network drop and return again and again. What does that do to your system, and how do you damp it?
  2. You lengthen the grace period to cut presence traffic further. What do you give up?
  3. The system is overloaded during an incident. What do you do with presence, and what must exist beforehand?

A strong answer covers

  • A connected client sends a heartbeat every few seconds. The server calls the user online while heartbeats keep arriving, so the dot is a guess.
  • The expensive part is fan-out: one change of state costs one update per contact.
  • A flapping connection announces every drop and return, so presence can produce more traffic than the chat it decorates.
  • Wait a grace period before announcing offline: a client that reconnects inside the window was never announced as gone.
  • Presence carries little value, so it is the first thing to batch or switch off under load.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last reviewed: Engineering write-up

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.