Apache Kafka
Kafka Offset
Message Brokers
Data Streaming
Kafka Tutorials

Kafka: simplest way to get the latest offset of a topic

Codemia Knowledge Hub ·

Use the consumer API: list the topic's partitions with partitionsFor(), then call KafkaConsumer.endOffsets() on them. It returns a map from each TopicPartition to its end offset.

Latest offset vs committed offset

Each record in a Kafka partition has a sequential ID called an offset, which uniquely identifies it within that partition. Two offsets matter when consuming:

  • Latest (end) offset: the end of the partition's log. It is the offset the next message written will get, one past the newest message.
  • Committed offset: the position a consumer has recorded as processed, stored as the offset of the next record it should read. It usually lags behind the end offset.

Fetching the end offsets in Java

java
1import org.apache.kafka.clients.consumer.*;
2import org.apache.kafka.common.*;
3import java.util.*;
4
5public class LatestOffsetFetcher {
6    public static void main(String[] args){
7        String topic = "your-topic";
8        Properties props = new Properties();
9        props.put("bootstrap.servers", "localhost:9092");
10        props.put("group.id", "test-group");
11        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
12        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
13        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
14
15        // Get the partitions of the topic
16        List<PartitionInfo> partitionInfos = consumer.partitionsFor(topic);
17        List<TopicPartition> partitions = new ArrayList<>();
18
19        for(PartitionInfo partition : partitionInfos){
20            partitions.add(new TopicPartition(partition.topic(), partition.partition()));
21        }
22
23        // Assign to all partitions
24        consumer.assign(partitions);
25
26        // Query the latest offsets for these partitions
27        Map<TopicPartition, Long> latestOffsets = consumer.endOffsets(partitions);
28
29        latestOffsets.forEach((tp, offset) -> {
30            System.out.printf("Latest offset in partition %d of topic %s is: %d%n",
31                              tp.partition(), tp.topic(), offset);
32        });
33
34        consumer.close();
35    }
36}

The program:

  1. Creates a KafkaConsumer configured for the lookup.
  2. Gets the topic's partitions.
  3. Builds TopicPartition objects, which Kafka's APIs use to identify partitions.
  4. Calls endOffsets to get the latest offset of each partition.

What the latest offset is used for

  • Data freshness: the consumer can tell whether it is processing the most recent data.
  • Lag monitoring: the distance between the committed offset and the latest offset is the consumer's lag, how far behind current data it is.
  • Fault tolerance: after a consumer fails, another process can resume from the last committed offset up to the latest known offset.

Configure consumers with sensible timeouts and retry policies, and monitor offset lag regularly so bottlenecks and failures in the pipeline show up early.

Terms

Key TermDescription
OffsetA unique identifier of a record within a Kafka partition.
Latest OffsetThe end of a partition's log: one past the newest message, i.e. the offset the next message will get.
Committed OffsetThe position a consumer has committed after processing: the offset of the next record it will read.
endOffsets MethodPart of Kafka's Consumer API used to retrieve the latest offsets for a list of partitions.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design