SkillSprout
Chapter 18 · Lesson 1
Intermediate
4 min read

Failure Detection and Heartbeat Basics

Overview

A heartbeat is a small message a machine sends at regular intervals to say "I am alive." If the messages stop arriving for long enough, the receiver concludes the machine has probably failed.

This is how a load balancer stops sending users to a broken server, and how clusters notice that a node has died.

Detailed Notes

What is a heartbeat

A heartbeat is a tiny periodic signal from one machine (or a machine to a monitor) that means "I am still here and working." The component that watches these signals and decides who seems dead is called a failure detector.

Key terms:

  • Interval: how often a heartbeat is sent (for example every 1 second).
  • Timeout: how long the watcher waits without a heartbeat before it suspects failure (for example 10 seconds).
  • Missed heartbeat: an expected heartbeat that did not arrive.

The problem it solves

A crashed machine cannot tell anyone it crashed. In the Load Balancing chapter, a load balancer kept traffic away from unhealthy servers, but how did it know they were unhealthy? It needs a steady stream of evidence. Without heartbeats, a frozen server would keep receiving users who stare at a spinning page.

There is also a deeper difficulty. From the outside, all of these look identical: the machine crashed, the machine is very slow, the network dropped the messages, or the machine is paused for a moment doing housekeeping. You only see silence. So a failure detector can never be certain a node is dead; it can only suspect it.

How it works

  1. Each node sends a heartbeat every interval.
  2. The detector records the last-seen time for every node.
  3. If now minus last-seen exceeds the timeout, the node is marked suspected.
  4. The system reacts: reroute traffic, restart the node, or hand its work to someone else.

Choosing the timeout is a trade-off:

  • Short timeout: failures are noticed fast, but a normal network hiccup causes a false positive (a healthy node declared dead), which triggers needless recovery.
  • Long timeout: few false alarms, but real failures stay undetected longer (users suffer meanwhile). Missing a real failure is a false negative.

A common rule: require several missed heartbeats in a row (for example 3), not just one. A timeout of roughly 3 to 5 times the interval is a typical starting point; tune it using real measurements of your network.

A simple example

A school teacher takes attendance by asking a student to wave every minute during a long field trip. If a student has not waved for 5 minutes, the teacher goes looking. Two minutes is too jumpy (kids wander off for a moment); thirty minutes is too slow to notice a lost child.

System design example

A ride-hailing app needs to know which drivers are online. Each driver app sends a heartbeat every 5 seconds. The server keeps a "last seen" time per driver and treats anyone silent for 30 seconds as offline. The team chose 30 seconds because tunnels and weak signal commonly cause 10-20 second gaps. A shorter timeout made drivers flicker on and off; a longer one sent riders to unreachable drivers.

Also remember that heartbeats only prove a process is responding. A better heartbeat reflects real health, for example "I can reach my database," and not just "my process exists." And because suspicion can be wrong, recovery actions should be safe even if the node was actually alive.

Summary

  • A heartbeat is a regular "I'm alive" signal; silence beyond a timeout means suspected failure.
  • You cannot tell dead from slow, so detection is always a suspicion, not a fact.
  • Short timeout means fast detection but false positives; long timeout means the opposite.
  • Require several missed heartbeats and keep recovery safe if the suspicion was wrong.

Architecture Diagram

Heartbeat failure detection
Architecture

Nodes

Node 1
Node 2
Failure detectorlast-seen table
Reroute / failover

Nodes send periodic beats; the monitor suspects silence.

  1. 1Each node sends a heartbeat every interval.
  2. 2The detector records the last-seen time of each node.
  3. 3A node silent beyond the timeout is marked suspected.
  4. 4The system reroutes traffic or starts recovery.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course