question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transactional producer

See original GitHub issue

The transactional producer allows an application to send messages to multiple partitions (and topics) atomically. Each transactional producer has a unique transactionalId, which is used to enable transaction recovery across multiple sessions.

There can be only one open transaction per instance of the producer.

Proposed API:

const producer = kafka.producer({ transactional: true })

const run = async () => {
  await producer.connect()
  const tx = await producer.tx()

  try {
    for (let i = 0; i < 100; i++) {
      await tx.add({
        topic,
        messages: [{ key: `key-${i}`, value: `value-${i}` }]
      })
    }
 
    await tx.commit()
  } catch (e) {
    // Investigate class of errors where the producer can't recover, i.e: ProducerFencedException
    await tx.abort()
  }

  await producer.disconnect()
}

Workflow:

await producer.connect():

  • Should call initProducerId to initialize the producer, since it can only happen once per instance it could be combined with connect
  • Add a new log line to indicate the use of a transactional producer

const tx = await producer.tx():

I still have mixed feeling about this design, but the idea is to create a transaction object where the user will be able to add records, commit, and abort.

  • It should check for other producers with the same ID
  • In memory flag to indicate that a transaction was created. There can be only one open transaction per instance of the producer.

await tx.add({ ... }):

Adds the records to some record accumulator

await tx.commit():

  • Calls AddPartitionsToTxn
  • Calls EndTxnRequest with COMMIT

await tx.abort():

  • Clear the in-memory accumulator
  • Calls EndTxnRequest with ABORT

We have to investigate the errors that cause the consumer to disconnect instead of abort. From the Java client, we have: ProducerFencedException, OutOfOrderSequenceException, and AuthorizationException where the authorization error is specific to the current transactionalId

await producer.disconnect():

Should close the connection as usual

Open questions: Do we need guidelines for the transactionalId?

Should we auto-generate the transactionId if it’s not defined?

NOTE:

The API design is still a draft

Depends on #165 #166 #167 #170

@Nevon @ianwsperber ^

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ianwsperbercommented, Nov 8, 2018

@tulios Created #200 so we can separately track the idempotent producer work

1reaction
tulioscommented, Nov 8, 2018

I’ve checked other libraries, and everybody is following this pattern. Let’s follow suit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transactions in Apache Kafka | Confluent
A: the producer and transaction coordinator interaction · The initTransactions API registers a transactional.id with the coordinator. · When the ...
Read more >
Kafka Transactional Producer & Consumer - Stack Overflow
Kafka generates offset for each message. Say, I am producing messages 5 and the offsets will be from 1 to 5. But, In...
Read more >
Transactional Producer · The Internals of Apache Kafka
Transactional Producer. Transactional Producer is a Kafka Producer that uses a TransactionManager for…​FIXME. results matching "". No results matching ""
Read more >
Exactly Once Processing in Kafka with Java - Baeldung
This registers the producer with the broker as one that can use transactions, identifying it by its transactional.id and a sequence number, or ......
Read more >
Kafka Transactional Producer - Progress Documentation
The transactional producer is an additional set of APIs that extend the existing message producer class and provide for transactional ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found