Consumer Group
Offset Management
Kafka
Programming
Data Processing

How to get last consumed offset for a consumer group?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In distributed systems that process data streams, managing how much information each part of the system has processed is critical. For Apache Kafka, a popular distributed event streaming platform, this tracking is handled via consumer offsets. Understanding how to retrieve these offsets for a consumer group is essential for monitoring and administrating your Kafka deployment. This involves using tools provided by Kafka and can also be executed programmatically.

Understanding Consumer Groups and Offsets

First, let's clarify some fundamental Kafka concepts:

  • Consumer: Part of an application that reads messages from Kafka topics.
  • Consumer Group: Collection of one or more consumers that together consume a topic. They share a group ID.
  • Offset: A sequential id tied to each message within a partition. It denotes the position of a consumer in a topic partition.

When consumers in a group read messages from a topic, they commit the offsets of messages they have processed. The last consumed offset is the latest position of the consumer in the log of a partition that the consumer has acknowledged processing effectively.

Retrieving Last Consumed Offsets

Using Kafka Command Line Tools

Kafka ships with a command-line tool known as kafka-consumer-groups.sh, which can list consumer groups, describe their status, and reset consumer offsets.

Here’s a step-by-step process to find the last consumed offsets using this tool:

  1. Open your terminal.
  2. Run the command below to get information on consumer groups:
bash
   kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group your-consumer-group-id

Replace localhost:9092 with your Kafka server's address and your-consumer-group-id with the actual consumer group ID.

This command provides output like the following:

 
   TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG     CONSUMER-ID                                     HOST            CLIENT-ID
   topic-name      0          102             103             1       consumer-1-blabla-uuid                         /127.0.0.1      consumer-1

Here, CURRENT-OFFSET is the last offset that the consumer has consumed.

Programmatically Using Kafka API

If you integrate this functionality into your applications, you can use Kafka’s Consumer API. Below is an example using the Java programming language:

java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import org.apache.kafka.common.TopicPartition;
3
4import java.util.Collections;
5import java.util.Properties;
6
7public class KafkaExample {
8    public static void main(String[] args) {
9        Properties props = new Properties();
10        props.put("bootstrap.servers", "localhost:9092");
11        props.put("group.id", "test");
12        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
13        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
14
15        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
16        TopicPartition partition = new TopicPartition("your-topic", 0);
17        consumer.assign(Collections.singletonList(partition));
18
19        // Seek to the end to find the latest offset
20        consumer.seekToEnd(Collections.singleton(partition));
21        long lastOffset = consumer.position(partition) - 1;
22
23        System.out.println("Last Consumed Offset: " + lastOffset);
24        consumer.close();
25    }
26}

Summary Table

TermDescription
ConsumerReads messages from Kafka topics.
Consumer GroupA group of consumers sharing a group ID to consume a topic collaboratively.
OffsetUnique identifier for each message in a partition, marking message's position.
CURRENT-OFFSETThe last message offset acknowledged by a consumer.
LOG-END-OFFSETThe offset of the latest message in the log + 1.

Conclusion

Knowing how to retrieve the last consumed offset for a Kafka consumer group is crucial for effectively managing a Kafka-based system. It helps in monitoring progress, troubleshooting issues, and ensuring data integrity through proper synchronization of consumer positions within a stream. Whether through command-line tools or directly within your code, Kafka provides flexible options tailored for diverse operational needs.


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.