ReviseAlgo Logo

Distributed System Concerns

Heartbeats

Periodic signals that let a system detect when a node has failed.

In short

Periodic signals that let a system detect when a node has failed.

Last Updated: June 26, 2026 21 min read

In a distributed network, there is no centralized physical observer that can instantly declare when a machine has crashed. A node might be dead, frozen in a long garbage collection (GC) pause, or simply temporarily partitioned by a network router. To detect when nodes fail, systems use Heartbeats—small, periodic "I'm alive" messages sent between nodes.

1. Learning Objectives

  • Explain why remote node crashes cannot be detected instantly.
  • Differentiate between Push (Active ping) and Pull (Passive health check) heartbeat models.
  • Determine optimal heartbeat intervals and timeout thresholds.
  • Analyze false positive failure detections caused by network jitter and garbage collection pauses.
  • Describe advanced membership detection algorithms such as the Gossip Protocol and Phi Accrual Failure Detector.
  • Implement a thread-safe Heartbeat Monitor and Node simulator in Java, Python, and C++.

2. Prerequisites

Before learning about failure detection, make sure you understand:

  • TCP/UDP Protocols: How lightweight packets travel over networks.
  • Multithreading: Running periodic tasks using timers or schedulers.
  • Service Discovery: How dynamically discovered nodes register and deregister.

3. Why This Topic Matters

Without heartbeats, distributed systems cannot react to failures.

If a primary database node crashes and there is no heartbeat mechanism to detect it, replica nodes will wait indefinitely. Traffic will continue routing to the dead IP, causing API calls to fail for all users.

Heartbeats enable core clustering capabilities:

  • Auto-Failover: Promotes a replica node to primary if the primary stops sending heartbeats.
  • Automatic Rerouting: Directs traffic away from nodes flagged as dead.
  • Cluster Membership: Keeps track of which machines are currently active in the cluster.

4. Real-world Analogy

Think of a Deep-Sea Diver connected to a surface support boat:

The diver cannot speak to the surface team due to the water depth. To prove they are safe and conscious, they pull on their safety rope once every 30 seconds.

The Surface Team (Monitor): Holds the rope. Every time they feel a tug, they reset their timer.

If 60 seconds pass without a tug (missing two expected heartbeats), the surface team assumes the diver is in danger and pulls them up immediately (initiating failover recovery).

5. Core Concepts

  • Push Heartbeat Model (Active Ping): The monitored node sends periodic heartbeats to a central monitor server.
    Note: Low monitor overhead, but requires nodes to know where the monitor resides.
  • Pull Heartbeat Model (Passive Health Check): The monitor server queries the monitored node's /health endpoint periodically.
    Note: Gives the monitor control over query rates and allows it to collect detailed health metrics, but adds connection overhead on the monitor.
  • Heartbeat Interval: The frequency at which heartbeats are sent (e.g. every 5 seconds).
  • Heartbeat Timeout: The duration of inactivity required before declaring a node dead (typically set to $3 \times$ the heartbeat interval to tolerate network jitter).
  • False Positives: Occur when a healthy node is incorrectly flagged as dead due to a temporary network drop or a long GC pause.
  • Gossip Protocol Failure Detection: A decentralized approach where cluster nodes swap status metadata randomly, disseminating node health updates without a central monitor.

6. Visualizations

Push vs. Pull Monitoring Models

False Positive Node Failure Cascade

7. How It Works Step-by-Step

  1. Node Setup: A node joins the cluster and registers with the monitor, specifying its heartbeat interval (e.g. 5 seconds).
  2. Periodic Transmission: The node starts a background thread that sends a lightweight UDP packet to the monitor every 5 seconds.
  3. Record Updates: The monitor receives the heartbeat packet, extracts the node ID, and updates the node's lastHeartbeat timestamp in its memory table.
  4. Periodic Scan: The monitor runs a background task every 2 seconds to check all registered nodes: $$\text{Elapsed Time} = \text{Current Time} - \text{lastHeartbeat}$$
  5. Death Declaration: If Elapsed Time > 15 seconds (missing 3 heartbeats), the monitor flags the node as DEAD and triggers the cluster recovery workflow.

