Introducing a new way to review your PRs.

Read the announcement

aio-libs / aiokafka

asyncio client for kafka

View on GitHub

19 documents · Updated Nov 23, 2026

docs / producer-transactions

Idempotent and transactional production

Idempotent and transactional production lets the producer retry record delivery without creating duplicate records and lets an application publish records as one committed unit. The producer uses stable delivery identity for retries. A transaction adds a boundary so output records and, when needed, input offsets become visible together or are discarded together.

The wider producer model is described in Producer subsystem, and the normal record path is described in Producer delivery pipeline.

Idempotent production

Idempotent production uses producer identity and per-partition sequence numbers to make retries safe. The broker can recognize a retry of a batch that it already accepted. An invalid identity or sequence stops unsafe delivery instead of silently creating uncertain data.

Idempotence starts when enable_idempotence=True or transactional_id is set. It requires acks=all; the producer selects that mode when the caller does not set it and rejects an incompatible value. A producer with only idempotence can send ordinary records. A producer with a transactional ID must send records from an active transaction.

References: aiokafka/…/producer.py• L253-283 aiokafka/…/producer.py• L491-510 aiokafka/…/producer.py• L562-573

Transaction manager lifecycle

The transaction manager records the state of one transactional producer. A producer first initializes its broker identity. It then enters a transaction, ends it by commit or abort, and returns to a ready state. An abortable error requires the application to abort. A fatal error means that the producer cannot safely continue.

The main lifecycle is:

flowchart LR
    U[UNINITIALIZED] --> R[READY]
    R --> I[IN_TRANSACTION]
    I --> C[COMMITTING_TRANSACTION]
    I --> A[ABORTING_TRANSACTION]
    I --> E[ABORTABLE_ERROR]
    E --> A
    C --> R
    A --> R
    I --> F[FATAL_ERROR]
    C --> F
    A --> F

begin_transaction() waits for producer identity initialization before entering IN_TRANSACTION. Commit and abort move the manager to an ending state and wait for the sender to finish the broker work. These waits are shielded from task cancellation so a cancelled caller does not cancel the transaction-end operation itself.

References: aiokafka/…/transaction_manager.py• L30-55 aiokafka/…/transaction_manager.py• L112-168 aiokafka/…/producer.py• L588-612

Transaction boundaries

The producer admits transactional records only while the manager is in IN_TRANSACTION. A send outside that state raises IllegalOperation, and a new transaction is required after a successful commit or abort. This boundary prevents records from a later unit of work from entering an earlier transaction.

The first record for a partition brings that partition into the transaction. The application can also add consumer offsets for a group. This supports a consume-transform-produce flow in which output and input progress share one commit decision.

References: aiokafka/…/transaction_manager.py• L170-204 aiokafka/…/producer.py• L491-510 aiokafka/…/producer.py• L562-573 aiokafka/…/producer.py• L619-637

Commit coordination

Commit closes the current delivery scope before it makes the transaction result visible. The producer waits for records already in the transaction to complete, then ends the transaction with a commit result. It also commits any offsets attached to the transaction before ending it. An empty transaction completes without broker data to commit.

The delivery and transaction systems coordinate in this order:

sequenceDiagram
    participant A as Application
    participant P as Producer
    participant B as Broker
    A->>P: Begin transaction
    A->>P: Send output records
    P->>B: Deliver records with producer identity
    A->>P: Add input offsets
    P->>B: Register output partitions and group
    P->>B: Commit offsets and end transaction
    B-->>P: Transaction result
    P-->>A: Commit completes

The background sender serializes transaction-control work with record delivery. It registers a partition before sending data for that partition, then waits for the transaction-end result. See Producer delivery pipeline for the broader delivery flow.

References: aiokafka/…/transaction_manager.py• L189-237 aiokafka/…/producer.py• L592-604 aiokafka/…/producer.py• L619-637

Abort coordination

Abort ends the active transaction without making its records or transactional offsets visible as committed. The transaction context uses this path when the block raises. It then lets the original application error continue to the caller.

An abortable producer error clears pending transaction work and requires an abort before normal use can resume. A fatal error is different: it can fail pending work and prevent the normal abort path because the producer identity or sender is no longer safe to use.

References: aiokafka/…/transaction_manager.py• L130-168 aiokafka/…/producer.py• L602-612 aiokafka/…/producer.py• L655-662

Transaction context

The transaction context is the normal application boundary for one unit of work. Entering the context begins a transaction. A successful block commits it. An exception aborts it unless the producer is already in a fatal state.

async with producer.transaction():
    await producer.send_and_wait("output", value)
    await producer.send_offsets_to_transaction(offsets, group_id)

The context requires a configured transactional_id. A producer without one can use idempotent delivery but cannot begin, commit, abort, or attach offsets to a transaction.

References: aiokafka/…/producer.py• L582-637 aiokafka/…/producer.py• L647-663

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the transactional_id and idempotence checks aligned with the state rules for both single-record and batch sends.
  • Preserve the order between record delivery, offset registration, offset commit, and transaction end.
  • Keep commit waits limited to records in the current transaction so later records cannot cross the transaction boundary.
  • Keep abortable errors distinct from fatal errors because only the former supports normal recovery by aborting.
  • Keep cancellation shielding on transaction-end waits so caller cancellation cannot leave broker-side work without its local completion path.

Important failure modes

TriggerConsequenceGuard
A transactional producer sends outside an active transactionThe send is rejected and no record is queuedBegin a new transaction before each group of transactional sends
Idempotence uses an acknowledgment mode other than allProducer construction raises ValueErrorUse acks=all or leave the setting unset
A transaction block raisesThe transaction aborts and its offsets do not advanceRetry the input in a new transaction when the application can recover
Another producer uses the same transactional IDThe existing producer is fenced and its operation failsGive each active producer a unique transactional ID
The sender or producer enters a fatal statePending delivery and transaction waiters fail; normal transaction use cannot continueStop and recreate the producer

Key files

FileWhy to read it
aiokafka/…/producer.pyDefines public idempotence rules, transaction boundaries, offset attachment, and context behavior.
aiokafka/…/transaction_manager.pyDefines transaction states, producer identity, sequence tracking, and pending transaction work.
aiokafka/…/sender.pyCoordinates initialization, record delivery, transaction control, retries, and broker errors.
aiokafka/…/message_accumulator.pyConnects batches to transaction scope and delivery completion.
tests/test_transactional_producer.pyShows the supported lifecycle and recovery behavior.