auto.offset.reset
configuration.
Auto offset reset options
earliest
- Consumer will start reading from the beginning of the partition
- Reads all available messages from the earliest available offset
- Useful for reprocessing all historical data
- Use case: Data migration, audit requirements, complete reprocessing
latest (default)
- Consumer will start reading from the end of the partition
- Only processes new messages produced after the consumer starts
- Use case: Real-time processing where historical data is not needed
none
- Consumer throws an exception if no previous offset is found
- Forces explicit offset management
- Use case: Strict control over consumer behavior, prevents accidental data loss or reprocessing
When auto offset reset is triggered
Theauto.offset.reset
behavior is triggered in these scenarios:
- New consumer group: First time a consumer group subscribes to a topic
- Invalid offset: Committed offset no longer exists (data deleted due to retention)
- Offset out of range: Committed offset is beyond the current log boundaries
Common scenarios
Scenario 1: New consumer group
Scenario 2: Data retention cleanup
Best practices
For production systems
For development/testing
For critical data processing
Monitoring offset reset events
Key metrics to monitor:- Consumer group lag
- Offset reset occurrences
- Consumer restarts and rebalances
Error handling example
Offset management strategies
Automatic offset management
- Use
enable.auto.commit=true
- Set appropriate
auto.commit.interval.ms
- Choose suitable
auto.offset.reset
policy
Manual offset management
- Use
enable.auto.commit=false
- Call
commitSync()
orcommitAsync()
after processing - Handle offset reset scenarios explicitly
External offset storage
- Store offsets in external systems (database, file system)
- Use
seek()
methods to position consumer - Implement custom offset management logic
Data loss vs duplication
auto.offset.reset=latest
can cause data loss if messages arrive while consumer is downauto.offset.reset=earliest
can cause message duplication if consumer group is recreatedauto.offset.reset=none
requires explicit error handling but provides the most control