Representative interview topic

General Interview: TCP Keepalive vs Application Heartbeats

GeneralMedium
Offer.cc Editorial TeamPublished Updated

Question

What is the difference between TCP Keepalive and an application heartbeat, and how would you set detection, timeouts, proxy idle limits, and reconnect behavior for a long-lived service?

1. Question and Context

A long-lived client connects through NAT, a load balancer, and a server. After a network break, the operating system still reports the socket as established. The team debates whether TCP Keepalive alone is enough or whether the protocol should add ping/pong messages. Compare the mechanisms and propose detection, timeout, proxy, and reconnect behavior. Assume the business needs to know whether the session can still process requests, not merely whether a network interface is reachable.

2. What the Interviewer Is Testing

  • Whether you distinguish transport probing from application-availability probing.
  • Whether you know that TCP Keepalive uses kernel timers, so defaults and middlebox behavior are not a business SLA.
  • Whether you can design a bounded heartbeat, timeout, close, and reconnect state machine without false positives or storms.
  • Whether you include NAT, proxy idle timeouts, mobile networks, and server load in an end-to-end design.

3. Clarifying Questions to Ask First

  1. Do we need to detect the network path, the TCP peer process, or application-session availability?
  2. What idle timeout applies at the NAT, gateway, and load balancer?
  3. Does the connection carry replayable reads, or commands and transactions that require confirmation?
  4. How many clients are there, what extra bandwidth is acceptable, and how many reconnects may run concurrently?

4. A 30-Second Answer Framework

TCP Keepalive is sent by the kernel and primarily discovers an idle half-open TCP peer or broken path. An application heartbeat is defined by the protocol and can verify that the peer application still reads, writes, and returns a business acknowledgement. I would define healthy, suspect, closed, and backoff states, then set the heartbeat interval from the shortest middlebox idle timeout. Keepalive is the transport fallback; the application heartbeat owns the business SLA. On timeout, close the old socket, reconnect with jittered exponential backoff, and cap concurrency.

5. Step-by-Step Deep Answer

Step 1: Define What Each Mechanism Observes

TCP Keepalive works at the socket layer. Linux tcp(7) exposes an idle period, probe interval, and probe count; an ACK proves only that the TCP stack can answer. It does not prove that the application is authenticated, its lease is valid, or it can process a request. Keepalive is useful for discovering silent half-open connections and releasing kernel resources.

An application heartbeat is a protocol message, such as ping carrying session or capability data, answered by a pong or a stateful response. It can detect a blocked event loop, an expired lease, invalid authentication, or an overloaded service that TCP cannot see. It costs application CPU, bandwidth, and connection capacity.

Step 2: Set Parameters from an End-to-End Time Budget

Find the shortest middlebox idle timeout T_idle, then choose a heartbeat interval below it with jitter margin, for example T_hb <= T_idle / 2. Keep the application response deadline below the business’s allowed disconnected time; require N consecutive misses before declaring death so one dropped packet does not kill a healthy session. TCP Keepalive can use a longer idle period as a transport fallback during application silence; it cannot replace the application SLA.

Step 3: Design States and Actions

After connecting, enter healthy. Sending a heartbeat moves to suspect; an application response before the deadline returns to healthy. Consecutive failures close the socket and enter backoff. Use exponential growth with random jitter and a cap; pause dialing while offline or while a page is hidden, then run a health check before resuming. Close the old socket first so two connections cannot consume one command stream.

Step 4: Recover According to Message Semantics

Notifications may be dropped and resubscribed after reconnect. A write command must not rely on heartbeat acknowledgement: attach an idempotency key or sequence number, receive an explicit confirmation, and record the last contiguous confirmation point. After reconnect, replay from a cursor or query state. If execution is uncertain, read server state before deciding whether to retry.

Step 5: Verify Middleboxes and Operational Signals

Use packet capture or connection logs to confirm that heartbeats cross the NAT, proxy, and load balancer. Record heartbeat RTT, timeout rate, failed Keepalive probes, connection age, reconnect attempts, backoff duration, and concurrency peaks. Exercise unplugged networks, NAT reclamation, a blocked server event loop, expired authentication, and a synchronized disconnect. An ESTABLISHED TCP state alone does not prove business availability.

6. High-Quality Sample Answer

I separate the two mechanisms by layer. TCP Keepalive is a kernel probe that can find a silent half-open TCP connection, but an ACK does not mean the application can process a request. An application heartbeat can verify protocol, authentication, and lease health, so it owns the business decision. I measure the shortest NAT or load-balancer idle timeout, set the heartbeat at roughly half of it, and add a response deadline and consecutive-failure threshold. A connection state machine moves between healthy, suspect, closed, and jittered backoff; timeout closes the old socket before reconnecting. Notifications resubscribe after recovery, while commands use idempotency keys and confirmation cursors. Keepalive is a fallback during application silence, not a replacement for a heartbeat or an unlimited retry loop.

7. Common Mistakes

  • Treating a TCP ACK as proof of business health → the kernel may answer while the application thread is stuck → use an application request-response heartbeat.
  • Adopting system Keepalive defaults unchanged → the idle period may exceed a proxy timeout → configure from the end-to-end time budget.
  • Reconnecting after one missed heartbeat → a short packet loss creates a storm → use a deadline, consecutive-failure threshold, and jittered backoff.
  • Tuning only the client heartbeat → a NAT or load balancer may still reclaim earlier → inspect every middlebox.
  • Blindly replaying write commands after reconnect → charges or creates resources twice → use idempotency keys, confirmation points, and state reads.

8. Follow-up Questions and Responses

Follow-up 1: If an application heartbeat is stronger, why keep TCP Keepalive?

The application may be silent for a long time or its protocol code may be broken. Keepalive can discover a dead transport during that silence and release the socket. It is a lower-layer fallback, not a business acknowledgement.

Follow-up 2: Should the heartbeat interval be as short as possible?

No. Shorter intervals detect failures faster but consume more CPU, bandwidth, mobile battery, and server concurrency. First satisfy the middlebox idle budget, then use fault drills to measure false positives and detection delay, with tiers for different connection types.

Follow-up 3: How do you avoid a reconnect storm after a fleet-wide restart?

Clients use exponential backoff with random jitter. The server rate-limits by tenant or connection type and may provide a retry time. Offline clients stop dialing; when connectivity returns, they reconnect in batches and expose the peak for monitoring.

Public sources

Related questions