Bird
Raised Fist0
Kafkadevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary role of the message key in Kafka?
easy
A. To decide which partition the message goes to
B. To store the actual data of the message
C. To encrypt the message content
D. To delete old messages automatically

Solution

  1. Step 1: Understand message key purpose

    The message key in Kafka is used to determine the partition where the message will be stored.
  2. Step 2: Differentiate key and value roles

    The message value holds the actual data, while the key helps with partitioning and ordering.
  3. Final Answer:

    To decide which partition the message goes to -> Option A
  4. Quick Check:

    Message key = Partition decision [OK]
Hint: Key decides partition, value holds data [OK]
Common Mistakes:
  • Confusing key with message value
  • Thinking key encrypts data
  • Assuming key deletes messages
2. Which of the following is the correct way to send a Kafka message with a key and value in Java?
easy
A. producer.send(new ProducerRecord<>("topic", value));
B. producer.send(new ProducerRecord<>(key, value));
C. producer.send("topic", value);
D. producer.send(new ProducerRecord<>("topic", key, value));

Solution

  1. Step 1: Review ProducerRecord constructor

    The correct constructor for sending a message with key and value includes topic, key, and value in that order.
  2. Step 2: Check each option

    producer.send(new ProducerRecord<>("topic", key, value)); uses new ProducerRecord<>("topic", key, value), which is correct syntax. Others miss topic or key.
  3. Final Answer:

    producer.send(new ProducerRecord<>("topic", key, value)); -> Option D
  4. Quick Check:

    ProducerRecord(topic, key, value) = correct [OK]
Hint: ProducerRecord needs topic, key, then value [OK]
Common Mistakes:
  • Omitting the topic parameter
  • Swapping key and value order
  • Using wrong constructor overload
3. Given the following Kafka producer code snippet, what will be the key of the message sent?
ProducerRecord record = new ProducerRecord<>("orders", "12345", "order details");
producer.send(record);
medium
A. "orders"
B. null
C. "12345"
D. "order details"

Solution

  1. Step 1: Identify parameters in ProducerRecord

    The constructor is ProducerRecord(topic, key, value). Here, "orders" is topic, "12345" is key, and "order details" is value.
  2. Step 2: Determine the key value

    The key is the second argument, which is "12345".
  3. Final Answer:

    "12345" -> Option C
  4. Quick Check:

    Second argument = key = "12345" [OK]
Hint: Second argument in ProducerRecord is key [OK]
Common Mistakes:
  • Confusing topic with key
  • Choosing message value as key
  • Assuming key is null if not obvious
4. Identify the error in this Kafka producer code snippet:
ProducerRecord record = new ProducerRecord<>("topic", null, "data");
producer.send(record);
medium
A. Message key cannot be null
B. This code is valid; no error
C. ProducerRecord constructor parameters are in wrong order
D. Message value cannot be null

Solution

  1. Step 1: Check if null key is allowed

    Kafka allows message keys to be null; this means messages can be sent without a key.
  2. Step 2: Verify constructor usage

    The constructor ProducerRecord<>("topic", null, "data") is valid and sends a message with no key.
  3. Final Answer:

    This code is valid; no error -> Option B
  4. Quick Check:

    Null key allowed in Kafka messages [OK]
Hint: Null keys are allowed in Kafka messages [OK]
Common Mistakes:
  • Assuming key must never be null
  • Confusing parameter order
  • Thinking value cannot be null without checking
5. You want to ensure all messages with the same user ID go to the same partition for ordering. Which Kafka message feature should you use and how?
hard
A. Use the user ID as the message key to ensure partitioning by key
B. Include the user ID only in the message value and leave key null
C. Set the user ID as the topic name
D. Send messages without keys and rely on default partitioning

Solution

  1. Step 1: Understand partitioning by key

    Kafka uses the message key to decide the partition. Messages with the same key go to the same partition.
  2. Step 2: Apply to user ID scenario

    Using the user ID as the message key ensures all messages for that user go to the same partition, preserving order.
  3. Final Answer:

    Use the user ID as the message key to ensure partitioning by key -> Option A
  4. Quick Check:

    Key = user ID for partitioning [OK]
Hint: Use key for grouping messages by user ID [OK]
Common Mistakes:
  • Putting user ID only in value loses partition control
  • Changing topic per user is inefficient
  • Relying on default partitioning breaks ordering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard