How to consume messages between two timestamps using Kafka Console Consumer
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 powerful distributed event streaming platform capable of handling trillions of events a day. One common task when working with Kafka is consuming messages that fall between two specific timestamps. This capability is very useful for various scenarios such as data recovery, analysis, and auditing.
Understanding Time-Based Consumption in Kafka
Kafka utilizes two primary time concepts: event time and ingestion time. Event time is the actual time when the event occurred, as recorded by the producing application, whereas ingestion time is the time when the event was appended to the Kafka log.
To consume messages between two timestamps, it's important to have an understanding of Kafka's offset and how it relates to time. Each message in Kafka is assigned a unique sequential ID called an offset. Offsets allow Kafka consumers to keep track of which messages have been consumed. Kafka also stores a timestamp for each message.
Pre-Requisites
Before diving into the specifics of consuming messages between two timestamps, you will need:
- Apache Kafka (including the Kafka server and the Kafka CLI tools) installed and running.
- Knowledge of the topic from which you want to consume messages.
Using Kafka Console Consumer
The Kafka Console Consumer is a tool that allows you to read messages from Kafka topics from the command line. To specify the consumption between two timestamps, you can leverage the --offsets-for-times parameter.
Fetching Offsets for Times
The first step is to determine the offsets corresponding to your desired start and end timestamps.
Replace <timestamp> with the milliseconds since epoch of your target time. This command will return the earliest offset whose timestamp is greater than or equal to the given timestamp. Perform this for both your start and end timestamps.
Consuming Messages
Once you have the starting and ending offsets, you can consume messages as follows:
Here, <start-offset> is the starting offset corresponding to your start timestamp, and <number-of-messages> should be calculated as (end-offset - start-offset + 1).
Example
Imagine you want to consume messages from a topic called "sample-topic" between January 1, 2021, and January 2, 2021.
- Fetching Start Offset (January 1, 2021):
This might return an offset of 25.
- Fetching End Offset (January 2, 2021):
This might return an offset of 75.
- Consume Messages:
Summary Table
| Item | Description |
| Topic | The Kafka topic from which to consume messages. |
| Start Timestamp | Timestamp (in milliseconds) to start consuming messages. |
| End Timestamp | Timestamp (in milliseconds) to stop consuming messages. |
| Start Offset | Offset corresponding to the start timestamp. |
| End Offset | Offset corresponding to the end timestamp. |
| Number of Messages | Calculated as (end-offset - start-offset + 1). |
Conclusion
Consuming messages between two specific timestamps in Kafka involves calculating offsets for those timestamps and then using those offsets to limit message consumption. This operation can be invaluable for scenarios that require precision and specific time-sliced data analysis.

