Consumer group concept in Kafka - Time & Space Complexity
When using Kafka consumer groups, it is important to understand how the time to process messages changes as the number of messages grows.
We want to know how the work done by consumers scales with more messages and partitions.
Analyze the time complexity of consuming messages with a consumer group.
consumer.subscribe(Collections.singletonList("topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record);
}
}
This code shows a consumer in a group polling messages from a topic and processing 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 consumer processes more messages each poll.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 messages | 10 processing steps |
| 100 messages | 100 processing steps |
| 1000 messages | 1000 processing steps |
Pattern observation: The work grows directly with the number of messages to process.
Time Complexity: O(n)
This means the time to process messages grows linearly with the number of messages received.
[X] Wrong: "Adding more consumers in a group always makes processing time constant no matter how many messages there are."
[OK] Correct: More consumers can share the load, but total work still grows with messages. Consumers just split the work, so each does less, but total work depends on total messages.
Understanding how consumer groups scale helps you explain real-world message processing and shows you grasp how distributed systems handle growing data.
"What if the topic has more partitions than consumers? How would that affect the time complexity for each consumer?"
Practice
consumer group in Kafka?Solution
Step 1: Understand consumer group role
A consumer group lets multiple consumers work together to read messages from a topic.Step 2: Compare other options
Storing messages, producing messages, and encrypting are not roles of consumer groups.Final Answer:
To allow multiple consumers to share the work of reading messages -> Option AQuick Check:
Consumer group = share reading work [OK]
- Confusing consumer groups with producers
- Thinking consumer groups store messages
- Assuming consumer groups encrypt data
Solution
Step 1: Recall Kafka consumer config keys
The correct key to set the consumer group is "group.id".Step 2: Check other options
Other keys like "consumer.group", "groupName", or "consumerId" are incorrect or do not exist.Final Answer:
"group.id" : "my-group" -> Option BQuick Check:
Consumer group ID key = "group.id" [OK]
- Using incorrect config keys
- Confusing consumer ID with group ID
- Misspelling the key name
Solution
Step 1: Understand partition assignment
Partitions are divided among consumers in a group. With 4 partitions and 2 consumers, partitions split evenly.Step 2: Calculate partitions per consumer
4 partitions / 2 consumers = 2 partitions per consumer.Final Answer:
Each consumer reads from 2 partitions -> Option AQuick Check:
Partitions per consumer = total partitions / consumers = 2 [OK]
- Assuming each consumer reads all partitions
- Thinking partitions are not shared
- Ignoring partition count
props.put("group.id", "my-group");
props.put("enable.auto.commit", "false");
consumer.subscribe(Arrays.asList("my-topic"));
consumer.poll(Duration.ofMillis(100));
// Missing commit after pollSolution
Step 1: Analyze auto commit setting
Auto commit is disabled, so offsets must be committed manually.Step 2: Check code for commit call
Code calls poll but does not commit offsets, so messages may be reprocessed on restart.Final Answer:
The consumer never commits offsets, risking message reprocessing -> Option DQuick Check:
Auto commit false needs manual commit [OK]
- Forgetting to commit offsets manually
- Assuming auto commit is enabled by default
- Ignoring poll timeout relevance
Solution
Step 1: Understand partition to consumer assignment
Each partition can be assigned to only one consumer in the group.Step 2: Analyze consumer count vs partitions
With 3 partitions and 4 consumers, only 3 consumers get partitions; the 4th consumer stays idle.Final Answer:
The extra consumer remains idle and does not receive messages -> Option CQuick Check:
More consumers than partitions means idle consumers [OK]
- Thinking Kafka creates new partitions automatically
- Assuming extra consumers share partitions
- Believing extra consumers cause errors
Ready to prove it?
Pass the test and this pattern moves unknown
