How to get message from a kafka topic with a specific offset
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 capable of handling trillions of events a day. Retrieval of specific messages using offsets is a typical requirement when dealing with Kafka topics. This guide covers how to fetch a message from a Kafka topic at a specific offset using Apache Kafka’s Consumer API.
Understanding Kafka Topics and Offsets
In Kafka, a topic is a category or feed name to which records are published. Topics in Kafka are divided into a number of partitions. Records within a partition are each assigned a sequential ID number known as the offset. The offset for a record is a unique identifier of that record within its partition.
Offsets are crucial when retrieving specific messages from a Kafka topic because they allow consumers to specify the exact location in the log from which they want to start consuming.
Setting up a Kafka Consumer
To read messages from a Kafka topic, you need a Kafka consumer. The consumer subscribes to a list of topics and reads the records in the order they were produced. Here's how you set up a consumer in Java using the Kafka client library:
Fetching a Message at a Specific Offset
To fetch a message from a specific offset, you first need to assign the consumer to a particular partition and then seek to the desired offset. Below is an example of how this can be done:
In this example, the consumer subscribes only to partition 0 of the topic named "topic_name". It then seeks to offset 15 in that partition and starts polling messages from that offset.
Important Considerations
- Consumer group: If you repeatedly use the same consumer group, which other consumers might also be using for different or the same topics, your setting of offset can interfere with other processes or be overwritten by other members of the group consuming different offsets.
- Auto-commit: Disabling
enable.auto.commitmight be beneficial when you need explicit control over when offsets are committed in your session.
Summary Table
| Parameter | Description | Example |
| bootstrap.servers | Kafka cluster's address | "localhost:9092" |
| group.id | Consumer group identifier | "test-group" |
| enable.auto.commit | Enable/disable auto commit of offsets | "true" or "false" |
| key.deserializer | Key deserializer class | "org.apache.kafka.common.serialization.StringDeserializer" |
| value.deserializer | Value deserializer class | "org.apache.kafka.common.serialization.StringDeserializer" |
| partition | Partition in topic to consume | 0 (for partition number 0) |
| offset | Offset to start consuming from | 15 (i.e., fetching from offset 15) |
Conclusion
Fetching a Kafka message by a specific offset allows you to access historical data precisely and quickly. This can be crucial for system recovery, message replay, and more intricate data processing tasks. Understanding how to effectively set up and use the Kafka Consumer API will enhance your capabilities in working with streaming data.
Always ensure that your consumer configurations are in sync with your data processing objectives and Kafka infrastructure settings for optimal performance.
Related reading
- How to get rid of negative consumer lag in Kafka
- How to get Spring RabbitMQ to create a new Queue?
- how to get the all messages in a topic from kafka server
- how to get the group commit offset from kafka(0.10.x)
- How to get the latest offset from the Kafka topic in Confluent kafka C# library?
- How to get topic list from kafka server in Java
- How to get topics list from Kafka using C#
- How to get windowed aggregation from kafka stream?

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.