← Back to distributed systems

Distributed Systems Topic

Heartbeats & Health Checks

Learn how to monitor node health and detect failures in distributed systems.

Intermediate8 min read

Heartbeats and health checks are mechanisms to detect node failures and monitor system health in distributed systems.


Heartbeats

Periodic messages sent to indicate a node is alive.

Implementation

TypeScript
Python
Java
1

Health Checks

Endpoints that report node health status.

Liveness vs Readiness

Liveness: Is the process running?

Readiness: Can the process handle requests?

TypeScript
Python
Java
1

Examples

Kubernetes Health Checks

TypeScript
Python
Java
1

Leader Election with Heartbeats

TypeScript
Python
Java
1

Common Pitfalls

  • Too frequent heartbeats: Network overhead. Fix: Balance frequency with detection time
  • Not handling network delays: False positives. Fix: Use timeout > 3x interval
  • Single health check: May miss issues. Fix: Check multiple components
  • Not failing fast: Unhealthy nodes continue serving. Fix: Remove from load balancer
  • No graceful shutdown: Health check fails during shutdown. Fix: Implement graceful shutdown

Interview Questions

Beginner

Q: What are heartbeats and health checks used for?

A:

Heartbeats: Periodic messages to indicate a node is alive. Used for failure detection.

Health checks: Endpoints that report node health. Used to determine if node can handle requests.

Purpose:

  • Failure detection: Know when nodes fail
  • Load balancing: Route traffic only to healthy nodes
  • Auto-recovery: Restart or replace failed nodes
  • Monitoring: Track system health

Intermediate

Q: How do you implement health checks for a microservice? What's the difference between liveness and readiness?

A:

Liveness: Is the process running?

  • Simple check: Process is alive
  • Use: Kubernetes will restart if fails

Readiness: Can the process handle requests?

  • Comprehensive check: Database, cache, dependencies all working
  • Use: Load balancer routes traffic only if ready

Implementation:

TypeScript
Python
Java
1

Senior

Q: Design a failure detection system for a distributed system with 1000+ nodes. How do you detect failures quickly while minimizing network overhead?

A:

Design:

TypeScript
Python
Java
1

Optimizations:

  • Hierarchical: Reduce network messages (nodes → cluster → region)
  • Gossip: O(log n) messages instead of O(n)
  • Adaptive: Adjust frequency based on failure rate
  • Sampling: Check subset of nodes, rotate

Key Takeaways

Heartbeats indicate nodes are alive, used for failure detection

Health checks report node status (liveness vs readiness)

Liveness: Process running (restart if fails)

Readiness: Can handle requests (route traffic if ready)

Failure detection: Use timeouts (3x heartbeat interval)

Minimize overhead: Use hierarchical or gossip-based approaches

Quick detection: Balance frequency with network overhead

Keep exploring

Partial failure and consistency show up together in real systems. Continue with the next hub topic that stresses the same idea.