Consumer configuration in Kafka - Commands & Configuration
bootstrap.servers=localhost:9092 key.deserializer=org.apache.kafka.common.serialization.StringDeserializer value.deserializer=org.apache.kafka.common.serialization.StringDeserializer group.id=my-consumer-group enable.auto.commit=false auto.offset.reset=earliest max.poll.records=500 session.timeout.ms=10000
bootstrap.servers: Kafka server address to connect.
key.deserializer and value.deserializer: How to convert message bytes to strings.
group.id: Consumer group name to share message reading.
enable.auto.commit: Whether to mark messages as read automatically.
auto.offset.reset: Where to start if no previous read position exists.
max.poll.records: Max messages to fetch in one request.
session.timeout.ms: Time to detect consumer failure.
kafka-console-consumer --bootstrap-server localhost:9092 --topic my-topic --group my-consumer-group --from-beginning --consumer.config consumer.properties--bootstrap-server - Specifies the Kafka server address to connect.--topic - Specifies the topic to consume messages from.--group - Sets the consumer group for load balancing and offset tracking.--from-beginning - Starts reading messages from the earliest offset.--consumer.config - Loads consumer configuration from a properties file.kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-consumer-group--bootstrap-server - Specifies the Kafka server address.--describe - Shows detailed information about the consumer group.--group - Specifies which consumer group to inspect.If you remember nothing else from this pattern, remember: consumer configuration controls how your app reads, commits, and manages message delivery from Kafka topics.
Practice
What is the purpose of setting group.id in Kafka consumer configuration?
Solution
Step 1: Understand the role of
Thegroup.idgroup.ididentifies the consumer group that the consumer belongs to, allowing Kafka to coordinate message consumption among multiple consumers.Step 2: Differentiate from other configurations
Other options like broker address or deserializer do not relate to consumer group coordination.Final Answer:
To identify the consumer group for coordinating message consumption -> Option CQuick Check:
group.id = consumer group ID [OK]
- Confusing group.id with broker address
- Thinking group.id sets deserializer
- Assuming group.id controls message fetch size
Which of the following is the correct way to set the Kafka consumer's auto.offset.reset property to start reading from the earliest message?
Solution
Step 1: Recall valid values for
The valid values are "earliest", "latest", and "none". "earliest" means start from the oldest message.auto.offset.resetStep 2: Identify the value for reading from earliest
Setting it to "earliest" makes the consumer read from the beginning if no offset is found.Final Answer:
"auto.offset.reset" = "earliest" -> Option BQuick Check:
earliest = start from oldest message [OK]
- Using "first" which is invalid
- Confusing "latest" with earliest
- Setting "none" expecting to read messages
Given the following Kafka consumer configuration snippet, what will be the output behavior when consuming messages?
props.put("enable.auto.commit", "false");
props.put("auto.offset.reset", "earliest");
props.put("group.id", "my-group");Assuming this consumer starts fresh with no committed offsets, what happens?
Solution
Step 1: Analyze
It is set to "false", so the consumer will not automatically commit offsets after reading messages.enable.auto.commitsettingStep 2: Analyze
With "earliest" and a new group "my-group", the consumer starts reading from the earliest messages available.auto.offset.resetandgroup.idFinal Answer:
Consumer reads messages from the earliest offset but does not auto-commit offsets -> Option AQuick Check:
enable.auto.commit=false + earliest = read earliest, no auto commit [OK]
- Assuming auto-commit is enabled by default
- Confusing earliest with latest offset reset
- Expecting error without deserializer (not shown here)
Identify the error in this Kafka consumer configuration snippet:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", true);
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");Solution
Step 1: Check data types for properties
Kafka consumer properties expect String values, so "enable.auto.commit" should be set as "true" (a String), not boolean true.Step 2: Verify other properties
"group.id" is present, "bootstrap.servers" format is correct, and deserializers are valid for String keys and values.Final Answer:
"enable.auto.commit" should be a String "true", not boolean true -> Option AQuick Check:
Properties values must be Strings [OK]
- Using boolean true instead of "true" string
- Omitting group.id property
- Incorrect bootstrap.servers format
- Wrong deserializer class for String data
You want a Kafka consumer to read messages from multiple topics and manually commit offsets only after processing batches successfully. Which configuration settings should you use?
Solution
Step 1: Understand manual offset commit requirement
Setting "enable.auto.commit" to "false" disables automatic commits, allowing manual control after processing.Step 2: Subscribe to multiple topics
Usingconsumer.subscribe(List.of(...))allows subscribing to multiple topics easily.Step 3: Evaluate other options
"enable.auto.commit" = "true" and assign partitions manually enables auto commit which conflicts with manual commit need; "auto.offset.reset" = "latest" and useconsumer.assign()for topics uses latest offset reset which may skip old messages; "enable.auto.commit" = "false" and setauto.offset.resetto "none" sets offset reset to "none" which causes errors if no offset found.Final Answer:
"enable.auto.commit" = "false" and subscribe to multiple topics withconsumer.subscribe(List.of(...))-> Option DQuick Check:
manual commit + subscribe multiple topics = "enable.auto.commit" = "false" and subscribe to multiple topics withconsumer.subscribe(List.of(...))[OK]
- Enabling auto commit when manual commit is needed
- Using assign() instead of subscribe() for multiple topics
- Setting auto.offset.reset to "none" causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
