Kafka
Data Consumption
Timestamp
Data Streaming
Kafka Consumer API

Kafka How to consume data based on Timestamp

System Design practice on Codemia

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

Practice system design

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. One of Kafka's notable features is the ability to consume messages starting from a specific point in time. This feature is particularly useful for applications that require replaying historical data or recovering from downtime without processing all previous messages again.

Understanding Kafka Time-Based Consumption

Kafka brokers store records in topics which are split into partitions. Each partition is an ordered, immutable sequence of records that is continually appended to. Records in a partition each have a unique offset, as well as a timestamp. The timestamp of a Kafka message can either be set explicitly by the producer or automatically by the broker when it appends the message to a partition.

When consuming messages from a Kafka topic, typically consumption starts from an offset. However, Kafka also allows consumers to begin consuming messages based on a timestamp. This can be particularly useful in scenarios where messages are related to time-sensitive data.

Fetching Messages from a Specific Time

To consume messages from a specific point in time, Kafka offers two main approaches:

  1. Using Consumer API to Seek by Timestamp: Kafka’s Consumer API allows you to look up offsets by timestamp using the offsetsForTimes method. This method takes a map of topics to the respective timestamp from which you want the offsets. Here’s how you can use this feature:
java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test");
4props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
5props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6
7try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
8    String topic = "your-topic";
9    Long targetTime = System.currentTimeMillis() - 24 * 3600 * 1000; // 24 hours ago
10
11    // Query Kafka for offsets matching or after the target timestamp within the desired topic
12    Map<TopicPartition, Long> query = new HashMap<>();
13    consumer.partitionsFor(topic).forEach(partitionInfo -> {
14        query.put(new TopicPartition(topic, partitionInfo.partition()), targetTime);
15    });
16
17    Map<TopicPartition, OffsetAndTimestamp> result = consumer.offsetsForTimes(query);
18
19    // Seek to the returned offsets and begin consuming
20    result.forEach((partition, offsetAndTimestamp) -> {
21        if (offsetAndTimestamp != null) {
22            consumer.assign(Collections.singletonList(partition));
23            consumer.seek(partition, offsetAndTimestamp.offset());
24        }
25    });
26
27    // Continue with consumption as usual
28    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
29    // process records
30}
  1. Administrative Tools: Kafka also provides administrative tools like kafka-console-consumer which can be used to consume messages from a specific time. For example:
bash
kafka-console-consumer --bootstrap-server localhost:9092 --topic your-topic --offsets-by-time <timestamp-in-milliseconds>

In this command, <timestamp-in-milliseconds> represents the epoch timestamp from which you want to start consumption.

Implications and Use Cases

Consuming messages based on timestamps have several implications and use cases:

  • Reprocessing Historical Data: Perfect for scenarios where historical data needs to be reprocessed, ensuring exactly the same data input.
  • Fault Recovery: In cases of failure, systems can restart processing from the last known good state, identified by timestamp.
  • Event Synchronization: Useful in systems requiring synchronization of events occurring simultaneously across different systems.

Summary Table

FeatureDescription
Offset Lookup by TimestampAllows starting message consumption from the exact time.
Flexible ReplayUseful for scenarios where only specific periods of data are required.
Fault RecoverySystems can return to a known good state using timestamp-based offsets.
Ease of UseAPI and command-line support make it accessible.

Conclusion

Kafka's ability to consume data based on timestamps offers flexibility and precise control, enabling robust data processing applications. Whether you are building a system that requires fault tolerance, historical data replay, or event synchronization, understanding and utilizing Kafka’s time-based consumption features is crucial for building efficient and reliable streaming data applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track 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.

Practice system design

All Rights Reserved.