Kafka
Messaging System
Data Offset
Topic Management
Programming Guide

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.

Practice system design

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:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("enable.auto.commit", "true");
5props.put("auto.commit.interval.ms", "1000");
6props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
8
9KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

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:

java
1import org.apache.kafka.common.TopicPartition;
2
3TopicPartition partition0 = new TopicPartition("topic_name", 0);
4consumer.assign(Arrays.asList(partition0));
5
6// Specify the offset from where you want to start consuming
7long desiredOffset = 15;
8consumer.seek(partition0, desiredOffset);
9
10// Polling loop to fetch records from Kafka
11try {
12    while (true) {
13        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
14        for (ConsumerRecord<String, String> record : records) {
15            System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
16            // Break after the first record since only one record is needed
17            break;
18        }
19        break; // Exit while loop after reading the desired record
20    }
21} finally {
22    consumer.close();
23}

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.commit might be beneficial when you need explicit control over when offsets are committed in your session.

Summary Table

ParameterDescriptionExample
bootstrap.serversKafka cluster's address"localhost:9092"
group.idConsumer group identifier"test-group"
enable.auto.commitEnable/disable auto commit of offsets"true" or "false"
key.deserializerKey deserializer class"org.apache.kafka.common.serialization.StringDeserializer"
value.deserializerValue deserializer class"org.apache.kafka.common.serialization.StringDeserializer"
partitionPartition in topic to consume0 (for partition number 0)
offsetOffset to start consuming from15 (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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.