Kafka How to get last modified time for a topic i.e. last message added to any partition of the topic
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since it deals with streams of data, understanding when data (messages) are added is crucial for many applications, especially those dealing with real-time processing or monitoring changes. However, unlike traditional file systems or databases, Kafka does not explicitly store the timestamp of the last modification on a topic. Instead, timestamps can be associated with individual messages (records) when they are produced.
Understanding Kafka Timestamps
Kafka messages contain timestamps that serve various purposes such as event-time processing or log compaction policies. There are two types of timestamps in Kafka messages:
- Creation Time: The timestamp when the message was produced.
- Log Append Time: The timestamp when the message was appended to the log on the server.
By default, Kafka uses the Creation Time, which is set when the message is produced. However, it can be configured at the broker (server) level to use the Log Append Time instead.
Fetching Last Modified Time of a Kafka Topic
To determine the last modified time of a topic, one approach is to identify the last message added across all its partitions. Here's how you can achieve this using Kafka’s command-line tools and consumer APIs.
Using Kafka Command Line Tools
Kafka ships with a command-line tool called kafka-console-consumer. However, this tool doesn’t directly provide the last modified time but can help in consuming the latest messages where you can manually check the timestamp.
To get the latest messages and their timestamps:
Replace localhost:9092 with your Kafka cluster's bootstrap server address and my-topic with your topic name. The output will display the timestamp of the message depending on your broker configurations (Creation Time or Log Append Time).
Using Kafka Consumer API
For more granular control or automation, you can use Kafka’s Consumer API. Below is a simple example in Java:
This example demonstrates how to connect to a Kafka topic, navigate to the last message of a specified partition, and print its timestamp. To fully determine the last modified time across all partitions of a topic, you would need to iterate across all the topic’s partitions and compare the timestamps.
Key Points Summary
Here is a summary table of the key methods to determine the last modified time in Kafka:
| Method | Pros | Cons |
| Kafka Console Consumer | Easy to use; Quick setup | Manual; Less control; Not real-time |
| Kafka Consumer API | Programmatic control; Real-time access | Requires programming; More setup |
Conclusion
Kafka does not directly provide a last modified timestamp for topics, but the timestamp metadata on individual messages can be utilized to infer this information. Depending on the requirements—whether it's a one-time check or continuous monitoring—the Kafka command-line tools or Consumer API are both viable methods to get the data needed. Advanced users can leverage the Consumer API for more precise and real-time applications.

