Search code examples
node.jskafkajs

Is it OK to create topics that already exist in KafkaJS?


I am dynamically creating a series of topics using KafkaJS, and found that in order to do so without creating election problems I should use the createTopics admin feature.

The issue is that if createTopics is called on a topic that already exist, it does not just return false (as is documented), but it also emits an error stating "Topic with this name already exists".

{"level":"ERROR","timestamp":"2020-08-24T18:19:48.465Z","logger":"kafkajs","message":"[Connection] Response CreateTopics(key: 19, version: 2)","broker":"localhost:9092","clientId":"tv-kitchen","error":"Topic with this name already exists","correlationId":2,"size":86}

I realize this error comes straight out of the Kafka protocol but I'm concerned because, well, errors are errors.

Is it safe for me to run createTopics even if it might risk creating a topic that already exists, or do I need to do some type of error handling as well?

If it is safe, is it possible to silence that error, since it is ultimately noise?


Solution

  • but it also emits an error stating "Topic with this name already exists".

    This is a log message.

    From the point of view of KafkaJS it logs the response from Kafka, which rightfully returns an (error) code indicating that the topic exists.

    KafkaJS then translates the response into the documented false value returned from the createTopics call.

    Is it safe for me to run createTopics even if it might risk creating a topic that already exists, or do I need to do some type of error handling as well?

    It is safe for you to attempt to create a topic that already exists, and it is your responsibility in the application to handle the case of getting a false result from createTopics -- probably by doing exactly nothing :)

    If it is safe, is it possible to silence that error, since it is ultimately noise?

    You can configure logging for KafkaJS, so yes, you could not log this message on your application side.