Apache Kafka Kafka Producers & Consumers 2 — Questions and Answers
Question 1: What happens when a consumer's `session.timeout.ms` expires without a heartbeat?
- The consumer is considered dead and a rebalance is triggered (Correct answer)
- The consumer pauses polling
- The broker drops all uncommitted offsets
- The consumer reconnects silently
Correct answer: The consumer is considered dead and a rebalance is triggered
If the broker does not receive a heartbeat from a consumer within session.timeout.ms, it marks that consumer as failed and triggers a group rebalance.
Question 2: Which producer configuration controls the maximum size of a single request sent to the broker?
- max.request.size (Correct answer)
- buffer.memory
- batch.size
- message.max.bytes
Correct answer: max.request.size
max.request.size limits the size (in bytes) of a request the producer sends to the broker, capping the size of individual produce requests.
Question 3: What does `auto.offset.reset=earliest` do for a new consumer group?
- Reads from the very beginning of the topic (Correct answer)
- Reads only new messages
- Throws an exception if no offset exists
- Reads from the middle partition
Correct answer: Reads from the very beginning of the topic
earliest causes the consumer to start reading from the earliest available offset when no committed offset exists for the group.
Question 4: How does a Kafka producer achieve idempotency?
- By setting enable.idempotence=true, which assigns a unique producer ID and sequence numbers (Correct answer)
- By using acks=0
- By disabling retries
- By writing to a single partition only
Correct answer: By setting enable.idempotence=true, which assigns a unique producer ID and sequence numbers
Enabling idempotence assigns a Producer ID and per-partition sequence numbers so the broker can deduplicate retried messages.
Question 5: What is the role of `buffer.memory` in a Kafka producer?
- Total bytes of memory the producer uses to buffer records waiting to be sent (Correct answer)
- Size of each message batch
- Memory reserved for compression
- JVM heap allocation for the producer
Correct answer: Total bytes of memory the producer uses to buffer records waiting to be sent
buffer.memory is the total amount of memory the producer allocates for buffering records before they are transmitted to brokers.
Question 6: Which consumer API call is used to manually commit the current offset synchronously?
- commitSync() (Correct answer)
- commitAsync()
- seek()
- assign()
Correct answer: commitSync()
commitSync() blocks until the broker acknowledges the offset commit, providing a reliable but potentially slower commit method.
What happens when a consumer's `session.timeout.ms` expires without a heartbeat?