Apache Kafka
Message Offset
Data Streaming
Distributed Systems
Real-time Processing

Reading messages offset in Apache Kafka

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 stream processing system which is widely used for real-time data pipelines and streaming applications. A fundamental part of Kafka's architecture is the concept of message offsets, which plays a critical role in message management and ensuring the reliability and scalability of the event streaming process.

What is an Offset?

In Kafka, messages are stored in topics, which are divided into one or more partitions. An offset is a unique identifier for each message within a partition. It denotes the position of a message within the ordered sequence in that partition. Offsets are incremental numbers, starting from 0, and increase by one for each subsequent message.

Why are Offsets Important?

Offsets are crucial because they allow Kafka consumers to keep track of which messages have been read and processed. This facilitates:

  • Fault tolerance: In the event of a failure, consumers can resume reading from the last committed offset.
  • Scalability: Consumers can run in parallel, each reading different segments of topic data.
  • Replayability: Enables reprocessing of data by resetting the offset.

How Do Consumers Manage Offsets?

Consumers track their offsets using either Kafka's internal mechanisms or external storage systems like a database. Kafka provides the following methods for offset management:

  • Auto-commit: Automatically persists the offset of messages periodically.
  • Manual commit: Offers more control over when offsets are committed, typically at the end of a successful message processing batch.

Reading Messages from a Specific Offset

Consumers can start reading messages from a specific offset. This feature is useful for many scenarios, such as replaying messages for debugging or processing data historically. Below is an example in Java using the Kafka consumer API:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("enable.auto.commit", "false");
5props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7
8try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
9    TopicPartition partition = new TopicPartition("your-topic-name", 0);
10    consumer.assign(Collections.singletonList(partition));
11    consumer.seek(partition, 1024); // start reading from offset `1024`
12
13    while (true) {
14        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
15        for (ConsumerRecord<String, String> record : records) {
16            System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
17        }
18    }
19}

In this example:

  • KafkaConsumer is configured and connected to a Kafka broker.
  • The consumer is assigned a specific partition of a topic.
  • consumer.seek allows the consumer to start reading from a specific offset.

Key Considerations

FactorDescription
Offset CommittingCommitting offsets appropriately ensures that messages are neither lost nor processed more than once.
Partition StrategyBalancing the partitions and understanding the relationship between partitions and consumer scalability is crucial.
Topic ConfigurationTopics should be configured properly for retention, segment file sizes, and more to optimize access and maintenance of the offsets.

Monitoring and Tooling

Effectively managing and monitoring offsets are facilitated by various Kafka management tools and UIs (like Confluent Control Center or Kafdrop). These tools provide visibility into consumer lag (how far behind a consumer is from the head of the log), which is essential for detecting issues in real-time data processing applications.

Conclusion

Managing offsets effectively is foundational for building robust, fault-tolerant Kafka applications. By understanding how to manually control and manipulate offsets, developers can have granular control over their message processing workflows, ensuring data integrity and system efficiency.


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.