Return to site

🩺🟢 HEALTH CHECKS IN SPRING BOOT (ACTUATOR) — KEEP YOUR APPS “ALIVE” FOR REAL

· java

✅ TL;DR

Health checks are your app’s “medical report”: Actuator exposes them, Kubernetes uses them, and custom indicators let you reflect real dependency + business readiness.

Section image

🔸 WHAT ARE HEALTH CHECKS (DEFINITION)

Health checks are lightweight endpoints that report whether your app is up and whether its dependencies (DB, Kafka, Redis, disk space, etc.) are reachable. They’re vital for monitoring, alerting, and especially for orchestrators (Kubernetes) to make smart decisions (restart? route traffic? wait?).

🔸 WHY THEY’RE VITAL (MONITORING + OPERATIONS)

▪️ Detect failures early (DB down, disk full, downstream timeout…)

▪️ Give SRE/ops a quick “what’s broken?” signal

▪️ Enable safer deployments (don’t send traffic until ready)

▪️ Reduce noisy incidents (restart only when truly unhealthy)

🔸 ADDING SPRING BOOT ACTUATOR (MAVEN)

Add Actuator to expose health endpoints and operational insights:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

🔸 CONFIGURING HEALTH ENDPOINTS (APPLICATION.YML)

Example configuration you’ll often see in production:

management:
  endpoints:
    web:
      exposure:
        include: health,info
  endpoint:
    health:
      probes:
        enabled: true
      show-details: when_authorized
  health:
    livenessstate:
      enabled: true
    readinessstate:
      enabled: true

✅ probes.enabled helps Kubernetes-style probe endpoints

🔒 show-details: when_authorized avoids leaking internals publicly

🔸 LIVENESS VS READINESS (DON’T MIX THEM)

▪️ Liveness: “Should the platform restart me?” (deadlocked, broken state, stuck)

▪️ Readiness: “Can I receive traffic?” (warming up, migrations running, dependency unavailable)

This separation prevents a classic anti-pattern: restarting a perfectly fine app that’s just temporarily not ready.

🔸 CUSTOM HEALTH INDICATOR (HEALTHINDICATOR)

Want your own business/dependency signal? Implement HealthIndicator:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
class DownstreamApiHealthIndicator implements HealthIndicator {

  @Override
  public Health health() {
    boolean ok = pingDownstream(); // your real check
    if (ok) {
      return Health.up()
          .withDetail("downstreamApi", "reachable")
          .build();
    }
    return Health.down()
        .withDetail("downstreamApi", "unreachable")
        .build();
  }

  private boolean pingDownstream() {
    return true; // replace with real call / timeout / cache
  }
}

💡 Tip: keep health checks fast, cached if needed, and avoid cascading failures (timeouts!).

🎯 TAKEAWAYS

▪️ Add spring-boot-starter-actuator for production-grade observability

▪️ Expose health (and info) intentionally — not everything

▪️ Use readiness to control traffic, liveness to control restarts

▪️ Custom HealthIndicator = your app tells the truth about what it depends on

#SpringBoot #Spring #Actuator #Java #Kubernetes #Observability #SRE #DevOps #Microservices #CloudNative #Monitoring #ProductionReady

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