2

My question is in a spring boot microservice using kafka what is appropriate to use KafkaTemplate.send() or KafkaProducer.send()

I have used KafkaConsumer and not KafkaListner to poll the records because KafkaListner was fetching the records as and when they were coming to the topics, I wanted the records to be polled periodically based on business needs.

Have gone through the documentation of KafkaProducer https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html

and Spring KafkaTemplate

https://docs.spring.io/spring-kafka/reference/html/#kafka-template

I am unable to make a decision like what is ideal to use or atleast the reason of using one over the other is unclear?

What my need is I want the operation to be sync i.e. I want to know if the published happened successfully or not because If the record is not delivered I need to retry publishing.

Any help will be appreciated.

RachitSharma
  • 567
  • 4
  • 11
  • 31
  • 1
    spring kafka is juat wrapper library on apache kafka, you can use either one based on your requirement – Ryuzaki L Apr 21 '21 at 08:24
  • 1
    personally, i like the KafkaProducer more than the spring wrapper without adding much complexity. Your requirement can be fulfilled by both using `flush()` (which is available in both clients) – Felix Apr 21 '21 at 08:26

1 Answers1

3

For your first question, which one should I use kafka Template or Kafka producer?

The Kafka Producer is defined in Apache Kafka. The KafkaTemplate is Spring's implementation of it (although it does not implement Producer directly) and so it provides more methods for you to use.

Read this link::

What is the difference between Kafka Template and kafka producer?

For retry mechanism, in case of failure in publishing. I have answered this in another question.

The acks parameter control how many partition replicas must receive the record before the producer can consider the write successful.

There are 3 values for the acks parameter:

acks=0, the producer will not wait for a reply from the broker before assuming the message sent successfully.

acks=1, the producer will receive a successful response from the broker the moment the leader replica received the message. If the message can't be written to the leader, the producer will receive an error response and can retry.

acks=all, the producer will receive a successful response from the broker once all in-sync replicas received the message.

Best way to configure retries in Kaka Producer

Dharman
  • 30,962
  • 25
  • 85
  • 135
Vaibs
  • 1,546
  • 9
  • 31