8. Internal Architecture

A high-scale cluster monitoring system isolates heartbeat handling to protect core application performance:

  • Lightweight Transport (UDP): Heartbeat packets are often sent over UDP instead of TCP. UDP avoids the connection overhead of handshakes and retries, ensuring heartbeats do not block application threads.
  • Dedicated Watchdog Thread Pool: Monitors isolate health checks to dedicated thread pools to prevent slow client queries from blocking system health checks.
  • Phi Accrual Failure Detector: Instead of using fixed timeouts, advanced systems (like Cassandra) use historical packet delivery times to calculate a probability score ($\Phi$) of whether a node is down, adapting to network conditions dynamically.

9. Request Lifecycle

Let's trace a client call routing around a node that has stopped sending heartbeats:

  1. Healthy Operations: Clients route requests to Node A and Node B. Both nodes are sending heartbeats.
  2. Node B Outage: Node B suffers a power failure and crashes.
  3. Heartbeat Timeout: The cluster monitor stops receiving heartbeats from Node B. After 15 seconds, the monitor declares Node B dead.
  4. Registry Eviction: The monitor updates the service registry, removing Node B from the active list.
  5. Client Call Routing: A client requests a new connection. The API gateway receives the updated registry list and routes all new requests to Node A.

10. Deep Dive

A. Phi ($\Phi$) Accrual Failure Detector

In a public cloud, network latency is unpredictable. A fixed heartbeat timeout (e.g. 10 seconds) will either flag healthy nodes during temporary network spikes (false positives) or take too long to detect real crashes.

The Phi Accrual Failure Detector solves this by modeling packet arrival history using a normal distribution. It outputs a value, $\Phi$, representing the probability that a node is offline: $$\Phi = -\log_{10}(P_{\text{later}}(t - t_{\text{last}}))$$ Where $t - t_{\text{last}}$ is the time since the last heartbeat.

If $\Phi \ge 8$, the probability of a false positive is low, and the system can safely trigger failover. This allows the system to adapt dynamically, using short timeouts during stable network conditions and longer timeouts during network spikes.

11. Production Examples

  • Kubernetes Probes: Uses liveness probes (pull model health checks) to monitor pod health. If a pod fails its health check repeatedly, Kubernetes restarts the container automatically.
  • Apache Cassandra: Uses a decentralized Gossip Protocol combined with a Phi Accrual Failure Detector to track node health across large database clusters.
  • Consul Health Checks: Supports script, HTTP, and TCP checks, running them locally on agent nodes to offload check work from core servers.

12. Advantages

  • Automated Outage Detection: Eliminates the need for manual operations intervention during node crashes.
  • Low Bandwidth Overhead: Heartbeat pings are tiny payloads (a few bytes), ensuring minimal network overhead.
  • Continuous Validation: Verifies that application threads, network cards, and operating systems are running correctly.

13. Limitations

  • False Positives: Heavy GC pauses or temporary network drops can cause healthy nodes to be evicted, leading to unnecessary failover overhead.
  • Detection Delay: The system must wait for the timeout window to expire before declaring a node dead, causing requests to fail during the timeout period.
  • Scale Bottlenecks: A central monitor tracking thousands of nodes can experience network saturation from heartbeat traffic.

14. Trade-offs

  • Short vs. Long Timeout Windows: Short timeout windows (e.g. 2 seconds) detect node failures quickly but are highly vulnerable to false positives during transient network spikes. Long timeout windows (e.g. 30 seconds) avoid false positives but delay failover recovery, increasing client error rates.
  • Push vs. Pull Models: The Push model requires minimal resource usage on the monitor but offers limited detail about node internal health. The Pull model allows the monitor to verify API routing and check sub-dependencies, but consumes more network and CPU resources.

15. Performance Considerations

  • Use Lightweight Payloads: Keep heartbeat payloads minimal (typically containing just the node ID and a sequence counter) to conserve network bandwidth.
  • Heartbeat over UDP: Use UDP instead of TCP for health pings to avoid the network overhead of handshakes and packet retransmissions.

