Is there a way to get the last message from Kafka topic?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. One common query with Kafka is how to retrieve the last message from a topic. Here, we will explore various methods to achieve this, along with technical explanations.
Understanding Kafka Topics and Partitions
Before we delve into retrieving messages, it's essential to understand the basic structure of Kafka. Kafka stores streams of records (messages) in categories called topics. Every topic is split into partitions, which allows the data to be distributed and parallelized across multiple brokers—and replicated for fault tolerance.
Each message in a partition is assigned a sequential ID number known as the offset, which uniquely identifies each record within the partition.
Retrieval of the Last Message
To retrieve the last message of a Kafka topic, you need to understand that Kafka’s design does not inherently provide an easy method to get the last message directly, owing to its nature as an append-only log with a focus on streaming data continuously. However, there are a few methods to approach this problem:
Method 1: Using Consumer Offsets
Kafka consumers track their offsets. By setting the offset to the end of the log, you can read the last message:
- Create a Kafka consumer: Configure it properly to connect to your Kafka cluster.
- Seek to the end: Kafka API provides a method
seekToEnd()that can be used to move the consumer’s position to the end of the partition. - Step back by one: Once you seek to the end, step back one offset to read the previous message, the last available message.
Here's a simplified code snippet using Java:
Method 2: Kafka Admin Client
The Kafka Admin Client can be used to fetch metadata about topics, including the current offset range of partitions.
- Instantiate the Admin client with proper configurations.
- Fetch the end offsets of a topic’s partitions using
listOffsets()where you specify theOffsetSpec.latest()spec.
However, using this method doesn't directly fetch the message but gives you the information about where the last message is, enabling your consumer configuration accordingly.
Summary
Here is a brief summary of the key points in retrieving the last message from a Kafka topic:
| Method | Description | Considerations |
| Consumer offsets | Use consumer's seekToEnd method | Direct and practical but alters offset |
| Kafka Admin Client | Use AdminClient to fetch last available offsets | Indirect, fetches offsets, not messages |
Additional Details
- Performance: Note that constantly fetching the last message can be resource-intensive, especially in large or high-throughput environments.
- Consumer Groups: Make sure to manage consumer groups correctly if you're using this method in production as it could affect message consumption of other consumers in the same group.
Retrieving the last message in Kafka can be seen as unconventional, but with the right configuration and understanding of Kafka’s architecture, it’s certainly achievable for specific use cases. Remember, the methods suggested can potentially impact Kafka's performance and should be used judiciously.
Related reading
- Is there a way to make Celery/RabbitMQ persistent?
- Is there a way to manually set an ElasticSearch document id when inserting via AWS Kinesis Firehose?
- Is there a way to prioritize messages in Apache Kafka 2.0?
- Is there a way to produce Kafka messages with headers using Kafka Confluent REST API?
- Is there a way to set a delay for a message sent by a kafka producer?
- Is there a way to stop Kafka consumer at a specific offset?
- Is there a way to use confluent Kafka Dotnet JSON serializer WITHOUT schema registry,
- Is there an equivalent of ping for RabbitMQ? How can I diagnose whether an exchange or queue is broadcasting?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.