0
0
Kafkadevops~20 mins

Consumer group concept in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Consumer Group Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka consumer group example?

Consider a Kafka topic with 3 partitions and a consumer group with 2 consumers. Each consumer reads messages from assigned partitions. If the topic has 9 messages evenly distributed, how many messages will each consumer receive?

Kafka
Topic partitions: 3
Messages per partition: 3
Consumers in group: 2

# Messages are evenly distributed across partitions.
# Consumers get assigned partitions evenly or as balanced as possible.
AConsumer 1: 4 messages, Consumer 2: 5 messages
BConsumer 1: 3 messages, Consumer 2: 6 messages
CConsumer 1: 5 messages, Consumer 2: 4 messages
DConsumer 1: 6 messages, Consumer 2: 3 messages
Attempts:
2 left
💡 Hint

Partitions are assigned to consumers. Each partition's messages go to one consumer only.

🧠 Conceptual
intermediate
1:30remaining
What happens when a new consumer joins an existing consumer group?

In Kafka, when a new consumer joins a consumer group that is already consuming from a topic, what is the expected behavior?

AThe new consumer is ignored until the current consumers disconnect.
BThe new consumer starts consuming from the last committed offset without affecting others.
CThe group triggers a rebalance and partitions are reassigned among all consumers.
DThe new consumer consumes from all partitions alongside existing consumers.
Attempts:
2 left
💡 Hint

Think about how Kafka ensures each partition is consumed by only one consumer in the group.

🔧 Debug
advanced
2:00remaining
Why does Consumer 3 not receive any messages?

Given a topic with 2 partitions and a consumer group with 3 consumers, one consumer is not receiving any messages. Here is the consumer group assignment log snippet:

Consumer 1 assigned partitions: 0, 1
Consumer 2 assigned partitions: 
Consumer 3 assigned partitions: 

Why is Consumer 3 not receiving messages?

ABecause the number of consumers exceeds the number of partitions, one consumer gets no partitions.
BBecause Consumer 3 has a network issue and cannot receive messages.
CBecause Kafka assigns partitions randomly and Consumer 3 was skipped by mistake.
DBecause Consumer 3 is not subscribed to the topic.
Attempts:
2 left
💡 Hint

Consider how Kafka assigns partitions when consumers outnumber partitions.

📝 Syntax
advanced
2:00remaining
What error does this Kafka consumer group code produce?

Consider this Kafka consumer group code snippet in Java:

Properties props = new Properties();
props.put("group.id", "my-group");
props.put("bootstrap.servers", "localhost:9092");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));

while(true) {
    ConsumerRecords records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord record : records) {
        System.out.println(record.value());
    }
}
consumer.close();

What error will this code produce?

ACompilation error because consumer.close() is unreachable after infinite loop.
BNullPointerException because props is missing required properties.
CCompilation error due to missing import statements.
DNo error; code runs and consumes messages correctly.
Attempts:
2 left
💡 Hint

Look at the infinite loop and the position of consumer.close().

🚀 Application
expert
2:30remaining
How many partitions will each consumer get after rebalance?

A Kafka topic has 7 partitions. A consumer group has 4 consumers. After a rebalance, how many partitions will each consumer be assigned?

AConsumers 1-2 get 3 partitions each, Consumers 3-4 get 0 partitions.
BConsumers 1-3 get 2 partitions each, Consumer 4 gets 1 partition.
CEach consumer gets exactly 1 partition, and 3 partitions remain unassigned.
DConsumers 1-4 get 1 partition each, and 3 partitions are assigned to a new consumer.
Attempts:
2 left
💡 Hint

Partitions are distributed as evenly as possible among consumers.