Spring Boot
Kafka
Health Indicator
Java
Application Monitoring

Spring Boot Kafka health indicator: write a custom HealthIndicator

Codemia Knowledge Hub ·

Spring Boot Actuator does not auto-configure a Kafka health indicator. One was added and then reverted (spring-boot issue #12225), and the Spring Boot team declined to bring it back because a client-side check cannot see broker and in-sync replica settings and risks reporting an inaccurate status. To show Kafka in /actuator/health, write your own HealthIndicator bean that checks the brokers you configure.

Why check Kafka's health

Apache Kafka is an open-source stream-processing platform, developed at LinkedIn and donated to the Apache Software Foundation, built for high-throughput, low-latency real-time data feeds. If your application depends on it for messaging or stream processing, Kafka failures can cause data loss, delayed processing or unavailable services. Actuator health indicators already report on things like database connections and disk space, and a Kafka check helps you spot problems early, often before users notice.

Dependencies and configuration

  1. Add the Actuator starter and Spring for Apache Kafka to your pom.xml or build.gradle.
  2. Configure the Kafka connection details in application.properties or application.yml, and enable health details.

Maven example:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-actuator</artifactId>
4</dependency>
5<dependency>
6    <groupId>org.springframework.kafka</groupId>
7    <artifactId>spring-kafka</artifactId>
8</dependency>

application.yml example:

yaml
1spring:
2  kafka:
3    bootstrap-servers: localhost:9092
4management:
5  endpoint:
6    health:
7      show-details: always

What a custom Kafka indicator should do

A custom indicator typically tries to connect to the brokers in spring.kafka.bootstrap-servers. If the connection succeeds, it reports UP. If it fails, it reports DOWN with details of the exception. Spring Boot picks up any HealthIndicator bean and includes it in the health endpoint. Keep the check simple and be aware of its limits: a successful connection does not prove that topics, replicas or transactions are healthy.

A minimal version uses the auto-configured KafkaAdmin to ask the cluster for its ID:

java
1import java.util.concurrent.TimeUnit;
2
3import org.apache.kafka.clients.admin.AdminClient;
4import org.springframework.boot.actuate.health.Health;
5import org.springframework.boot.actuate.health.HealthIndicator;
6import org.springframework.kafka.core.KafkaAdmin;
7import org.springframework.stereotype.Component;
8
9@Component
10public class KafkaHealthIndicator implements HealthIndicator {
11
12    private final KafkaAdmin kafkaAdmin;
13
14    public KafkaHealthIndicator(KafkaAdmin kafkaAdmin) {
15        this.kafkaAdmin = kafkaAdmin;
16    }
17
18    @Override
19    public Health health() {
20        try (AdminClient client = AdminClient.create(kafkaAdmin.getConfigurationProperties())) {
21            String clusterId = client.describeCluster().clusterId().get(5, TimeUnit.SECONDS);
22            return Health.up().withDetail("clusterId", clusterId).build();
23        } catch (Exception e) {
24            return Health.down(e).build();
25        }
26    }
27}

Spring Boot derives the name from the bean, so this shows up as kafka in /actuator/health.

FeatureDescription
DependencyRequires spring-boot-starter-actuator and spring-kafka.
Properties ConfigurationMust configure Kafka brokers in application.yml or application.properties.
Health StatusA custom indicator returns UP if the broker connection succeeds, otherwise DOWN.
Endpoint UsageAccessible via /actuator/health when Spring Boot Actuator’s web exposure includes 'health'.

Health is not performance

A connection check says nothing about latency or throughput. For full monitoring, add metrics and logging that show how Kafka performs inside your application, which matters most in event-driven systems that rely heavily on asynchronous processing.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design