Transactions

  • Compare a Kafka-to-Kafka atomic result with a database effect that can survive replay.
100%
Application processKafka cluster · broker-owned stateOptional databasesinkKafka output and offsets share one transaction boundaryExternal effectTransaction and group stateRecordsinput 41write outputsubmit next 42commit or abortfinish outputfinish offsetsvisible dataexternal sink optionConsumer +transactional producerInput partitionorders P0Output partitionenriched-ordersTransaction coordinatorTransaction state logGroup offset partitionenrich · orders P0Downstream consumerread_committedBusiness effect+ unique receiptinput 41write outputsubmit next 42commit or abortfinish outputfinish offsetsvisible dataexternal sink option

Select a component to trace its direct connections. Drag to pan · Ctrl + wheel or pinch to zoom.

  • A worker reads order 41. Its transactional producer writes the enriched order and saves 42 as the group’s next input offset in one Kafka transaction.
  • The coordinator records a commit or abort. A read_committed consumer can see the committed result and skips aborted output.
  • For a database sink, a unique receipt identifies the processed input. The database commits the receipt and billing change together so a replay can be recognized.
  • If the worker crashes after sending output and the Kafka transaction aborts, the group can retry 41.
  • If the database commits first and the worker crashes before saving 42, the next worker can encounter the receipt when 41 is delivered again.
Using transactions

Using transactions

Apache Kafka 4.1 · illustrative cluster and offsets

Kafka input and output

  • A worker polls input using group enrich. Its transactional producer appends transformed records to the output topic and calls sendOffsetsToTransaction with the next input offsets and current group metadata.
  • The transaction coordinator manages the producer transaction. Partition leaders receive commit or abort markers. A read_committed downstream reader observes committed output once it is visible.
  • Assign transactional IDs so a replacement can fence its predecessor and simultaneously active workers have distinct IDs. Group rebalances can invalidate ownership. Handle the failure and resume from committed offsets.
Output, offsets and an external effectjson
{
  "input": {"topic":"orders","partition":0,"processedOffset":41,"group":"enrich"},
  "producer": {"transactional.id":"enrich-worker-7","enable.idempotence":true,"acks":"all"},
  "consumer": {"enable.auto.commit":false,"isolation.level":"read_committed"},
  "transaction": ["beginTransaction", "send transformed result to enriched-orders", "sendOffsetsToTransaction: orders/0 next offset 42 with the current consumer group metadata", "commitTransaction"],
  "abortCase": {"outputVisibleToReadCommitted":false,"groupOffsetAdvanced":false,"eligibleForReplay":true},
  "commitCase": {"outputVisibleToReadCommitted":true,"committedNext":42},
  "externalEffect": {"store":"billing database","uniqueKey":["sourceCluster","topic","partition","offset"],"exampleKey":["east","orders",0,41],"contract":"Atomically insert processed identity and apply the billing change in one database transaction; acknowledge input only after durable success."}
}

Crash with input offset 41

Boundary reached Kafka output seen by read_committed readers Input recovery
Output appended, transaction subsequently aborted Aborted records are skipped Resume from prior group commit and process again
Output and next offset 42 committed together Committed result can be fetched Next owner starts at 42
Open transaction stalls Later records can be held behind the last stable offset Check transaction timeout, worker liveness and coordinator state
  • Disable consumer auto-commit for this pattern. The transactional producer must own the Kafka output and the group-offset update. A long transaction can extend visibility delay and increase replay work.

External database alternative

  • For a database sink, write a unique receipt for the consumed source record and apply the business mutation in the same database transaction. Commit the Kafka offset only after durable success.
  • A crash after the database commit can replay offset 41. The receipt suppresses the repeated effect if its scope, retention and uniqueness cover that replay. A commit before the database write can lose work.
  • If the logical business operation can arrive as two input records, an offset-based receipt protects only redelivery of that one record. Use a receiver-enforced business key when wider deduplication is required.
  • A remote provider needs an idempotency or authoritative outcome contract of its own. A Kafka transaction cannot reverse a completed external effect.