Apache Kafka Streaming (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do Kafka Transactions work to achieve Exactly-Once Semantics (EOS)?
senior LevelCode ExampleState ManagementSystem DesignDatabases & SQL
In Simple Words:
In a Consume-Transform-Produce loop (read from Topic A, compute result, write to Topic B, commit offset to __consumer_offsets):
If the app crashes after writing to Topic B but before committing the offset, the record is processed twice.
Kafka Transactions (TransactionCoordinator):
- Wraps the outgoing message and the consumer offset commit into an atomic transaction:
java
producer.initTransactions(); producer.beginTransaction(); producer.send(new ProducerRecord("output-topic", result)); producer.sendOffsetsToTransaction(offsets, consumerGroupId); producer.commitTransaction(); - Downstream consumers configured with
isolation.level=read_committedwill buffer uncommitted records and only deliver them to the application once the transaction coordinator writes theCOMMITmarker.