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?"
Practice
partition key in Kafka?Solution
Step 1: Understand partition key role
The partition key is used to decide which partition a message goes to in Kafka.Step 2: Connect key to message ordering
Using the same key ensures related messages stay in the same partition, preserving order.Final Answer:
To ensure related messages go to the same partition -> Option CQuick Check:
Partition key controls message placement = D [OK]
- Thinking partition key encrypts messages
- Confusing partition key with compression
- Believing partition key increases message size
Solution
Step 1: Recall ProducerRecord constructor
The correct constructor order is (topic, key, value) to specify partition key.Step 2: Match options to constructor
producer.send(new ProducerRecord<>(topic, key, value)); matches this order exactly, others mix parameters incorrectly.Final Answer:
producer.send(new ProducerRecord<>(topic, key, value)); -> Option DQuick Check:
Topic, key, value order = B [OK]
- Swapping key and value positions
- Omitting the key parameter
- Placing key before topic incorrectly
Solution
Step 1: Understand partition key hashing
Kafka hashes the partition key to decide the partition number.Step 2: Same key means same hash and partition
Repeated messages with "user123" key will always hash to the same partition.Final Answer:
All messages will go to the same partition -> Option AQuick Check:
Same key -> same partition = C [OK]
- Assuming keys distribute evenly without hashing
- Thinking messages go to partition 0 by default
- Believing messages are randomly assigned despite keys
producer.send(new ProducerRecord<>(topic, null, value));
Solution
Step 1: Check the key parameter
The key is set to null, so Kafka cannot use it for partitioning.Step 2: Understand default partitioning behavior
Without a key, Kafka distributes messages randomly across partitions.Final Answer:
The key is null, so messages are distributed randomly -> Option BQuick Check:
Null key -> random partitions = A [OK]
- Assuming null key sends to partition 0
- Confusing missing topic with null key
- Thinking null value causes partitioning issues
Solution
Step 1: Identify the problem with null or empty keys
Null or empty keys cause Kafka to distribute messages randomly, breaking order.Step 2: Use a default fixed key for missing IDs
Assigning a fixed key for missing IDs groups those messages in one partition, preserving order for them.Step 3: Keep valid customer IDs as keys
Valid IDs remain keys to ensure their messages go to the same partition.Final Answer:
Use the customer ID as key only if it is non-empty; otherwise, assign a default fixed key -> Option AQuick Check:
Non-empty key or default fixed key = A [OK]
- Sending null keys causing random partitioning
- Using random UUIDs breaking message grouping
- Ignoring keys and losing message order
Ready to prove it?
Pass the test and this pattern moves unknown
