Apache Kafka Tutorial 0/111 lessons ~6 min read Lesson 11
Consumers Overview
What is Kafka Consumer?
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
What is Kafka Consumer? A Kafka Consumer reads records from topics, processes them, and commits offsets to track progress in a consumer group.
Understanding the topic
What happens — Kafka Consumer:
- Subscribe to topics as part of a consumer group.
- Poll for new records in batches.
- Process each record (update DB, call API, etc.).
- Commit offset only after processing succeeds.
| Term | Description |
|---|---|
| Consumer group | A set of consumers sharing work — each partition goes to at most one member at a time. |
| Offset | Position in a partition log — where this consumer last read. |
| Poll loop | consumer.poll() fetches batches of records; keep processing faster than max.poll.interval.ms. |
| Commit | Saving offset to Kafka after processing — sync or async, manual or auto. |
| Lag | Difference between latest offset and consumer offset — key health metric. |
Visual explanation
Pipeline view:
text
Consumer polls records from topic partitions↓Process business logic (DB, API, etc.)↓Commit offset after success↓Monitor consumer lag
Step-by-step explanation
- Subscribe — Consumer joins a group and receives partition assignments.
- Poll — Fetch records in batches with consumer.poll().
- Process — Run business logic for each record.
- Commit — Save offset after successful side effects.
- Repeat — Continue polling; rebalance if group membership changes.
Informative example
Example:
java
Properties props = new Properties();props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");props.put(ConsumerConfig.GROUP_ID_CONFIG, "order-service");props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {consumer.subscribe(List.of("orders"));while (true) {ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));for (ConsumerRecord<String, String> record : records) {process(record);}consumer.commitSync();}}
Execution workflow
1Kafka Consumer workflow
1 / 5Subscribe
Consumer joins a group and receives partition assignments.
Best practices
- Use manual commits for critical side effects.
- Make consumers idempotent with event IDs or unique constraints.
- Keep processing under max.poll.interval.ms or use pause/resume patterns.
- Send poison messages to DLT with failure metadata.
Common mistakes
- Committing before database writes complete.
- Scaling consumers beyond partition count and expecting more throughput.
- Blocking the poll loop with slow downstream calls.
Summary
Consumers pull records from partitioned logs, process them, and commit offsets. Design for at-least-once delivery with idempotent handlers and lag monitoring.
Ready to mark this lesson complete?Track your journey across the entire course.