Message key and value in Kafka - Time & Space Complexity
When working with Kafka messages, it's important to understand how processing time changes as the number of messages grows.
We want to see how the time to handle message keys and values changes when more messages are sent or received.
Analyze the time complexity of the following Kafka message processing code.
for (ConsumerRecord record : consumer.poll(Duration.ofMillis(100))) {
String key = record.key();
String value = record.value();
process(key, value);
}
This code loops through all messages received in a poll, extracts each message's key and value, and processes them.
Look at what repeats as the input grows.
- Primary operation: Looping through each message in the batch.
- How many times: Once for every message received in the poll.
As the number of messages increases, the time to process them grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times processing key and value |
| 100 | 100 times processing key and value |
| 1000 | 1000 times processing key and value |
Pattern observation: The work grows directly with the number of messages; double the messages, double the work.
Time Complexity: O(n)
This means the processing time grows in a straight line with the number of messages.
[X] Wrong: "Processing the key and value is constant time no matter how many messages there are."
[OK] Correct: Each message adds work because you handle its key and value separately, so more messages mean more total work.
Understanding how message processing time grows helps you explain system behavior clearly and shows you can think about efficiency in real applications.
"What if we batch process messages in groups instead of one by one? How would the time complexity change?"