How to make kafka consumer to read from last consumed offset but not from beginning
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. A fundamental piece of Kafka's capability is its durable consumer offset tracking feature. This feature allows Kafka consumers to continue consuming data from where they left off, ensuring no data loss or duplication after a rebalance or restart of consumer clients. This behavior is especially crucial for applications that require high reliability and consistency. Below, we will explore how you can configure your Kafka consumer to read from the last consumed offset rather than from the beginning.
Understanding Consumer Groups and Offsets
In Kafka, consumers are typically organized into consumer groups. Each consumer within a group reads from exclusive partitions of a topic, and the group collectively covers the entire topic. Each message in a partition is assigned a unique sequential ID called an offset. Kafka stores the offset of the last message read by a consumer group in a special internal Kafka topic named __consumer_offsets. When a consumer in a group restarts, it can resume reading from where it left off using this stored offset.
Configuration Requirements
To ensure that your Kafka consumer reads from the last consumed offset, you need to correctly configure the following properties in your consumer:
- Group ID: Each consumer has to specify a
group.idthat identifies the consumer group it belongs. This ID is used by Kafka to track the offset consumption for each consumer group. - Auto Offset Reset: The
auto.offset.resetproperty should be set wisely.earliest: This tells the consumer to start from the oldest message in the topic if no previous offset is found for this group.latest: This will start reading from new messages that get added after the consumer joins the group.none: This setting will throw an exception if no previous offset is tracked for this consumer group. For reading from the last consumed offset, generallylatestornoneis preferred dependent on your application behavior in situations where no offsets are saved.
- Enable Auto Commit: Setting
enable.auto.committotrueallows Kafka to automatically commit offsets at a frequency determined byauto.commit.interval.ms. This setting relieves developers from manually managing offset commits, which can reduce complexity and potential errors.
Practical Consumer Configuration Example
Here’s an example configuration for a Kafka consumer in Java:
Offset Management Practices
- Manual Offset Control: Advanced users might choose to manually manage offsets using
commitSyncandcommitAsyncmethods provided by KafkaConsumer. However, this increases the complexity and potential errors. - Handling Rebalances: Implement a
ConsumerRebalanceListenerto handle the event when a consumer might lose access to partitions. This ensures that consumer can commit its offset before losing the partition.
Summary Table
| Property | Recommended Value | Description |
group.id | Must be unique per group | Identifies the consumer‘s group. |
enable.auto.commit | true | If true, offsets are committed automatically. |
auto.offset.reset | latest or none | Controls where consumption starts if no offset is saved. |
Conclusion
Handling offsets in Kafka is foundational for reliable message processing. Proper configuration and understanding of consumer groups, offset management, and the implications of various settings are critical for developing robust Kafka-based applications. Through the careful use of the settings and practices discussed, developers can enhance durability and fault tolerance in their Kafka consumer implementations.

