Skip to content
Kafka Is Fun

Getting Started · Beginner · Topic 5 of 5

Topic Creation with KafkaAdmin

Spring creates topics at startup from NewTopic beans via KafkaAdmin; TopicBuilder keeps the declarations short. If a topic already exists the bean is ignored, except that KafkaAdmin increases the partition count when the existing topic has fewer partitions than declared. It does not change existing topic configuration unless spring.kafka.admin.modify-topic-configs is enabled.

KafkaTopicConfig.javajava
@Configuration
public class KafkaTopicConfig {

    @Bean
    public NewTopic orderCreatedTopic() {
        return TopicBuilder.name("orders.order.created")
            .partitions(6)
            .replicas(3)
            .config(TopicConfig.RETENTION_MS_CONFIG, "604800000")     // 7 days
            .config(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "2")
            .build();
    }

    @Bean
    public NewTopic orderDeadLetterTopic() {
        return TopicBuilder.name("orders.order.created-dlt")
            .partitions(6)
            .replicas(3)
            .config(TopicConfig.RETENTION_MS_CONFIG, "2592000000")   // 30 days
            .build();
    }

    @Bean
    public KafkaAdmin.NewTopics allTopics() {
        return new KafkaAdmin.NewTopics(
            TopicBuilder.name("orders.order.updated").partitions(6).replicas(3).build(),
            TopicBuilder.name("orders.order.cancelled").partitions(6).replicas(3).build()
        );
    }
}

Sources

Checked against Spring for Apache Kafka 4.1.1, Spring Boot 4.1.1 and Kafka 4.2 documentation on 11 October 2026. Draft: not yet reviewed by a Kafka specialist, and releases move on, so confirm against the current documentation before you act on it.