Bird
Raised Fist0

Examine the following Kafka producer code snippet using transactions:

medium📝 Debug Q6 of Q15
Kafka - Message Delivery Semantics
Examine the following Kafka producer code snippet using transactions:
producer.initTransactions();
producer.beginTransaction();
producer.send(record);
producer.abortTransaction();
producer.close();
What is the effect of calling abortTransaction() here?
AThe transaction remains open and must be committed or aborted before closing.
BMessages are committed and become visible to consumers immediately.
CAll messages sent in the transaction are discarded and not visible to consumers.
DThe producer throws an exception and stops sending messages.
Step-by-Step Solution
Solution:
  1. Step 1: Understand abortTransaction()

    Calling abortTransaction() discards all messages sent in the current transaction, so they are not committed.
  2. Step 2: Analyze code flow

    The code calls abortTransaction() after sending a record, so the message is discarded and won't be visible to consumers.
  3. Step 3: Evaluate other options

    Messages are committed and become visible to consumers immediately. is incorrect because aborting does not commit messages. The transaction remains open and must be committed or aborted before closing. is wrong since abortTransaction closes the transaction. The producer throws an exception and stops sending messages. is incorrect as no exception is thrown by abortTransaction itself.
  4. Final Answer:

    All messages sent in the transaction are discarded and not visible to consumers. -> Option C
  5. Quick Check:

    abortTransaction discards messages [OK]
Quick Trick: abortTransaction discards all transactional messages [OK]
Common Mistakes:
MISTAKES
  • Assuming abortTransaction commits messages
  • Thinking abortTransaction leaves transaction open
  • Believing abortTransaction causes exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes