Bird
0
0

How can you modify a Kafka consumer poll loop to handle empty polls gracefully without busy looping?

hard📝 Application Q9 of 15
Kafka - Consumers
How can you modify a Kafka consumer poll loop to handle empty polls gracefully without busy looping?
ARemove the poll timeout to wait indefinitely.
BAdd a short sleep (e.g., Thread.sleep(100)) when poll returns no records.
CCall consumer.close() when no records are returned.
DUse poll(0) to avoid waiting for messages.
Step-by-Step Solution
Solution:
  1. Step 1: Understand busy looping problem

    Polling continuously with no messages wastes CPU cycles (busy loop).
  2. Step 2: Add sleep to reduce CPU usage

    Adding a short sleep when no records are returned pauses the loop briefly, reducing CPU load.
  3. Final Answer:

    Add a short sleep (e.g., Thread.sleep(100)) when poll returns no records. -> Option B
  4. Quick Check:

    Sleep on empty poll prevents busy loop [OK]
Quick Trick: Sleep briefly when no messages to avoid busy loop [OK]
Common Mistakes:
  • Removing poll timeout causes indefinite blocking
  • Closing consumer on empty poll
  • Using poll(0) causes tight loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes