In a mobile live trivia-show app, how do you choose the heartbeat interval on its live socket, weighing NAT idle timeouts against battery?
answer
- the shortest timer on the path
- the standard's floor is not the network's
- two chances inside the window
- game traffic already resets the idle timer
- interval plus deadline equals detection time
basics
~20 sSet the interval comfortably below the shortest idle timeout assumed on the path, send a heartbeat only after a quiet spell, and give each reply a deadline. Shorter intervals detect death sooner but wake the radio more.
solid answer
~50 sA heartbeat does two jobs: it keeps NAT and proxy mappings from expiring and it proves the server still answers. The NAT standard asks for an idle timeout of at least 2 hours 4 minutes, but that is a requirement on NATs, not a promise about mobile networks, so pick an assumed shortest timeout for the path, say 60 seconds, and heartbeat at about 25 seconds so two heartbeats fit in the window. Send one only when nothing else has crossed the socket for that long, because live game traffic already resets every idle timer. Give each heartbeat a reply deadline, say 10 seconds, so the worst-case detection time is about 35 seconds. The socket is closed in the background, so the battery cost is bounded by the session. Make the interval server-configurable so it can be tuned from measured drops.
go deeper
Remember the two jobs, keeping the path open and proving the server answers, and that the interval sits below the shortest idle timeout.
Size it out loud: assumed timeout, interval with margin, reply deadline, worst-case detection time, and skipping heartbeats when traffic flowed.
Argue the battery bound from closing in the background, and make the interval server-configurable with drop reports per network type.
Weigh detection speed against battery and server load for a fleet of phones, and decide who owns tuning the interval from field data.
## Two jobs for one message A **heartbeat** is a small message a client sends on an otherwise quiet connection. On a phone's live socket it does two separate jobs: - **Keeping the path open.** Between the phone and the game server sit **middleboxes**: the carrier's NAT (network address translation), firewalls, proxies, load balancers. Each keeps a mapping for the connection and forgets it after an **idle timeout** with no traffic. A forgotten mapping silently kills the connection. - **Proving the server is alive.** A heartbeat that gets a reply within a deadline proves the whole path and the server still work. A heartbeat with no reply is the client's evidence that the socket is dead. The protocol frames for this (ping and pong) and their exact rules belong to the protocol. The decision here is the phone's: how often to send, when to skip, and how long to wait. ## The standard's floor is not the network's reality The NAT behaviour standard for TCP says a NAT's idle timeout for an established connection **must not be less than 2 hours 4 minutes**. That is a requirement written for NAT builders. It says nothing about what every middlebox on a mobile network actually does, and the QUIC transport standard itself warns that middlebox state can time out earlier than the recommended figures (it says so for UDP flows). A design therefore picks the **shortest idle timeout it assumes on the path** and states it as an assumption, then corrects it from field data. ## Sizing it for the trivia app Assume, for this scenario, that the shortest idle timeout anywhere on the path is **60 seconds**. 1. **Interval below the timeout, with margin.** At 25 seconds, two heartbeats fall inside any 60-second window, so one lost heartbeat does not let the mapping expire. 2. **A reply deadline.** Wait, say, 10 seconds for the answer. Without a deadline a heartbeat proves nothing. 3. **Worst-case detection.** A path that dies just after a reply is noticed at the next heartbeat plus its deadline: about **25 + 10 = 35 seconds**. 4. **Skip when traffic flowed.** Any message in either direction resets every idle timer, so the client sends a heartbeat only after 25 seconds of silence. During a live round, questions and ticks arrive every few seconds, so heartbeats mostly run in the lobby and between rounds. | Interval | Heartbeats per 30-minute lobby wait | Worst-case detection (10 s deadline) | Risk | |---|---|---|---| | 5 s | 360 | ~15 s | Many radio wake-ups for little gain | | 25 s | 72 | ~35 s | Balanced for a 60 s assumed timeout | | 55 s | ~33 | ~65 s | One lost heartbeat lets a 60 s mapping expire | | 5 min | 6 | ~5 min | Mappings expire between heartbeats | ## The battery side Each heartbeat on cellular can wake the radio, and the radio stays in a high-power state for a while after each burst; the cost model belongs to the device budgets. Two facts keep it bounded here: - the socket is **closed in the background**, so heartbeats only run while the player is in the app; - **skipping heartbeats when traffic flowed** means a live round adds almost none. Halving the interval roughly doubles the heartbeats in a quiet lobby, so a very short interval is paid for in battery with little to show for it. ## Making it tunable The best interval differs by network and changes over time, so the server should send it to the client in the connect handshake (interval and reply deadline), and the client should report drops it detects with the network type. The team can then shorten the interval for networks that drop early without shipping a new app build. Either side may send the heartbeat; the client must at least run its own deadline, because the client is the side that needs to know its socket is dead. ## Common mistakes - Quoting the 2 hours 4 minutes floor as the mobile network's behaviour. - Sending heartbeats on a fixed timer even while game traffic is flowing. - Sending heartbeats with no reply deadline, so a dead path is never noticed. - Hard-coding the interval in the app so it can only change with a release.
- During a live round the server sends a tick every two seconds; is a heartbeat still needed then?Not as a separate message. The ticks already reset every idle timer, and an expected tick that does not arrive is a faster death signal than any heartbeat: if three ticks are missing, the client can treat the socket as dead within about six seconds. Heartbeats matter in quiet phases such as the lobby.
- Why should the client run a reply deadline even if the server is the side that sends pings?The client is the side that must act on a dead socket: reconnect, show a banner, stop accepting answers. If only the server checks, the server learns and the client keeps waiting. A client-side rule such as 'no message of any kind for 35 seconds means dead' gives the client its own signal whoever sends the pings.
- The NAT standard guarantees a mobile path stays open for two hours idle.
- A heartbeat with no reply deadline still detects a dead connection promptly.
- Heartbeats must be sent on a fixed timer even while game data is flowing.
- The interval should be hard-coded because networks do not differ.
- A heartbeat interval longer than the path's idle timeout keeps the mapping alive.