0
0
Kafkadevops~20 mins

Consumer poll loop in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Consumer 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 poll loop?

Consider the following Kafka consumer poll loop in Java. What will be printed to the console?

Kafka
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));
int messageCount = 0;
while (messageCount < 3) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println("Received: " + record.value());
        messageCount++;
    }
}
consumer.close();
AThrows a NullPointerException because poll duration is invalid.
BPrints messages indefinitely without stopping.
CPrints exactly 3 messages from the topic and then stops.
DPrints zero messages and exits immediately.
Attempts:
2 left
💡 Hint

Look at the messageCount condition in the while loop.

🧠 Conceptual
intermediate
1:30remaining
What happens if you forget to call consumer.poll() in a Kafka consumer loop?

In a Kafka consumer application, what is the effect of not calling consumer.poll() regularly inside the consumer loop?

AThe consumer will automatically commit offsets without polling.
BThe consumer will continue receiving messages normally without any issues.
CThe consumer will throw a compile-time error.
DThe consumer will stop receiving messages and may be considered dead by the broker, causing a rebalance.
Attempts:
2 left
💡 Hint

Think about how Kafka detects active consumers in a group.

🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer poll loop cause a commit failure?

Examine the following code snippet. Why might this consumer fail to commit offsets properly?

Kafka
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("topic1"));
while (true) {
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        process(record.value());
    }
    // Missing commit call here
}
AOffsets are never committed because commitSync or commitAsync is not called after processing.
BThe consumer throws a CommitFailedException immediately.
COffsets are committed automatically after poll, so no issue occurs.
DThe consumer commits offsets before processing messages, causing data loss.
Attempts:
2 left
💡 Hint

Check where offset commits happen in Kafka consumer code.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this Kafka consumer poll loop?

Identify the correct fix for the syntax error in this Kafka consumer poll loop snippet:

while (true) {
    ConsumerRecords records = consumer.poll(100);
    for (ConsumerRecord record : records) {
        System.out.println(record.value());
    }
}
AChange consumer.poll(100) to consumer.poll(Duration.ofMillis(100))
BChange consumer.poll(100) to consumer.poll(100L)
CAdd a semicolon after consumer.poll(100)
DChange for loop to for (record in records)
Attempts:
2 left
💡 Hint

Check the expected argument type for poll method in KafkaConsumer.

🚀 Application
expert
3:00remaining
How to implement a graceful shutdown in a Kafka consumer poll loop?

You want to stop a Kafka consumer poll loop gracefully on receiving a shutdown signal. Which approach correctly implements this?

Kafka
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic"));
final AtomicBoolean running = new AtomicBoolean(true);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    running.set(false);
    consumer.wakeup();
}));
try {
    while (running.get()) {
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
        for (ConsumerRecord<String, String> record : records) {
            process(record.value());
        }
        consumer.commitSync();
    }
} catch (WakeupException e) {
    if (running.get()) throw e;
} finally {
    consumer.close();
}
AUse Thread.stop() to kill the consumer thread immediately.
BUse an AtomicBoolean flag and call consumer.wakeup() in a shutdown hook to exit poll loop safely.
CIgnore shutdown signals and let the JVM terminate the consumer abruptly.
DCall consumer.close() directly inside the poll loop without any flag or wakeup.
Attempts:
2 left
💡 Hint

Consider how to interrupt a blocking poll call safely.