Bird
Raised Fist0

You want to send two messages atomically to two different topics using a Kafka transactional producer. Which sequence of calls is correct?

hard🚀 Application Q15 of Q15
Kafka - Message Delivery Semantics

You want to send two messages atomically to two different topics using a Kafka transactional producer. Which sequence of calls is correct?

// Assume producer is configured with transactional.id
producer.initTransactions();
// What is the correct order?
AbeginTransaction(); send(topic1); send(topic2); commitTransaction();
Bsend(topic1); beginTransaction(); send(topic2); commitTransaction();
CbeginTransaction(); send(topic1); commitTransaction(); send(topic2);
Dsend(topic1); send(topic2); beginTransaction(); commitTransaction();
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction boundaries

    beginTransaction() must be called before sending any messages to start the transaction.
  2. Step 2: Send all messages within the transaction and then commit

    Both messages must be sent after beginTransaction() and before commitTransaction() to be atomic.
  3. Final Answer:

    beginTransaction(); send(topic1); send(topic2); commitTransaction(); -> Option A
  4. Quick Check:

    Start transaction, send all, then commit [OK]
Quick Trick: Begin transaction before sends, commit after all sends [OK]
Common Mistakes:
MISTAKES
  • Sending messages before beginTransaction()
  • Committing before all messages sent
  • Starting transaction after sending messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes