Kafka - ConsumersHow 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.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand busy looping problemPolling continuously with no messages wastes CPU cycles (busy loop).Step 2: Add sleep to reduce CPU usageAdding a short sleep when no records are returned pauses the loop briefly, reducing CPU load.Final Answer:Add a short sleep (e.g., Thread.sleep(100)) when poll returns no records. -> Option BQuick 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 blockingClosing consumer on empty pollUsing poll(0) causes tight loop
Master "Consumers" in Kafka9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Kafka Quizzes Consumer Groups - Why consumer groups enable parallel processing - Quiz 9hard Consumer Groups - Cooperative vs eager rebalancing - Quiz 2easy Kafka Basics and Event Streaming - Event streaming concept - Quiz 13medium Kafka Basics and Event Streaming - Message broker architecture - Quiz 6medium Kafka Basics and Event Streaming - Kafka CLI tools overview - Quiz 14medium Kafka Cluster Architecture - Controller broker - Quiz 13medium Kafka Cluster Architecture - Broker nodes - Quiz 3easy Producers - Message key and value - Quiz 5medium Topics and Partitions - Topic deletion and cleanup - Quiz 8hard Topics and Partitions - Why topics organize messages - Quiz 8hard