Partition key and routing in Kafka - Time & Space Complexity
When sending messages in Kafka, the partition key decides where each message goes.
We want to understand how the time to route messages grows as we send more messages.
Analyze the time complexity of the following code snippet.
producer.send(new ProducerRecord(topic, key, value));
// Kafka uses the key to find the partition
// It hashes the key to pick a partition
// Then routes the message to that partition
This code sends one message with a key, which Kafka hashes to decide the partition for routing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Hashing the partition key to find the partition.
- How many times: Once per message sent.
Each message requires hashing its key once to find the partition.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 hash operations |
| 100 | 100 hash operations |
| 1000 | 1000 hash operations |
Pattern observation: The number of operations grows directly with the number of messages.
Time Complexity: O(n)
This means the time to route messages grows linearly with how many messages you send.
[X] Wrong: "Routing a message takes the same time no matter how many messages are sent."
[OK] Correct: Each message requires hashing its key, so more messages mean more hashing operations, increasing total time.
Understanding how partition keys affect routing time helps you explain message distribution and performance in Kafka systems.
"What if we removed the partition key and let Kafka assign partitions randomly? How would the time complexity change?"