What kafka.common.OffsetOutOfRangeException means
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Kafka.common.OffsetOutOfRangeException is a specific type of exception or error that arises within Apache Kafka, a high-throughput distributed streaming platform often used for building real-time data pipelines and streaming apps. This exception indicates that the Kafka consumer attempted to fetch a message with an offset that is outside the range of offsets maintained by the server for the requested topic-partition.
Understanding Offsets
In Kafka, records within a topic are divided across different partitions for scalability and fault tolerance. Each record within a partition is assigned a sequential ID number called the offset. These offsets are used to uniquely identify each record within a partition.
Causes of OffsetOutOfRangeException
The OffsetOutOfRangeException can occur due to several reasons:
- Data eviction: Kafka topics can be configured to preserve data for a set duration or size. If data beyond this limit is purged and a consumer tries to access it, this exception can be thrown.
- Offset reset: If the consumer's previously saved offset is no longer valid, perhaps because the log has been compacted or messages have been deleted.
- Incorrect Offset Management: Consumer manually setting an offset that is beyond the current scope of available records.
Handling the Exception
When this exception is encountered, the consumer needs to handle it appropriately to continue processing. Here are some strategies:
- Seek to Beginning or End: The consumer can adjust its offset to the beginning or end of the log. This is a common recovery option.
- Dynamic Offset Adjustment: Implement logic to adjust the offset dynamically based on the available range, potentially querying the broker to fetch the earliest or latest offset.
- Manual Intervention: In some cases, particularly with a managed topic, offsets may need to be adjusted manually.
Example in Java
Here’s how this exception might be handled in a Kafka consumer application using Java:
In this example, if an OffsetOutOfRangeException is caught, the consumer’s offset is reset to the beginning of the partitions involved in the exception.
Summary Table
| Attribute | Details |
| Exception Name | kafka.common.OffsetOutOfRangeException |
| Trigger | Consumer requests an offset outside the current range on the server. |
| Common Causes | Data eviction, log compaction, or incorrect offset management. |
| Handling Strategies | Seek to valid offsets, either to the start, end, or dynamically determine a valid position. |
| Impact | Can halt data processing if unhandled, leading to potential data loss or duplicated processing. |
Additional Considerations
Monitoring and Logging: It's crucial for robust Kafka applications to implement monitoring and logging of offsets to anticipate and react to OffsetOutOfRangeExceptions. Keeping a historical log of offset ranges per consumer could prevent unexpected errors and facilitate a quicker recovery strategy.
Consumer Configuration: Configuring the consumer's auto.offset.reset property helps in managing scenarios where consumers might read deleted offsets. This configuration can be set to latest, earliest, or none, directing the consumer how to behave when data corresponding to a stored offset is missing.
Handling OffsetOutOfRangeException effectively demands a deep understanding of how Kafka's data management works in conjunction with consumer configurations and behaviors. Efficient recovery from such exceptions ensures high availability and robustness of the Kafka-based streaming applications.

