Consumer poll loop in Kafka - Time & Space Complexity
When using Kafka, the consumer poll loop is how your program reads messages continuously.
We want to know how the time to process messages grows as more messages arrive.
Analyze the time complexity of the following code snippet.
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record);
}
}
This code keeps polling Kafka for new messages and processes each message one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that processes each message in the batch.
- How many times: Once for every message received in each poll call.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | Processes 10 messages |
| 100 messages | Processes 100 messages |
| 1000 messages | Processes 1000 messages |
Pattern observation: The time grows directly with the number of messages to process.
Time Complexity: O(n)
This means the time to process messages grows linearly with how many messages arrive.
[X] Wrong: "The poll loop always takes the same time no matter how many messages come in."
[OK] Correct: Actually, the more messages received in each poll, the longer the processing takes because each message is handled one by one.
Understanding how your consumer handles messages helps you explain how your program scales with load, a key skill in real projects.
"What if we processed messages in parallel inside the poll loop? How would the time complexity change?"