Bird
0
0

Given a Kafka topic with 3 partitions, what happens if you send messages with different keys in this code?

medium📝 Predict Output Q13 of 15
Kafka - Topics and Partitions
Given a Kafka topic with 3 partitions, what happens if you send messages with different keys in this code?
producer.send(new ProducerRecord<>("topic", "key1", "msg1"));
producer.send(new ProducerRecord<>("topic", "key2", "msg2"));
producer.send(new ProducerRecord<>("topic", "key1", "msg3"));

What is true about message order?
AAll messages are ordered in the same partition.
B"msg1" and "msg3" are ordered; "msg2" may be in a different partition.
C"msg2" and "msg3" are ordered; "msg1" is unordered.
DMessages are randomly ordered regardless of keys.
Step-by-Step Solution
Solution:
  1. Step 1: Identify partition assignment by key

    Messages with the same key ("key1") go to the same partition; "key2" goes to another partition.
  2. Step 2: Understand ordering within partitions

    "msg1" and "msg3" share "key1" so they keep order; "msg2" with "key2" is separate.
  3. Final Answer:

    "msg1" and "msg3" are ordered; "msg2" may be in a different partition. -> Option B
  4. Quick Check:

    Same key = same partition order [OK]
Quick Trick: Same key means same partition and order [OK]
Common Mistakes:
  • Assuming all messages go to one partition
  • Thinking different keys keep order together
  • Believing order is global across partitions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes