Quick revision
The short answer
Setting spring.kafka.producer.transaction-id-prefix makes Spring Boot create a transactional producer factory and a KafkaTransactionManager, and Boot wires that manager into the listener container. The container then starts a Kafka transaction before calling our listener, so the sends to invoices and audit and the consumed offset of the orders record are committed together in one Kafka transaction, or aborted together. Outside a listener we use kafkaTemplate.executeInTransaction or @Transactional with the Kafka transaction manager; downstream consumers must set isolation.level=read_committed, and transactional ids must be stable per producer instance so zombie producers are fenced.
What to remember
In this answer, we enabled Kafka transactions in Spring Boot with transaction-id-prefix, which creates a transactional producer factory and a KafkaTransactionManager used by the listener container. The container begins a transaction before the listener, so sends to invoices and audit and the consumed offset commit or abort together. Outside listeners we use executeInTransaction or @Transactional with the Kafka manager, downstream consumers must use isolation.level=read_committed, and stable transactional ids let Kafka fence zombie producers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Setting spring.kafka.producer.transaction-id-prefix makes Spring Boot create a transactional producer factory and a KafkaTransactionManager, and Boot wires that manager into the listener container. The container then starts a Kafka transaction before calling our listener, so the sends to invoices and audit and the consumed offset of the orders record are committed together in one Kafka transaction, or aborted together. Outside a listener we use kafkaTemplate.executeInTransaction or @Transactional with the Kafka transaction manager; downstream consumers must set isolation.level=read_committed, and transactional ids must be stable per producer instance so zombie producers are fenced.
Why this question is asked
Consume, transform and produce is the most common Kafka pattern, and without transactions a crash in the middle can leave the invoice written but the audit missing, or produce both twice. Kafka transactions solve this inside Kafka, and Spring makes them nearly invisible, which is exactly why interviewers ask how they work: the configuration is two lines, but the guarantees depend on details such as the consumer isolation level. In this answer, we will configure transactions in Spring Boot, write the listener, see what the container does around it, use transactions outside listeners, set up downstream consumers, and look at fencing and what happens when a database joins in.
1A Quick Recap of Kafka Transactions
A Kafka transaction lets one producer write to several partitions, and also commit consumer offsets, as a single atomic unit. The producer registers with a transactional.id, begins a transaction, sends records, adds the consumer's offsets to the transaction, and commits. The transaction coordinator on the broker then writes commit markers to every partition involved. If the producer aborts or dies, abort markers are written instead.
Records of an aborted transaction are still physically in the log. What hides them is the consumer: a consumer with isolation.level=read_committed skips aborted records and only reads up to the last stable offset, the point before which every transaction has finished. That detail matters for downstream services.
2Configuring Spring Boot
Spring Boot needs only the transaction id prefix. When it is set, Boot configures the producer factory as transactional, creates a KafkaTransactionManager bean, and applies it to the auto-configured listener container factory. Idempotence and acks=all are required for transactions and are already the defaults.
3The Listener and What the Container Does Around It
The listener itself looks ordinary. It contains no transaction code.
7 messages in order. Play them, or step through them one at a time.
On an exception the container calls abortTransaction instead, and nothing becomes visible.
Because the container has a KafkaTransactionManager, it begins a Kafka transaction before invoking onOrder. The KafkaTemplate sees the active transaction and sends through the same transactional producer, so both records belong to it. When the method returns, the container sends the consumed offset of the orders record to the transaction, using sendOffsetsToTransaction, and commits. If the method throws, the container aborts the transaction: the invoice and audit records become aborted records, invisible to read_committed consumers, the offset is not committed, and the error handler decides whether to redeliver the order, for example after a back-off, or send it to a dead letter topic.
4Transactions Outside a Listener
When we produce from a REST controller or a scheduled job, there is no container to start the transaction. A transactional KafkaTemplate refuses to send outside a transaction unless configured otherwise, so we start one explicitly. executeInTransaction runs a callback in a local Kafka transaction. Alternatively, @Transactional naming the Kafka transaction manager does the same declaratively.
The bean name kafkaTransactionManager is the one Boot creates; if the application also has a JPA transaction manager, naming the manager explicitly avoids using the wrong one.
5Downstream Consumers Must Read Committed
The guarantee only holds for consumers that respect it. The default isolation level of a Kafka consumer is read_uncommitted, which returns every record in the log, including records of aborted transactions and records of transactions still open. A downstream invoicing service with the default setting would see the invoice from a failed attempt and then the invoice from the successful retry, and bill the customer twice. So every consumer of invoices and audit must set isolation.level=read_committed, in Spring Boot spring.kafka.consumer.isolation-level=read_committed. The cost is a little extra latency, because a read_committed consumer cannot read past an open transaction.
6Stable Transactional Ids and Fencing
Fencing is how Kafka stops a zombie, an old instance that is still running after it was replaced, perhaps paused by a long garbage collection, from committing duplicate results. When a producer initialises with a transactional.id, the coordinator bumps that id's epoch and aborts any open transaction of the previous holder, and writes from the old epoch are rejected with a fencing error. This only works if a replacement instance uses the same transactional.id as the one it replaces, which is why ids must be stable per producer instance rather than random per start.
Spring builds the ids from the prefix plus a suffix it manages, and for container-started transactions it uses the exactly-once mode based on KIP-447, where the broker also fences using the consumer group's generation, so ids no longer have to be tied to individual partitions. In a deployment with several pods, giving each pod its own prefix, for example from the pod name in a StatefulSet, keeps ids unique between pods and stable across restarts; check the recommendations for your Spring Kafka version.
7Follow-up questions
What happens if the listener also writes to a database?
The database write is not part of the Kafka transaction; Kafka transactions only cover Kafka writes and offsets. If we annotate the listener with @Transactional for the JPA manager while the container runs the Kafka transaction, Spring synchronises them: the database commits when the method returns, then the container commits the Kafka transaction. A crash between the two leaves the database committed and the Kafka transaction aborted, so the order is redelivered and the listener runs again, so the database write must be idempotent, for example an upsert keyed by order id. If the business needs the event and the row to be truly consistent, the transactional outbox, writing the event into the database in the same transaction, is the reliable pattern.
Is this exactly-once processing?
Within Kafka, yes: each input record's effects on output topics and its offset commit happen exactly once, as seen by read_committed consumers. Side effects outside Kafka, such as calling an HTTP API or writing to a database, are not covered and can happen more than once.
8Conclusion
In this answer, we enabled Kafka transactions in Spring Boot with transaction-id-prefix, which creates a transactional producer factory and a KafkaTransactionManager used by the listener container. The container begins a transaction before the listener, so sends to invoices and audit and the consumed offset commit or abort together. Outside listeners we use executeInTransaction or @Transactional with the Kafka manager, downstream consumers must use isolation.level=read_committed, and stable transactional ids let Kafka fence zombie producers.
Open with
In Spring Boot I set spring.kafka.producer.transaction-id-prefix. That makes the producer factory transactional and Boot creates a KafkaTransactionManager and puts it on the listener container factory. The listener is a normal @KafkaListener on orders that sends to invoices and audit with the KafkaTemplate.
Then, if they want more
Because the container has the transaction manager, it begins a Kafka transaction before calling the listener, the template's sends join it, and when the method returns the container adds the consumed offset with sendOffsetsToTransaction and commits, so the two output records and the offset are atomic.
If the listener throws, the transaction aborts, the offset is not committed, and the error handler redelivers or dead-letters the record. Outside a listener, I would use kafkaTemplate.executeInTransaction with a callback, or a method annotated @Transactional naming the Kafka transaction manager.
The important part for other teams is that downstream consumers must set isolation.level to read_committed; the default read_uncommitted would show records from aborted attempts, so an invoice could be processed twice. Finally, transactional ids must be stable per producer instance, so when an instance is replaced the new one fences the old zombie by bumping the epoch.
If the listener also writes to a database, that write is not in the Kafka transaction; I would make it idempotent or use an outbox.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show how to enable Kafka transactions in Spring Boot and write a listener that consumes...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/f47edb1d90/