Best Practice Springs Kafka Consumer: What kind of message should the consumer receive

Viewed 141

I need to start working with kafka. I have a hard time getting my head around on what should the Consumer receive: From what I understand we can configure the consumer in multiple ways:

example 1:

@KafkaListener(topics = "topic_name)
public void receiveSimpleString(@Payload String message) {
}

example 2:

@KafkaListener(topics = "topic_name)
public void receiveConsumerRecord(@Payload ConsumerRecord<String, String> message) {
}

example 3:

@KafkaListener(topics = "topic_name)
public void receiveObject(@Payload SomeCustomClass message) {
}

example 4:

@KafkaListener(topics = "topic_name)
public void receiveSpringMessage(@Payload org.springframework.messaging.Message<T> message) {
}

Maybe there are even more ways, but those once are what i mostly saw while researching for kafka+spring.

The question now is:

Is there a best practice on what the consumer should receive? Are there pros/cons of the different examples?

1 Answers

There is no golden rule for this and it all depends on your specific use case. Some, including myself, would prefer receiving data via a ConsumerRecord instance. This way you not only have access to the actual payload but also to the whole meta data coming with it.

It might be worth for you to look into Kafka Streams API. This is usually the pattern used when consuming from Kafka and producing to Kafka because of the exactly once delivery.

Related