Kafka How to Display Offsets
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform that is widely used for building real-time data pipelines and streaming apps. It is also a robust tool for handling real-time data feeds, with capabilities including fault-tolerance, scalability, and event sequencing. A fundamental aspect of managing and monitoring Kafka effectively lies in understanding and displaying offsets, which are crucial for tracking message consumption.
Understanding Kafka Offsets
In Kafka, an offset is a unique identifier for each record in a partition of a topic. It denotes the position of a record within that partition. Offsets are incremental; new records get higher offsets, making them critical for consumers to keep track of which messages have been processed and where to resume in case of failure or restart.
Key Components Related to Offsets
- Producer: Pushes messages into Kafka topics.
- Broker: Kafka servers where topics and partitions are hosted.
- Consumer: Retrieves messages from the broker using the offset to keep track of its current position within a partition.
- Consumer Group: A group of consumers acting as a single logical subscriber. Positions are tracked separately for each consumer group.
Displaying and Managing Offsets
To effectively manage message consumption in Kafka, you need to know how to view and manipulate consumer offsets. The most common tool for this is Kafka's command-line tools, particularly kafka-consumer-groups.sh.
Display Consumer Group Offsets
To view the current offset for a consumer or consumer group, use the following command:
This command displays information such as:
- GROUP: The name of the consumer group.
- TOPIC: The topic the group is consuming.
- PARTITION: The specific partition within the topic.
- CURRENT-OFFSET: The current position of the consumer.
- LOG-END-OFFSET: The latest available offset in the log (i.e., where the next message will be placed).
- LAG: The difference between LOG-END-OFFSET and CURRENT-OFFSET, showing how many messages the consumer is behind.
Example Output
Here's an example output of the kafka-consumer-groups.sh command:
Resetting Consumer Offsets
There might be scenarios where you need to reset the offsets for a consumer group, such as after reprocessing messages. To reset consumer offsets, use the following command:
Common Use Cases for Resetting Offsets
- Reprocessing Messages: Reset to an earlier offset.
- Skipping Corrupt Messages: Advance offset past problematic messages.
- Initial Setup: Set specific starting points when adding new consumer groups.
Summary of Key Commands and Actions
| Command | Description | Example |
kafka-consumer-groups.sh --describe | Displays current group offsets. | --bootstrap-server localhost:9092 --describe --group group1 |
kafka-consumer-groups.sh --reset-offsets | Resets offsets of a consumer group. | --bootstrap-server localhost:9092 --group group1 --reset-offsets --to-offset 5 --execute |
--to-offset | Resets to a specific offset. | --to-offset 123 |
--shift-by | Shifts current offset by a given number. | --shift-by -2 |
--to-earliest | Resets to the earliest offset. | --to-earliest
Resets to the beginning of the log. |
--to-latest | Resets to the latest offset. | --to-latest
Jumps to the end of the log. |
--to-datetime | Resets to offsets from a specific time. | --to-datetime 2023-06-01T10:00:00.000 |
Conclusion
Understanding and managing offsets are key to effectively using Kafka. Whether you are overseeing consumer positions, troubleshooting, or setting up new consumer groups, having a solid grasp of how to display and manipulate offsets can greatly enhance your Kafka operations. Using these command-line tools, you can maintain tighter control over data flow and consumption in your streaming applications.
Related reading
- Kafka How to get last modified time for a topic i.e. last message added to any partition of the topic
- Kafka how to read from __consumer_offsets topic
- Kafka How to retrieve a response from consumer?
- Kafka how to set producer retries to Infinity
- Kafka Idempotent producer
- Kafka ignoring `transaction.timeout.ms` for producer
- Kafka in distributed system
- Kafka in Kubernetes - Marking the coordinator dead for group

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.