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
The program:
- Creates a
KafkaConsumerconfigured for the lookup. - Gets the topic's partitions.
- Builds
TopicPartitionobjects, which Kafka's APIs use to identify partitions. - Calls
endOffsetsto 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 Term | Description |
| Offset | A unique identifier of a record within a Kafka partition. |
| Latest Offset | The end of a partition's log: one past the newest message, i.e. the offset the next message will get. |
| Committed Offset | The position a consumer has committed after processing: the offset of the next record it will read. |
endOffsets Method | Part of Kafka's Consumer API used to retrieve the latest offsets for a list of partitions. |
Related reading
- kafka - ssl handshake failing
- Kafka - stop retrying on ConnectException
- Kafka - Stream vs Topic
- Kafka - This server is not the leader for that topic-partition
- Kafka - Unable to send a message to a remote server using Java
- Kafka - What are the better alternatives than poll() to listen to a topic in Java?
- Kafka - why new topic partition leader is not elected?
- Kafka 0.10 Java Client TimeoutException Batch containing 1 record(s) expired
Course lessons on this
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

Course
Beginner
27 lessons
10 hours
System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.