How to Fix Kafka Offset Out of Range Error Quickly
offset out of range error in Kafka happens when a consumer tries to read a message offset that no longer exists on the broker. To fix it, reset the consumer's offset to a valid position using Kafka's --reset-offsets tool or configure the consumer to start from the earliest or latest offset.Why This Happens
This error occurs because Kafka stores messages for a limited time or size. If a consumer tries to read an offset that was deleted due to log retention policies, Kafka throws an offset out of range error. This usually happens when the consumer is too slow or has been offline for a long time.
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-topic --partition 0 --offset 1000
The Fix
Reset the consumer offset to a valid position using Kafka's kafka-consumer-groups.sh --reset-offsets command. You can set the offset to the earliest or latest available offset to resume consumption without errors.
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-group --topic my-topic --reset-offsets --to-earliest --executePrevention
To avoid this error, keep your consumers active and process messages promptly. Configure your consumer with auto.offset.reset=earliest or auto.offset.reset=latest to handle missing offsets gracefully. Also, monitor your Kafka retention policies and consumer lag regularly.
Related Errors
- Consumer Lag Too High: Causes delayed processing and potential offset errors.
- Group Coordinator Not Available: Consumer cannot commit offsets properly.
- Offset Commit Failed: Happens if consumer loses connection during commit.