Write to two Kafka topics in a single transaction using Spring Kafka
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a powerful distributed event streaming platform capable of handling trillions of events a day. Initiating transactions across multiple Kafka topics ensures that either all messages in the transaction are committed or none of them. Spring Kafka abstracts some of the complexity of using Kafka behind a simpler API. In a typical Spring application, you can perform transactions across multiple Kafka topics easily using Spring Kafka’s transactional capabilities.
Fundamental Concepts of Kafka and Transactions
In Kafka, a transaction ensures that messages sent to different topics or partitions either all are committed or none are. This is critical for maintaining data consistency in distributed systems and is supported in Kafka using the concept of exactly-once semantics.
Transactions in Kafka are used to ensure that:
- Messages can span multiple Kafka topics and partitions.
- A group of messages are consumed and produced atomically.
To support transactions, Kafka uses a special transactional.id which uniquely identifies the transaction across the cluster. This ensures that retries due to network errors or other issues do not result in duplicated transactions.
Setup and Configuration in Spring Kafka
Setting up a transactional producer in Spring Kafka involves several key configurations and annotations. Below are the essential steps and configurations required:
- Enable Kafka Transactions: To enable transactional support, you need to set the
spring.kafka.producer.transaction-id-prefix. This prefix will be used by Spring Kafka to configure each transaction ID uniquely. - KafkaTransactionManager: Spring Kafka provides
KafkaTransactionManagerwhich manages Kafka transactions. It binds a producer to the thread running the@Transactionalmethod. - @EnableKafka: This annotation is added to configuration classes to enable Kafka listeners and transaction management.
Here is an example configuration class in Spring Kafka:
Writing to Multiple Topics
To write to multiple Kafka topics within a single transaction, you use the KafkaTemplate provided by Spring Kafka. Here's how you might do it:
In this method, @Transactional ensures that these operations are part of a Kafka transaction. If any send operation or any other operation in the method throws an exception, the whole transaction will be rolled back, and none of the sends will be committed.
Key Points Summary
| Feature | Description |
| Transaction management | Managed by KafkaTransactionManager. |
| Configuration | Requires setting up transaction-id-prefix and enabling idempotence. |
| KafkaTemplate | Used to send messages to different topics atomically within a transaction. |
| @Transactional | This annotation ensures that method execution is within a transaction boundary. |
| Rollback | On failure, all messages are rolled back to maintain atomicity and consistency. |
Conclusion
Using Spring Kafka to manage transactions across multiple topics enhances the robustness and consistency of distributed systems. Proper configuration and the use of Spring’s @Transactional ensures that your messages are either completely successful or completely rolled back, maintaining the atomic nature of your operations across multiple Kafka topics.
Related reading
- Writing Custom Kafka Serializer
- Writing JUnit tests for Kafka Consumer
- Writing large DataFrame from PySpark to Kafka runs into timeout
- Writing logs to log file as well as kafka
- Writes on Cassandra Network Partitioned Nodes
- Writes to geographically distributed database
- Writing a thread safe modular counter in Java
- Zipkin - Is there any more informtaion about creating spans and traces in Java

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.