Health Checks and Registration
What you'll learn: How to ensure only healthy service instances receive traffic by implementing health check endpoints and registration logic.
The Problem with Blindly Routing Traffic
Once a service registers with a service registry (like Consul or Eureka), you need a mechanism to ensure only healthy instances receive traffic. A registered service might crash, run out of memory, lose database connectivity, or become unresponsive. Without health checks, the registry would continue routing requests to broken instances, causing failures.
What Are Health Checks?
A health check is an endpoint (typically /health or /actuator/health) that your service exposes. The registry periodically pings this endpoint to verify the instance is alive and ready to handle requests. Think of it like a heartbeat monitor in a hospital—constant verification that vital signs are good.
Types of Health Checks
- Liveness check: Is the service process running? (Basic "am I alive?")
- Readiness check: Is the service ready to accept traffic? (Dependencies like databases connected?)
Registration with Health Checks
When a service starts up, it registers itself with the service registry and provides its health check endpoint URL. The registry then:
- Periodically polls the health endpoint (e.g., every 10 seconds)
- Marks the instance as unhealthy if checks fail consecutively
- Removes it from the load balancer pool automatically
- Re-adds it once health checks pass again
Service Instance → Registry: "I'm at 192.168.1.5:8080, check /health"
Registry → Service (every 10s): GET /health
Service → Registry: 200 OK {"status": "UP"}
This creates a self-healing system where failing instances are automatically removed from rotation without manual intervention.
Key Takeaway: Health checks and proper registration ensure your service registry routes traffic only to instances that can successfully handle requests, preventing cascading failures in distributed systems.