How can I create many kafka topics during spring-boot application start up?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed event streaming platform that can handle trillions of events a day. It’s a core component in many scalable applications for handling real-time data streams. In Kotlin-based Spring Boot applications, automating the creation of Kafka topics during startup can make deployments smoother and ensure consistency across different environments. Here’s how you can do this effectively:
Configuring Kafka in Spring Boot
To begin with, you need to add dependencies for Kafka and Spring Kafka to your Spring Boot project. You can do this by adding the following to your build.gradle or pom.xml file.
Gradle:
Maven:
Creating Kafka Topics on Startup
Spring Boot provides several ways to execute code on startup. One effective method is to implement an ApplicationRunner or CommandLineRunner, which are interfaces providing the run method that will be executed after the application context is loaded.
Approach 1: Using AdminClient
Kafka’s AdminClient API allows you to manage and inspect topics, brokers, and other Kafka objects. Here's how you can use it to create topics:
- Create a bean for
AdminClient:First, configure theAdminClientbean in your Spring configuration.
- Use
ApplicationRunnerto create topics:ImplementApplicationRunnerto create topics during startup.
In this example, NewTopic is used to define the specifics of the topics. Adjust the number of partitions and the replication factor based on your needs.
Topic Creation Best Practices
When creating topics during application start-up, consider the following best practices:
- Idempotence: Ensure that topic creation is idempotent. It shouldn't fail if the topic already exists with the same settings.
- Configuration Externalization: Externalize topic names, partition counts, and replication factors into configuration files like
application.properties. - Error Handling: Add error handling when creating topics. Log useful information if the creation fails, possibly due to misconfiguration or Kafka broker issues.
Summary Table
Here is a summary table detailing the properties and configurations for topic creation:
| Property | Description | Example Value |
| Topic Name | Name of the Kafka topic | "exampleTopic" |
| Partitions | Number of partitions in the topic | 10 |
| Replication Factor | Number of topic replicas across brokers | 1 |
| Configuration Source | Source of configuration parameters | application.properties |
Conclusion
Setting up Kafka topics programmatically in a Spring Boot application provides control and consistency over your messaging environment. By leveraging Spring Boot’s auto-configuration and Kafka’s AdminClient, you can ensure that your Kafka topics are always configured correctly based on your application’s requirements.
Related reading
- How can I delete a RabbitMq exchange?
- How can I delete a topic from DCOS Kafka?
- How can I disable spring cloud stream for development purpose when there are not kafka broker running?
- How can I find kafka config file?
- How can I create multiple servers with Java RMI?
- How can I determine what core a Java thread is running on?
- How can I force a RabbitMQ broker to NACK a message for testing?
- How can i get all group list with kafka0.10.x

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.