spring-kafkaspring-mongo

Abort mongo transaction on kafka write failure


I have an example Spring Boot application saving a document to MongoDB using Spring's MongoRepository. After that, I write a message to a Kafka topic.

What I want is to rollback the MongoDB transaction when the Kafka write fails.

Do I have other options than XA transactions?


Solution

  • Kafka does not support XA transactions.

    You can use a ChainedKafkaTransactionManager with the mongo tm and a KafkaTransactionManager (in that order); if the Kafka transaction commit fails, the mongo transaction will roll back.

    See Transactions and the Chained Transaction Manager.