16. Failure Scenarios

  • GC Pause Outage (False Death): A Java database node executes a major garbage collection pause, freezing all application threads. The node stops sending heartbeats, causing the monitor to flag it as dead and trigger a replica promotion.
    Mitigation: Configure failover triggers to require multiple check sources, or use adaptive accrual detectors that tolerate longer pauses.
  • Network Partition Split-Brain: A network partition divides a cluster into two segments. Nodes in each segment stop receiving heartbeats from the other side, causing both sides to attempt leader election.
    Mitigation: Require a quorum (majority vote) to elect a new leader, preventing split-brain states where two leaders run simultaneously.

17. Best Practices

  • Set the timeout window to at least $3 \times$ the heartbeat interval to tolerate transient network drops.
  • Isolate heartbeat execution threads from application request handlers to ensure health checks run reliably under load.
  • Combine heartbeats with application health metrics (e.g. disk space, database connection pool health) to ensure nodes are fully functional.

18. Common Mistakes

  • Setting timeouts too low, causing healthy nodes to be evicted during brief network drops.
  • Sending heartbeats over TCP without setting socket connection timeouts, which causes the monitor threads to hang.
  • Failing to monitor disk space and memory leaks on the monitor server, which can cause the heartbeat tracker itself to fail.

19. Implementation (Heartbeat Monitor)

Below is a complete implementation of a thread-safe Heartbeat Monitor and Node simulator in Java, Python, and C++. The simulator models periodic node pings, monitors heartbeat timeouts, evicts dead nodes, and handles node recovery.

20. Interview Questions & Answers

Q1. Why are heartbeats usually sent over UDP instead of TCP in clustering setups?

Answer: UDP is a connectionless, lightweight protocol. It does not require connection handshakes, maintain socket state, or retransmit lost packets. Heartbeat pings are frequent and minor; if one ping is dropped, the next ping will arrive shortly. Using TCP would create unnecessary connection overhead, socket buffering limits, and retry queues on the monitor.

Q2. What is a garbage collection pause, and how does it cause false positive failure declarations?

