How to extract timestamp embedded in messages in Kafka Streams
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kafka Streams uses a timestamp for every record, and that timestamp drives event-time features such as windows, joins, and suppression. If the meaningful event time is stored inside the record payload rather than in the Kafka record metadata, you need a custom TimestampExtractor.
Why Embedded Timestamps Matter
A producer record already has a Kafka timestamp, but that value may represent producer time or broker append time. In many streaming systems, the real business event time lives inside the message body, such as an eventTime field serialized in JSON.
If you use the wrong timestamp source, your windows and aggregations can still run, but they will reflect the wrong notion of time.
Implement a Custom TimestampExtractor
Kafka Streams lets you plug in your own extractor by implementing TimestampExtractor. The method receives the ConsumerRecord and the current partition time.
The extractor must return milliseconds since the Unix epoch. That is the contract Kafka Streams expects.
Register the Extractor in Streams Config
After implementing the extractor, wire it into the application configuration with StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG.
With this in place, source records will be timestamped from the embedded field instead of from the default record timestamp behavior.
A Minimal Topology
The topology itself can remain simple. The extractor changes the time semantics without changing the rest of the DSL code.
If your payload timestamps are correct, these five-minute windows now use event time from the message body.
What to Do With Bad Timestamps
This is the most important design decision. Kafka Streams allows several behaviors:
- throw an exception and fail fast
- return a fallback value
- return a negative value and let the record be skipped
Fail-fast behavior is often the safest default because silently skipping bad timestamps can hide data quality problems. If you do estimate a value, use that choice deliberately and document the rule clearly.
Performance Considerations
The extractor runs for every source record, so keep it lightweight. Parsing full JSON for each message may be acceptable at moderate throughput, but if performance matters, use an efficient serializer or store the timestamp in a strongly typed value object that avoids repeated generic parsing.
The extractor should also be stateless. Kafka's contract assumes it can be reused safely without holding record-specific mutable state.
Common Pitfalls
One common mistake is returning seconds instead of milliseconds. Kafka Streams expects epoch milliseconds, and getting that wrong shifts windows dramatically.
Another issue is using a default extractor accidentally because the custom class was not registered in the config or was registered under the wrong property name.
Developers also overlook what happens to downstream topics. Record timestamps can propagate into changelogs and internal topics, so nonsensical extracted values can affect retention and stream-time behavior beyond one operator.
Finally, do not bury parsing failures. If the payload schema changes and the extractor quietly invents timestamps, the topology may keep running while producing misleading results.
Summary
- Use a custom
TimestampExtractorwhen the real event time is embedded in the message payload. - Return epoch milliseconds, not seconds or formatted strings.
- Register the extractor with the Kafka Streams timestamp extractor config.
- Choose an explicit policy for malformed or missing timestamps.
- Keep the extractor stateless and efficient because it runs for every source record.
Related reading
- How to fanout an AWS kinesis stream?
- how to fetch a field in ConsumerRecord
- How to fetch offset id while consuming Kafka from Spark, save it in Cassandra and use it to restart Kafka?
- How to fetch recent messages from Kafka topic
- How to filter messages from Kafka based on headers value in AWS lambda?
- how to find consumer group coordinator in kafka?
- How to find RabbitMQ URL?
- How to find the root cause of high CPU usage of Kafka brokers?

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.