0
0
Kafkadevops~20 mins

Transactional producer in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Transaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka transactional producer code snippet?

Consider the following Java Kafka producer code using transactions. What will be printed to the console?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");
props.put("transactional.id", "txn-1");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.initTransactions();

try {
    producer.beginTransaction();
    producer.send(new ProducerRecord<>("my-topic", "key1", "value1"));
    producer.commitTransaction();
    System.out.println("Transaction committed");
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
    producer.close();
    System.out.println("Fatal error, producer closed");
} catch (KafkaException e) {
    producer.abortTransaction();
    System.out.println("Transaction aborted");
}
ATransaction committed
BTransaction aborted
CFatal error, producer closed
DNo output, program hangs
Attempts:
2 left
💡 Hint

Think about what happens when no exceptions are thrown during the transaction.

Predict Output
intermediate
2:00remaining
What error does this Kafka transactional producer code raise?

Examine this Kafka producer code snippet. What error will it raise when run?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// Missing transactional.id property

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.initTransactions();
ANo error, runs fine
BIllegalStateException
CInvalidConfigurationException
DTimeoutException
Attempts:
2 left
💡 Hint

Transactional producers require a specific property to be set before calling initTransactions().

🔧 Debug
advanced
2:00remaining
Why does this Kafka transactional producer code abort the transaction?

Look at this code snippet. Why does it print "Transaction aborted"?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");
props.put("transactional.id", "txn-2");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.initTransactions();

try {
    producer.beginTransaction();
    producer.send(new ProducerRecord<>("my-topic", "key2", "value2"));
    throw new KafkaException("Simulated failure");
    // producer.commitTransaction();
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
    producer.close();
    System.out.println("Fatal error, producer closed");
} catch (KafkaException e) {
    producer.abortTransaction();
    System.out.println("Transaction aborted");
}
ABecause a KafkaException was thrown before commitTransaction()
BBecause the producer was closed before commitTransaction()
CBecause the transactional.id was invalid
DBecause enable.idempotence was set to false
Attempts:
2 left
💡 Hint

Check what happens when an exception occurs after beginTransaction() but before commitTransaction().

📝 Syntax
advanced
2:00remaining
Which option contains the correct syntax to begin and commit a Kafka transaction?

Choose the code snippet that correctly begins and commits a Kafka transaction.

A
producer.beginTransaction();
producer.send(record)
producer.commitTransaction
B
producer.beginTransaction;
producer.send(record);
producer.commitTransaction();
C
producer.beginTransaction()
producer.send(record)
producer.commitTransaction()
D
producer.beginTransaction();
producer.send(record);
producer.commitTransaction();
Attempts:
2 left
💡 Hint

Remember Java method calls require parentheses and semicolons.

🚀 Application
expert
3:00remaining
How many messages are guaranteed to be written atomically in this transactional producer code?

Given the following code, how many messages will be committed atomically to Kafka?

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("enable.idempotence", "true");
props.put("transactional.id", "txn-3");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.initTransactions();

producer.beginTransaction();
producer.send(new ProducerRecord<>("topic1", "key1", "value1"));
producer.send(new ProducerRecord<>("topic2", "key2", "value2"));
producer.send(new ProducerRecord<>("topic3", "key3", "value3"));
producer.commitTransaction();
A0 messages
B1 message
C3 messages
DDepends on the number of partitions
Attempts:
2 left
💡 Hint

Think about what commitTransaction() does with all messages sent after beginTransaction().