javaapache-kafkaarchitecturemicroservicesspring-kafka

In which place is it better to create kafka topics programmatically?


I'm creating java microservice application on Spring Cloud + Kafka, and during the process of development I asked myself a question if I'm creating kafka topics correctly. I was doing this like this:

    @Bean
    public NewTopic employee() {
        return new NewTopic("email", 5, (short) 1);
    }

So the problem is that in every microservice project I am creating different topics, I don't have a centralized place for this, is it a good practice? I was thinking about creating a special microservice for creating kafka topics only, but I'm not sure about this, because I don't think that this microservice will have any use other then creating topics in @Configuration class. So my question is: which is the right place for creating topics? Is it good to create them like in the above example or maybe I should create them other way? Should the place of creation be centralized (like special microservice) or is it better to create topics in different microservices due to the domain requirements?


Solution

  • So the problem is that in every microservice project I am creating different topics, I dont have a centralized place for this, is it a good practice?

    Generally, good approach is to centralize Kafka topic creation in a dedicated microservice or an infrastructure management instead of creating them in each microservice. It can simplifies management and avoids potential possible conflicts.

    However, if your Kafka topics are tightly coupled to specific microservices and their domains, it may be more appropriate to define them locally.