Heartbeats and presence
A heartbeat is a small message sent on a schedule so each side can tell a live connection from a dead one; presence (online, last seen) is derived from it, and its interval sets both cost and detection time.
What it is
A heartbeat is a small message one side of a long-lived connection sends on a schedule, so the other side can tell a peer that is quiet from a peer that is gone. The need comes from how TCP behaves when nothing is being sent: an established connection can sit idle, and still count as live, for as long as you like, so nothing arrives to tell a server that the phone on the other end has lost its signal. The server's socket stays open and looks healthy. Even a NAT box on the path has no better information; RFC 5382 notes that a NAT can find out whether an endpoint has crashed by sending a TCP keep-alive and seeing whether a reset comes back, and otherwise it keeps the mapping until the connection has been idle for a while.
Heartbeats exist at three layers, and the one you pick decides how fast you notice a dead peer. TCP keepalive is done by the kernel once a socket turns on SO_KEEPALIVE: on Linux, the tcp(7) manual page gives tcp_keepalive_time a default of 7200 seconds of idleness before the first probe, then up to 9 probes (tcp_keepalive_probes) 75 seconds apart (tcp_keepalive_intvl) before the connection is dropped, so a dead peer is declared after more than two hours. WebSocket Ping and Pong frames are control frames that RFC 6455 defines for this check; a browser answers a server's Ping with a Pong on its own, but page JavaScript can't send a Ping or see one arrive, so browser apps end up with the third layer. Application-level pings are ordinary messages your own protocol defines; Socket.IO v4, for example, has the server send a ping packet each pingInterval (default 25000 ms) and treat the connection as closed when no pong arrives within pingTimeout (default 20000 ms since v4.0.0). The client runs the mirror check: no ping for pingInterval + pingTimeout and it also gives up.
| Knob | What it decides | What moving it costs |
|---|---|---|
Interval | How often each connection sends a message, and so the fleet-wide message rate | Shorter: more traffic and more wake-ups on phones. Longer: a dead peer stays unnoticed for longer |
Timeout | How long silence is tolerated before the connection is declared dead | Shorter: a slow network or a briefly stalled server looks dead and clients reconnect in a wave. Longer: slower detection |
Grace period before 'offline' | How long after the connection dies you wait before telling others | Shorter: presence flaps when a phone switches networks. Longer: a user who really left stays 'online' a little longer |
Direction | Whether the client pings (MQTT: the client agrees a keep-alive value in seconds at connect, sends PINGREQ when it has been idle for that long, and the broker disconnects it after 1.5 times the value) or the server pings | Client-driven lets a device choose its own pace, for instance by signal strength; server-driven gives the server control of detection time |
Presence is what you derive from those heartbeats: online means the server holding this user's connection has heard from it within the window; offline means the window passed and the grace period ran out; last seen is the time of the last heartbeat or the moment the connection was declared dead. The connection server knows this first. Other servers need it too, to show a green dot or to decide whether to push a message to a socket or store it, so the state is published to a shared store. We'd weigh two designs for keeping that store honest. One is a key per user with a lifetime a little longer than the heartbeat window, refreshed on each heartbeat, so a dead connection expires on its own; the price is a store write per heartbeat. The other writes transitions (came online, went offline) and pairs them with a lease per connection server, so when a whole server dies its users expire together; far fewer writes, more code to get right. Distributed locks and leases covers the lease half.
Catching up is the other half of the same failure. While the socket looked open, the server may have written messages to it that the phone had already stopped receiving, so a successful write to the socket is not proof of delivery; an acknowledgment from the client is, which is the ground covered in delivery semantics. The pattern that fixes it is a server-side inbox plus a client-side cursor: messages are stored per recipient, the client remembers the id of the last one it processed, and on reconnect it sends that id and receives everything after it. A bounded, library-level version of this exists as connection state recovery (added in v4.6.0), which keeps sessions and packets for maxDisconnectionDuration (two minutes in the documented example) and is described as a tool for intermittent disconnections rather than long absences.
When to use it
Consider an application-level heartbeat for connections that stay open and carry state you care about: chat and messaging, collaborative editing, multiplayer games, dashboards fed over WebSockets, and devices on MQTT where the keep-alive interval is part of the protocol. The decision to make is the detection target. Say how fast the product needs to notice a dead peer, then pick interval and timeout to hit it, then state the cost: the 25 s and 20 s defaults above notice a dead connection in about 45 s, and at that interval a million connections produce roughly 40,000 pings per second across the fleet, plus the same number of pongs. A one-second interval, by contrast, is documented to produce load you'll notice with a few thousand clients.
Presence deserves its own fan-out design. A user's status change is interesting to everyone who can see that user, which for a widely followed account can mean many screens at once, so a strong answer scopes subscriptions to what is on screen (the open chats, the visible part of a contact list) and unsubscribes when the view changes, batches status changes that happen within the same second, and rounds last-seen to a coarse value so a stream of tiny updates doesn't fan out at all. Fan-out on write and on read has the general shape of that trade-off.
When not to
Don't add a heartbeat where traffic already is one. Any inbound message proves the peer is alive, so reset the timer on each one and send a ping when the connection has been quiet for the whole interval; MQTT works this way, with the client sending PINGREQ when it has had nothing else to send during the keep-alive period. And don't use heartbeats for a request-and-response API: a request timeout on each call does the job with no extra traffic.
Skip the shared presence store when the scale doesn't ask for it. A single server can keep presence in a map in memory, and our advice is to start from the library's built-in heartbeat and its defaults unless the product has a tight detection target. Likewise, if the product has no visible online status, no last seen and no routing decision that depends on who is connected, turning on SO_KEEPALIVE and accepting the kernel's defaults is an acceptable backstop for cleaning up dead sockets eventually and slowly.
Presence isn't free for users either. A precise last-seen time reveals sleep patterns and work hours, so a product that doesn't need it should show coarse states (online, recently, a while ago) or let the user hide it. And this page's heartbeat is a client-to-server check; deciding which servers in a cluster are alive is a different failure detector with different costs, covered under leader election and failover.
Compared
| Mechanism | Who sends, who answers | Timing | The right choice when |
|---|---|---|---|
TCP keepalive | The kernel probes, once | Linux defaults (tcp(7)): first probe after 7200 s idle, then 9 probes 75 s apart | You need dead sockets cleaned up eventually, nothing is shown to users, and turning on one socket option is all the work you want |
WebSocket Ping/Pong frames (RFC 6455) | The server sends a Ping; the client answers with a Pong, which a browser does on its own | Whenever the sender chooses; two bytes of overhead plus an optional payload of up to 125 bytes | You control both ends (a server and a non-browser client, say) and want the cheapest check that stays out of your message types |
Server-driven application ping (Socket.IO v4) | The server pings each 25 s by default; the client answers, and also gives up if no ping arrives in 45 s | Detection in about interval + timeout, 45 s with defaults | The server must free per-connection state and update presence on a known schedule, and clients are browsers you don't fully control |
Client-driven keep-alive (MQTT) | The client sends PINGREQ when idle; the broker disconnects after 1.5 times the keep-alive | Chosen by the client at connect, in seconds; zero turns it off | Devices that sleep or pay for traffic should pick their own pace, and the server is happy to let them |
In a picture
Step 1 of 3
The heartbeat loop
The connection server pings on a schedule and the client answers. Each pong resets the server's timer; each ping resets the client's.
How to say it in an interview
The common mistake
Quiz
Questions that use this
Hear it in
Related
Sources
- Socket.IO v4 documentation: Server options
- RFC 6455: The WebSocket Protocol
- RFC 5382: NAT Behavioral Requirements for TCP
- Linux manual pages: tcp(7)
- websocket.org: WebSocket Heartbeat: Ping/Pong, Keep-Alive & Zombie Detection
- HiveMQ: What Is MQTT Keep Alive and Client Take-Over? (MQTT Essentials Part 10)