message consistency test scenario in Kafka
See the Spring for Apache Kafka documentation about transactions.
If you are new to Spring; also see Spring Framework's Transaction Support which Spring Kafka leverages.
Kafka cannot paritipate in a JTA (XA) transactions so your only option is to consider Best Efforts 1PC commits via Spring transaction synchronization and deal with the (small) possibility of redeliveries of messages that have previously been processed (after a failure).