Muqthar Ali
Muqthar Ali

Reputation: 93

Spring Kafka with Spring JPA

I have two micro services A and B.

A service is sending message to Kafka topic "A-Topic". B service is consuming the message.

In the B service, kafka listener will do the below steps

             1. Persist the data in the database     (repo.save(entity))
             2. Publish the response message to "B-Topic".   (kafkatemplte.send("B-Topic",message))

I am using the Spring @Transactional annotation at the service level in both services.

In the success scenario, data is getting persisted and success message is published to the topic only once

where as

in the Failure scenario, database save was failed due to integrity constraint violation issue. In this case, failure message is published to the Kafka 10 times continuously.

If I remove Transactional annotation from the service class then the message is published only once in failure scenario also.

I don't understand, how come transactional annotation is causing the message to be published 10 times to kafka.

Please let me know your inputs.

Thanks in advance.

Upvotes: 0

Views: 793

Answers (1)

Gary Russell
Gary Russell

Reputation: 174739

The default error handler will attempt delivery 10 times; you need to enable Kafka transactions in the listener container so the kafka sends will be rolled back (and the consumer on B-Topic needs isolation.level=read_committed).

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

Upvotes: 2

Related Questions