Can you describe what a Kafka Consumer is and its primary function?

Instruction: Explain the role of a consumer within the Kafka ecosystem.

Context: This question evaluates the candidate's understanding of Kafka consumers and how they are used to read data from Kafka topics, which is crucial for building consumer applications.

Updated

Official answer

A Kafka consumer fetches records from topic partitions and passes them to application code. In the Java client, a KafkaConsumer commonly subscribes to topics and calls poll repeatedly. Configured deserializers interpret the record bytes.

A standard subscribing consumer group divides partitions among its members. Other groups can read the same topic independently. Manual partition assignment is also available and does not use automatic group assignment in the same way.

The consumer tracks its position and can commit a resume offset for each partition. Fetching a record is different from completing its business effect. Committing before required processing can skip work after a crash; processing before committing can cause replay. The application must choose its failure contract and make repeated effects safe when necessary.

For an order projection in a database, one option is an atomic database transaction that records the event identifier and updates the projection, rejecting repeats. Kafka offsets alone do not make that database operation exactly once.

Bound processing and memory, handle decoding failures and rebalance ownership, and keep polling within the supported timing contract. KafkaConsumer is not generally thread-safe. If work is delegated while polling continues, disable automatic commits and explicitly coordinate completed work before advancing offsets. Measure completion age and partition skew when scaling; adding consumers beyond the available partitions does not create more ownership slots.

KafkaConsumer API, consumer configuration

Related Questions