1. 5 APIs for transactions
// 1初始化事务
void initTransactions();
// 2开启事务
void beginTransaction() throws ProducerFencedException;
// 3在事务内提交已经消费的偏移量(主要用于消费者)
void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets,
String consumerGroupId) throws ProducerFencedException;
// 4提交事务
void commitTransaction() throws ProducerFencedException;
// 5放弃事务(类似于回滚事务的操作)
void abortTransaction() throws ProducerFencedException;
2. The relationship between idempotency and transactionality
The prerequisite for the implementation of transaction attributes is idempotence, that is, when configuring the transaction attribute transaction id, idempotency must also be configured; however, idempotency can be used independently without relying on transaction attributes.
- Idempotence introduces the Porducer ID
- The transaction attribute introduces the Transaction Id attribute.
scenes to be used
- enable.idempotence = true, transactional.id is not set: only idempotence is supported.
- enable.idempotence = true, transactional.id setting: support transaction attributes and idempotence
- enable.idempotence = false, transactional.id not set: kafka without transactional attributes and idempotence
- enable.idempotence = false, transactional.id setting: the PID cannot be obtained, and an error will be reported at this time
3. Reference
Kafka idempotent producer and transaction_51CTO blog_kafka transaction