Why consumers process messages in Kafka - Performance Analysis
When Kafka consumers process messages, the time it takes depends on how many messages they handle.
We want to understand how the work grows as more messages arrive.
Analyze the time complexity of the following code snippet.
consumer.poll(Duration.ofMillis(100)).forEach(record -> {
process(record);
});
This code polls messages from Kafka and processes each one in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each message received in the poll.
- How many times: Once for every message in the batch.
As the number of messages increases, the processing time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 process calls |
| 100 | 100 process calls |
| 1000 | 1000 process calls |
Pattern observation: The work grows directly with the number of messages.
Time Complexity: O(n)
This means the time to process messages grows in a straight line as more messages come in.
[X] Wrong: "Processing one message takes the same time no matter how many messages there are."
[OK] Correct: Each message adds extra work, so total time grows with the number of messages.
Understanding how message processing time grows helps you explain system behavior clearly and shows you can think about performance in real situations.
"What if the consumer processed messages in parallel instead of one by one? How would the time complexity change?"