Answer: In managed memory languages (like Java or C#), major garbage collection (Stop-The-World GC) pauses execution threads to clean up memory.

During a major GC pause, the application cannot process requests or send heartbeats. If the GC pause duration exceeds the monitor's timeout limit, the monitor will assume the node has crashed and trigger a failover, even though the node is healthy and simply paused.

Q3. How does the Gossip Protocol handle failure detection in decentralized systems?

Answer: In a decentralized Gossip network (like Cassandra), there is no central monitor. Instead, each node maintains a local state table containing status logs and versions for all nodes.

Every second, each node randomly selects another node and swaps its state table. If a node stops updating its version number, peers will detect the inactivity and propagate a failure flag across the network, notifying the entire cluster.

21. Practice Exercises

  • Exercise 1 (Easy): Trace a diagram showing state transitions for Node A and Node B monitored by a single surface ping server.
  • Exercise 2 (Medium): Modify the Python HeartbeatMonitor code to support grace periods. If a node is declared dead, wait for 3 consecutive check loops to fail before triggering eviction.
  • Exercise 3 (Hard): Write a Python mock simulation of a Gossip Protocol cluster membership tracker. Implement state table swaps between 5 virtual nodes.

22. Challenge Problem

The Garbage Collection Storm Challenge: You operate a search index cluster of 100 Java nodes. The cluster monitor has a timeout window of 10 seconds.

During high search volumes, memory pressure increases, causing 10 nodes to trigger Stop-The-World GC pauses at the same time. The pauses last 12 seconds.

  • Explain the cascade failure that occurs when the monitor evicts these 10 healthy nodes.
  • Describe how you would design a Phi Accrual Failure Detector to handle dynamic latency spikes and prevent false evictions.
  • Provide pseudocode showing how the monitor calculates the accrual score using historical packet timings.

23. Summary

Heartbeats are the foundational failure detection mechanism in distributed systems. Nodes send periodic pings to a monitor to confirm they are active. Balancing heartbeat intervals and timeout thresholds is key to preventing false positive failure declarations while ensuring fast recovery times during real node crashes.

24. Cheat Sheet

Feature Push Model (Active Ping) Pull Model (Passive Check)
Initiator The monitored node pings the monitor. The monitor queries node health endpoints.
Network Load Low (lightweight UDP packets). Higher (requires full HTTP connections).
Failure Sensitivity High (detects connection crashes). Very High (verifies app logic and database status).
Best Use Case Database cluster node synchronization. Kubernetes container liveness monitoring.

25. Quiz

1. What is the primary purpose of a heartbeat ping in distributed networks?

  • A. To load balance API queries.
  • B. To verify remote node status and detect failures.
  • C. To synchronize database transactions.
  • D. To compress database indexes.

Answer: B. Heartbeats confirm nodes are alive by sending periodic pings.

2. Why are heartbeat timeouts typically set to $3\times$ the heartbeat interval?

  • A. To save memory on the server.
  • B. To tolerate transient network drops and prevent false positive evictions.
  • C. To encrypt the heartbeat packets.
  • D. To speed up leader elections.

Answer: B. A buffer of missed pings prevents false positive dead declarations during brief drops.

3. Which protocol is ideal for sending lightweight health checks?

  • A. HTTPS.
  • B. UDP.
  • C. TCP.
  • D. SMTP.

Answer: B. UDP avoids connection setup and retransmission overhead, making it ideal for frequent pings.

4. What is a garbage collection pause?

  • A. A period when the database is backed up.
  • B. A stop-the-world event where a managed runtime pauses application threads to clean up memory.
  • C. A hard drive defragmentation task.
  • D. A network card reset.

Answer: B. STW GC pauses freeze execution threads, which can cause nodes to miss heartbeat pings.

5. How does a Pull model health check function?

  • A. The client pushes its IP address.
  • B. The central monitor queries the node health status endpoint.
  • C. The database runs query joins.
  • D. The node restarts itself.

Answer: B. In the Pull model, the monitor queries nodes for health status updates.

6. In a Gossip network, how is node health information shared?

  • A. Through a central load balancer.
  • B. Nodes periodically swap membership status tables with random peers.
  • C. By saving data to a central database.
  • D. Using SMS alerts.

Answer: B. Gossip protocols distribute health states randomly peer-to-peer.

7. What does the Phi Accrual Failure Detector calculate?

  • A. The network bandwidth limit.
  • B. The probability score that a monitored node is down based on historic latency.
  • C. The encryption key.
  • D. The size of the database.

Answer: B. The Phi detector estimates failure probability dynamically, adjusting to network conditions.

8. What is the risk of a false positive dead node declaration?

  • A. Data encryption keys are deleted.
  • B. Triggering unnecessary failover overhead, node replication, and route changes.
  • C. The server hard drive locks.
  • D. Users are logged out.

Answer: B. False declarations cause unnecessary failovers, consuming network and compute resources.

9. Which of the following is a pull-based health monitoring system?

  • A. Cassandra gossip.
  • B. Kubernetes container liveness probes.
  • C. Redis GEOADD.
  • D. TCP handshakes.

Answer: B. Kubernetes pulls health status by querying container endpoints.

10. What does a monitor do after declaring a node dead?

  • A. It updates the database password.
  • B. It removes the node from the active registry and triggers failover processes.
  • C. It formats the server hard drive.
  • D. It contacts the user.

Answer: B. Pruning dead nodes prevents traffic from routing to dead IP addresses.

26. Further Reading

27. Next Lesson Preview

Failure detection works hand-in-hand with how microservices hold state. In the next lesson, we will look at Stateful vs Stateless architectures—the structural division that determines how microservices scale and handle failover.

Key takeaways

  • Periodic signals detect node failure when beats stop arriving.
  • Balance interval: too fast wastes resources, too slow delays detection.
  • Tolerate a few missed beats to avoid false-positive failures.