0
0
Kafkadevops~20 mins

Message key and value in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Key-Value 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 producer code snippet?

Consider this Kafka producer code in Java that sends a message with a key and value. What will be printed by the callback?

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("topic1", "key1", "value1");
producer.send(record, (metadata, exception) -> {
    if (exception == null) {
        System.out.println("Sent to partition: " + metadata.partition() + ", offset: " + metadata.offset());
    } else {
        System.out.println("Error sending message");
    }
});
ACompilation error due to missing key
BSent to partition: 0, offset: 0
CSent to partition: null, offset: null
DError sending message
Attempts:
2 left
💡 Hint

The key determines the partition. If the topic has one partition, the message goes there with offset 0.

🧠 Conceptual
intermediate
1:30remaining
What is the role of the message key in Kafka?

In Kafka, what does the message key control?

AIt determines the partition to which the message is sent
BIt is used as the message payload
CIt controls the message timestamp
DIt encrypts the message value
Attempts:
2 left
💡 Hint

Think about how Kafka distributes messages across partitions.

Predict Output
advanced
1:30remaining
What happens if you send a Kafka message with a null key?

Given this Kafka producer code snippet, what will be the effect on partitioning?

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("topic1", null, "value1");
producer.send(record);
AMessage is sent to a random partition
BMessage is rejected with NullPointerException
CMessage is sent to partition 0 always
DMessage is sent to all partitions
Attempts:
2 left
💡 Hint

What does Kafka do when the key is missing?

🔧 Debug
advanced
2:00remaining
Why does this Kafka consumer fail to read messages by key?

A Kafka consumer tries to filter messages by key but gets no results. What is the likely cause?

Kafka
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
    if (record.key() != null && record.key().equals("key1")) {
        System.out.println(record.value());
    }
}
AMessage value is empty string
BConsumer is subscribed to wrong topic
Crecord.key() is null causing NullPointerException
DConsumer group id is missing
Attempts:
2 left
💡 Hint

Check if all messages have keys before calling equals.

🚀 Application
expert
2:30remaining
How to ensure messages with the same key are processed in order?

You want to guarantee that all messages with the same key are processed in the order they were sent. Which Kafka feature helps achieve this?

ASetting message timestamp to current time
BEncrypting messages with the same key
CUsing multiple consumer groups for parallel processing
DUsing the same partition for messages with the same key
Attempts:
2 left
💡 Hint

Think about how Kafka guarantees order within partitions.