Building Blocks

Heartbeat

Understanding heartbeat mechanisms for failure detection in distributed systems.

3 min readConcepts · System Design

What is a Heartbeat?

A Heartbeat is a periodic signal sent between nodes in a distributed system to indicate they are alive and functioning. If a node stops sending heartbeats, other nodes assume it has failed.


How It Works


Heartbeat Types

Push-Based

Node sends heartbeats to monitor:

Pull-Based (Polling)

Monitor polls nodes for status:

Peer-to-Peer

Nodes monitor each other:


Key Parameters

ParameterDescriptionTypical Value
IntervalTime between heartbeats1-5 seconds
TimeoutMissed beats before failure3x interval
Grace periodBuffer before action5-15 seconds

Failure Detection

False Positives

A slow network can cause false failure detection:

CauseMitigation
Network partitionIncrease timeout
GC pauseUse longer grace period
CPU overloadAdaptive thresholds
Packet lossRequire multiple missed beats

Heartbeat with Metadata

Heartbeats often carry useful information:

{
  "node_id": "node-3",
  "timestamp": 1710432000,
  "status": "healthy",
  "cpu_usage": 45,
  "memory_usage": 72,
  "active_connections": 1250,
  "version": "2.1.0"
}

This enables load balancers and orchestrators to make routing decisions.


Real-World Examples

SystemMechanism
Kuberneteskubelet → API server (10s default)
ZooKeeperSession heartbeats (ephemeral nodes)
CassandraGossip protocol (1s interval)
Redis SentinelPING/PONG every 1s
ConsulTCP/HTTP health checks

Heartbeat vs Health Check

AspectHeartbeatHealth Check
DirectionNode → MonitorMonitor → Node
Content"I'm alive""Are you healthy?"
DepthLiveness onlyLiveness + readiness
OverheadLowHigher (runs checks)

Best Practices

  1. Set timeout > 2x interval to tolerate occasional delays
  2. Include metadata for smarter load balancing
  3. Use adaptive timeouts based on network conditions
  4. Avoid single monitor — use distributed failure detection
  5. Distinguish slow from dead — don't kill overloaded nodes

Interview Tips

  • Explain push vs pull heartbeat models
  • Discuss false positive problem and mitigations
  • Know typical intervals (1-5s) and timeout multipliers
  • Mention use in leader election and cluster membership
  • Give examples: Kubernetes, ZooKeeper, Cassandra gossip

Finished reading?

Mark it done to track your progress in Concepts.