Skip to content
Kafka Is Fun

SpringSpring Kafka Advanced

What is the KafkaAdmin bean and how do you use it to manage topics?

Reference answer

KafkaAdmin is Spring Kafka's wrapper around the Kafka AdminClient. Spring Boot registers a KafkaAdmin bean automatically, so you only need NewTopic (or NewTopics) beans. With a KafkaAdmin bean in the context, each NewTopic bean is added to the broker at startup: @Bean NewTopic ordersTopic() { return TopicBuilder.name('orders').partitions(6).replicas(3).build(); }. If a topic already exists, the admin increases the number of partitions when the existing topic has fewer than NewTopic.numPartitions; it does not reduce them. By default, if the broker is not available a message is logged and the context still loads; set fatalIfBrokerNotAvailable to true to make that fatal, and call initialize() to try again later. KafkaAdmin also offers runtime methods (since 4.0 including deleteTopics), but nothing is deleted automatically. For other operations, such as partition reassignment or config changes, use the AdminClient directly.

Expected key concepts: KafkaAdmin, AdminClient, NewTopic, TopicBuilder, Spring Boot, startup, partitions, replicas, fatalIfBrokerNotAvailable, initialize

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.