Subscribing to topics in Kafka - Time & Space Complexity
When subscribing to Kafka topics, it is important to understand how the time taken grows as the number of topics or partitions increases.
We want to know how the subscription process scales with input size.
Analyze the time complexity of the following code snippet.
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("topic1", "topic2", "topic3"));
This code creates a Kafka consumer and subscribes it to a list of topics.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating over the list of topics to register subscriptions.
- How many times: Once for each topic in the subscription list.
As the number of topics increases, the consumer must process each topic to subscribe.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 operations (one per topic) |
| 10 | 10 operations |
| 100 | 100 operations |
Pattern observation: The number of operations grows directly with the number of topics.
Time Complexity: O(n)
This means the time to subscribe grows linearly as you add more topics.
[X] Wrong: "Subscribing to multiple topics happens instantly regardless of how many topics there are."
[OK] Correct: Each topic requires processing, so more topics mean more work and more time.
Understanding how subscription time grows helps you design efficient consumers and handle scaling gracefully.
"What if we subscribed to topics using a pattern instead of a list? How would the time complexity change?"