Message broker architecture in Kafka - Time & Space Complexity
When working with Kafka's message broker architecture, it's important to understand how the time to process messages grows as more messages flow through the system.
We want to know: how does the work increase when the number of messages or partitions grows?
Analyze the time complexity of the following Kafka message consumption loop.
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 Kafka and processes each message one by one.
Look at what repeats as messages come in.
- Primary operation: Loop over all messages received in each poll.
- How many times: Once per message batch, and inside that, once per message.
As the number of messages increases, the processing time grows roughly in direct proportion.
| Input Size (messages) | Approx. Operations |
|---|---|
| 10 | 10 process calls |
| 100 | 100 process calls |
| 1000 | 1000 process calls |
Pattern observation: The work grows linearly as more messages arrive.
Time Complexity: O(n)
This means the time to process messages grows directly with the number of messages received.
[X] Wrong: "Processing time stays the same no matter how many messages come in."
[OK] Correct: Each message needs its own processing step, so more messages mean more work.
Understanding how message processing scales helps you explain system behavior clearly and shows you grasp real-world data flow challenges.
"What if we added multiple consumer threads to process messages in parallel? How would the time complexity change?"