What if you could never miss a single message in a flood of data, all without lifting a finger?
Why Consumer API basics in Kafka? - Purpose & Use Cases
Imagine you have a huge stack of letters arriving every second, and you need to read each one to find important information. Doing this by hand means sorting, opening, and reading each letter one by one.
Manually checking each letter is slow and tiring. You might miss some letters, read them out of order, or get overwhelmed by the volume. It's easy to make mistakes and lose track of what you've read.
The Consumer API acts like a smart assistant that automatically picks up letters from the stack, keeps track of which ones you've read, and delivers them in the right order. It handles the heavy lifting so you can focus on the important details.
while True: message = get_next_message() process(message) mark_as_read(message)
from kafka import KafkaConsumer consumer = KafkaConsumer('topic') for message in consumer: process(message)
It lets you efficiently and reliably read streams of data in real time without losing or duplicating messages.
Think of a news app that shows breaking headlines instantly by consuming live updates from a news feed without missing any story.
Manually handling data streams is slow and error-prone.
The Consumer API automates message reading and tracking.
This makes real-time data processing reliable and easy.
Practice
Solution
Step 1: Understand Kafka Consumer API role
The Consumer API is designed to read or consume messages from Kafka topics.Step 2: Differentiate from Producer API
The Producer API sends messages, while Consumer API reads them.Final Answer:
To read messages from Kafka topics -> Option DQuick Check:
Consumer API = Read messages [OK]
- Confusing Consumer API with Producer API
- Thinking Consumer API creates or deletes topics
- Mixing up reading and writing operations
Solution
Step 1: Recall Kafka consumer instantiation syntax
Kafka consumer is created by calling the constructor: new KafkaConsumer<>(properties);Step 2: Eliminate incorrect options
Producer is for sending messages, so new KafkaProducer<>(properties); is wrong. Options C and D are not valid KafkaConsumer methods.Final Answer:
new KafkaConsumer<>(properties); -> Option BQuick Check:
Constructor new KafkaConsumer<>(props) = Correct [OK]
- Using KafkaProducer instead of KafkaConsumer
- Calling non-existent static methods
- Confusing method names for creation
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
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(Collections.singletonList("my-topic"));
ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord record : records) {
System.out.println(record.value());
}Solution
Step 1: Understand consumer subscription and poll
The consumer subscribes to "my-topic" and polls messages for 1 second.Step 2: Process messages in the topic
Since the topic has two messages "Hello" and "World", the poll will fetch both, and the loop prints each value.Final Answer:
Hello\nWorld -> Option CQuick Check:
poll() fetches messages = Hello and World printed [OK]
- Assuming poll returns no messages immediately
- Confusing subscribe with assign
- Expecting compile or runtime errors without cause
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("key.deserializer", "StringDeserializer");
props.put("value.deserializer", "StringDeserializer");
KafkaConsumer consumer = new KafkaConsumer<>(props);
consumer.subscribe(List.of("topic1"));
ConsumerRecords records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord record : records) {
System.out.println(record.value());
}Solution
Step 1: Check deserializer configuration
The deserializer class names must be fully qualified, e.g., "org.apache.kafka.common.serialization.StringDeserializer".Step 2: Identify impact of incorrect class names
Using "StringDeserializer" alone will cause ClassNotFoundException at runtime.Final Answer:
Deserializer class names are incomplete and will cause ClassNotFoundException -> Option AQuick Check:
Deserializer full class name required = Deserializer class names are incomplete and will cause ClassNotFoundException [OK]
- Using short class names for deserializers
- Ignoring missing consumer.close() (not error here)
- Assuming poll duration causes errors
Solution
Step 1: Disable auto commit for manual offset control
Setting "enable.auto.commit" to "false" allows manual offset commits.Step 2: Subscribe to multiple topics correctly
Use consumer.subscribe(Arrays.asList("topicA", "topicB")) to listen to multiple topics.Step 3: Commit offsets after processing
Calling consumer.commitSync() after processing ensures offsets are saved manually.Final Answer:
Code snippet with manual commit, multiple topic subscription, and commitSync() -> Option AQuick Check:
Manual commit + multiple topics = props.put("enable.auto.commit", "false"); consumer.subscribe(Arrays.asList("topicA", "topicB")); while (true) { ConsumerRecords records = consumer.poll(Duration.ofMillis(1000)); for (ConsumerRecord record : records) { process(record.value()); } consumer.commitSync(); } [OK]
- Using auto commit when manual commit is needed
- Mixing assign() and subscribe() incorrectly
- Forgetting to commit offsets after processing
Ready to prove it?
Pass the test and this pattern moves unknown
