Java consumer client in Kafka - Time & Space Complexity
When using a Java consumer client in Kafka, it's important to understand how the time to process messages grows as the number of messages increases.
We want to know how the client's work changes when more messages arrive.
Analyze the time complexity of the following Java Kafka consumer code snippet.
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record);
}
}
This code continuously polls messages from a Kafka topic and processes each message one by one.
Look at what repeats in this code:
- Primary operation: Looping over all messages returned by each poll call.
- How many times: Once for each message in the batch returned by poll.
As the number of messages received in each poll grows, the processing work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | Processes 10 messages |
| 100 messages | Processes 100 messages |
| 1000 messages | Processes 1000 messages |
Pattern observation: The work grows directly with the number of messages received each time.
Time Complexity: O(n)
This means the time to process messages grows linearly with the number of messages received.
[X] Wrong: "The poll method itself takes constant time no matter how many messages arrive."
[OK] Correct: Poll returns a batch of messages, so processing all messages inside the batch takes time proportional to the batch size.
Understanding how message processing time grows helps you design efficient consumers and shows you can reason about real-world streaming data systems.
"What if we changed the processing to handle messages in parallel? How would the time complexity change?"