Spring Boot Actuator for Production Monitoring

Difficulty: Intermediate

Question

What is Spring Boot Actuator, which endpoints matter most, and how do you secure them?

Answer

Shipping code is only half the job; you also need to know whether it is healthy once it is running. Is the database connection alive? How much memory is the JVM using? Which log level is active? Actuator is the Spring Boot module that answers those questions by exposing operational endpoints, and it is the foundation of monitoring for Boot microservices.

You add spring-boot-starter-actuator. Immediately you get endpoints under /actuator. By default over HTTP only health is exposed in Boot 2.5 and later, to be safe, and you opt in to others with management.endpoints.web.exposure.include. The important ones: health reports UP or DOWN by aggregating health indicators for the database, disk space, Redis, message brokers, and anything custom you register. info shows build and Git information. metrics exposes numbers such as JVM memory, GC, HTTP request counts and latencies, and connection pool statistics. prometheus, with micrometer-registry-prometheus, publishes these in a format a Prometheus server scrapes for Grafana dashboards. env and configprops show the configuration, loggers lets you change log levels at runtime without restart, threaddump and heapdump help debug production issues, mappings lists request mappings, beans lists the context, and conditions shows the auto-configuration report.

Micrometer is the metrics library underneath. Think of it as SLF4J for metrics: you write to a vendor-neutral API, and registries ship the data to Prometheus, Datadog, CloudWatch, or New Relic. You can add your own counters and timers with MeterRegistry, for example counting successful payments or timing a third-party call.

Health has two special uses in Kubernetes. Liveness answers is the process alive, and if it fails Kubernetes restarts the pod. Readiness answers can it serve traffic now, and if it fails the pod is removed from the load balancer without restarting. Boot supports these as health groups at /actuator/health/liveness and /actuator/health/readiness, enabled by management.endpoint.health.probes.enabled=true. A common mistake is making liveness depend on the database, so a database outage restarts every pod and worsens the incident. Keep liveness simple.

Security matters a lot here. Endpoints like env, heapdump, and loggers can leak secrets or let attackers change behaviour. Only expose what you need, place Actuator behind Spring Security with role restrictions, or move it to a separate management port using management.server.port so it is not reachable from the public internet. Sensitive values are sanitised by default, but do not rely on that alone.

Custom health indicators are easy: implement HealthIndicator and return Health.up() or Health.down().withDetail(...). Use this for a payment gateway ping, but keep the check quick and cached, since the health endpoint may be polled every few seconds.

Code examples

Configuring and extending Actuator

# application.yml
management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus,loggers
  endpoint:
    health:
      show-details: when-authorized
      probes:
        enabled: true
  server:
    port: 9091            # separate management port, not public

@Component
class PaymentGatewayHealth implements HealthIndicator {
    private final GatewayClient client;
    PaymentGatewayHealth(GatewayClient client) { this.client = client; }

    @Override
    public Health health() {
        return client.ping()
                ? Health.up().withDetail("gateway", "reachable").build()
                : Health.down().withDetail("gateway", "timeout").build();
    }
}

@Service
class PaymentService {
    private final Counter successCounter;
    PaymentService(MeterRegistry registry) {
        this.successCounter = registry.counter("payments.success");
    }
    void onSuccess() { successCounter.increment(); }
}

The custom indicator appears in the aggregate health, and Micrometer counters flow to Prometheus.

Key points

Common follow-up questions

Interview tips

Concepts covered

Actuator, Health Checks, Micrometer, Endpoints

More Spring Boot questions