📖 Scenario: Imagine you run a busy online store. Many orders come in every second. You want to process these orders quickly and smoothly without making customers wait.
🎯 Goal: You will learn how Kafka consumer groups help process many messages at the same time, making your order system faster and more efficient.
📋 What You'll Learn
Create a list of order messages
Set up a consumer group size variable
Assign orders to consumers using parallel processing logic
Print which consumer processes which orders
💡 Why This Matters
🌍 Real World
In real online stores or apps, many messages or tasks come in fast. Using consumer groups lets multiple workers handle these tasks at the same time, speeding up processing.
💼 Career
Understanding consumer groups and parallel processing is key for jobs in data engineering, backend development, and system design where Kafka or similar tools are used.
Progress0 / 4 steps
1
Create the list of orders
Create a list called orders with these exact strings: 'order1', 'order2', 'order3', 'order4', 'order5'.
Kafka
Hint
Use square brackets [] to make a list and put the order names inside quotes.
2
Set the consumer group size
Create a variable called consumer_group_size and set it to 2.
Kafka
Hint
Just write the variable name, an equals sign, and the number 2.
3
Assign orders to consumers in parallel
Create a dictionary called assignments that assigns each order to a consumer number from 0 up to consumer_group_size - 1. Use a for loop with enumerate(orders) and assign consumers using index % consumer_group_size.
Kafka
Hint
Use enumerate to get the order position and use modulo % to cycle through consumers.
4
Print the consumer assignments
Use a for loop with order, consumer in assignments.items() to print lines like Order order1 is processed by consumer 0 for each order.
Kafka
Hint
Use a for loop over assignments.items() and print with an f-string.
Practice
(1/5)
1. What is the main reason consumer groups enable parallel processing in Kafka?
easy
A. They limit the number of messages a consumer can read at once.
B. They duplicate messages to all consumers for faster processing.
C. They combine all messages into a single stream for one consumer.
D. They allow multiple consumers to read different partitions of a topic simultaneously.
Solution
Step 1: Understand consumer groups and partitions
Consumer groups split topic partitions among consumers so each reads a unique subset.
Step 2: Recognize parallel processing benefit
Because consumers read different partitions at the same time, processing happens in parallel.
Final Answer:
They allow multiple consumers to read different partitions of a topic simultaneously. -> Option D
Quick Check:
Parallel reading = multiple consumers on partitions [OK]
Hint: Think: multiple consumers share partitions, not duplicate messages [OK]
Common Mistakes:
Thinking all consumers get all messages
Believing messages are duplicated for speed
Confusing parallel with sequential processing
2. Which of the following is the correct way to assign a consumer to a group in Kafka's consumer configuration?
easy
A. group.id = "my-group"
B. consumer.group = "my-group"
C. groupName = "my-group"
D. assign.group = "my-group"
Solution
Step 1: Recall Kafka consumer configuration keys
The correct key to set the consumer group is 'group.id'.
Step 2: Match the correct syntax
Only 'group.id = "my-group"' matches Kafka's expected configuration format.
Final Answer:
group.id = "my-group" -> Option A
Quick Check:
Kafka uses 'group.id' to set consumer groups [OK]
Hint: Remember Kafka config uses 'group.id' for groups [OK]
Common Mistakes:
Using incorrect keys like 'consumer.group'
Confusing variable names with config keys
Missing quotes around group name
3. Given a topic with 4 partitions and a consumer group with 2 consumers, what will happen when both consumers start reading?
medium
A. Each consumer reads 2 unique partitions, no overlap.
B. Each consumer reads all 4 partitions simultaneously.
C. Only one consumer reads all partitions; the other waits.
D. Consumers randomly read any partition, possibly overlapping.
Solution
Step 1: Understand partition assignment in consumer groups
Partitions are divided among consumers so each partition is read by only one consumer.
Step 2: Calculate partition distribution
With 4 partitions and 2 consumers, each consumer gets 2 partitions uniquely.
Final Answer:
Each consumer reads 2 unique partitions, no overlap. -> Option A
Quick Check:
Partitions split evenly among consumers [OK]
Hint: Partitions divide evenly among consumers in a group [OK]
Common Mistakes:
Assuming consumers read all partitions
Thinking partitions overlap between consumers
Believing one consumer reads everything
4. You have 3 consumers in a group but only 2 partitions in the topic. What issue will occur?
medium
A. All consumers will share partitions equally.
B. One consumer will be idle with no partitions assigned.
C. Kafka will create a new partition automatically.
D. Consumers will read duplicate messages from partitions.
Solution
Step 1: Analyze partition to consumer ratio
There are fewer partitions (2) than consumers (3), so not all consumers get partitions.
Step 2: Understand assignment behavior
Kafka assigns partitions exclusively; one consumer will have no partition and remain idle.
Final Answer:
One consumer will be idle with no partitions assigned. -> Option B
Quick Check:
Partitions < consumers means idle consumers [OK]
Hint: More consumers than partitions means some idle consumers [OK]