Explain the purpose of Kafka's transactional API.
Instruction: Provide a brief explanation of why and how Kafka's transactional API is used.
Updated
Official answer
Kafka's transactional API can commit records written to multiple Kafka partitions and the corresponding consumer-group offsets as one transaction. It supports a consume-transform-produce workflow whose committed Kafka results appear once despite retries.
Configure a stable transactional.id for a logical producer identity, unique among concurrently active producers. Initialize transactions, begin a transaction, produce output records, and send the next input offsets with the consumer's current group metadata using sendOffsetsToTransaction. Then commit the transaction. Disable consumer automatic commits and do not separately commit those offsets outside the transaction. A replacement producer with the same transactional identity can fence an obsolete producer.
On an abort, outputs are excluded from consumers using isolation.level=read_committed, and input offsets are not advanced by that transaction. Such consumers also wait behind unresolved transactions as required by the last stable offset. Aborted records may still exist physically in the log; the API does not promise that they were never written.
Handle fatal, abortable and uncertain timeout outcomes according to the client's documented error contract. Do not blindly catch every exception and declare success. Downstream consumers must use read_committed for the intended visibility guarantee.
This atomic boundary covers Kafka records and Kafka offsets. A payment, HTTP request or arbitrary external database write is not rolled back by aborting a Kafka transaction. Use a compatible transactional integration or an idempotent/reconcilable external workflow. Processing code may run again even when its final committed Kafka result appears once.