Amazon MSK in Kafka - Time & Space Complexity
When working with Amazon MSK, it's important to understand how the time to process messages grows as the number of messages increases.
We want to know how the system handles more data and how that affects processing time.
Analyze the time complexity of the following Kafka consumer code using Amazon MSK.
consumer.subscribe(Collections.singletonList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record.value());
}
}
This code subscribes to a topic and continuously polls for new messages, processing each one as it arrives.
Look at what repeats in this code:
- Primary operation: Looping over all messages received in each poll.
- How many times: The outer loop runs indefinitely, and the inner loop runs once for each message in the batch.
As the number of messages increases, the time to process them grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 messages |
| 100 | Processes 100 messages |
| 1000 | Processes 1000 messages |
Pattern observation: If you double the number of messages, the processing time roughly doubles too.
Time Complexity: O(n)
This means the time to process messages grows linearly with the number of messages received.
[X] Wrong: "Processing time stays the same no matter how many messages arrive."
[OK] Correct: Each message needs to be handled, so more messages mean more work and more time.
Understanding how message processing scales helps you design systems that handle growth smoothly and shows you can think about real-world performance.
"What if we batch process messages instead of one by one? How would the time complexity change?"