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?"
Practice
subscribe() method in Kafka consumers?Solution
Step 1: Understand the role of subscribe()
Thesubscribe()method is used by Kafka consumers to specify which topics they want to listen to.Step 2: Identify what subscribe() enables
Once subscribed, the consumer automatically receives messages from those topics when callingpoll().Final Answer:
To listen to one or more Kafka topics and receive messages automatically -> Option CQuick Check:
subscribe() = listen to topics [OK]
- Confusing subscribe() with sending messages
- Thinking subscribe() creates topics
- Assuming subscribe() closes connections
Solution
Step 1: Recall the subscribe() method signature
In Kafka,subscribe()takes a list (or collection) of topic names as an argument.Step 2: Identify correct syntax for multiple topics
Passing a list like["topicA", "topicB"]is the correct way to subscribe to multiple topics.Final Answer:
consumer.subscribe(["topicA", "topicB"]) -> Option AQuick Check:
subscribe() needs a list for multiple topics [OK]
- Passing multiple string arguments instead of a list
- Concatenating topic names into one string
- Using curly braces instead of square brackets
consumer.subscribe(["news"])
records = consumer.poll(timeout_ms=1000)
for record in records:
print(record.value)Solution
Step 1: Understand subscribe() and poll() behavior
The consumer subscribes to "news" and callspoll()once, which fetches available messages.Step 2: Analyze the for loop printing each record
The loop prints each message value on its own line, so "Hello" and "World" appear separately.Final Answer:
Hello\nWorld -> Option BQuick Check:
poll() returns messages, loop prints each [OK]
- Thinking poll() returns a list printed directly
- Assuming messages print concatenated without newline
- Believing poll() needs a loop to work at all
consumer.subscribe("topic1")
while True:
records = consumer.poll(1000)
for record in records:
print(record.value)Solution
Step 1: Check subscribe() argument type
Thesubscribe()method requires a list of topics, even if subscribing to one topic.Step 2: Identify the error in argument
Passing a single string instead of a list causes a type error or unexpected behavior.Final Answer:
subscribe() expects a list, not a single string -> Option AQuick Check:
subscribe() needs list argument [OK]
- Passing a string instead of a list to subscribe()
- Thinking poll() can't be inside a loop
- Misunderstanding loop type requirements
Solution
Step 1: Understand dynamic topic subscription
Kafka consumers can subscribe to topics matching a regex pattern usingsubscribe(Pattern).Step 2: Identify correct method for pattern subscription
Usingconsumer.subscribe(Pattern.compile("logs-.*"))subscribes to all topics starting with "logs-" automatically.Final Answer:
Use consumer.subscribe(Pattern.compile("logs-.*")) to subscribe by regex pattern -> Option DQuick Check:
subscribe() supports regex patterns [OK]
- Listing topics manually instead of using regex
- Calling subscribe() multiple times incorrectly
- Using assign() which requires partitions, not topics
Ready to prove it?
Pass the test and this pattern moves unknown
