Video in progress
Where does the timestamp on a Kafka message come from, what's the difference between `CreateTime` and `LogAppendTime`, and what does a wrong timestamp break?
By default it's CreateTime, where the producer sets the time; with LogAppendTime the broker sets the time it wrote the message. Timestamps are used by retention, offsetsForTimes() and Kafka Streams windows, so a wrong producer clock or a deliberately old or future time throws these mechanisms off.
📖 Answer
The topic-level setting:
message.timestamp.type=CreateTime # default
# or
message.timestamp.type=LogAppendTime
CreateTime is useful because it's close to when the event actually happened. If the producer sets the time itself in the ProducerRecord (for example, when importing old data), that time is kept.
Where the timestamp has an effect:
- Retention: segment deletion is based on the largest timestamp in the segment. If one message arrives with a time in the year 2030, the segment holding it won't be deleted for years.
- Search by time:
offsetsForTimes()and--reset-offsets --to-datetimerely on the.timeindex. If the times are out of order, "read from the start of yesterday" begins at the wrong place. - Kafka Streams: windows work on event time, so a record with a very old timestamp can be dropped as "late".
One project imported historical data with a CreateTime three years old; with 7-day retention, the broker started deleting it almost immediately.
Protection: since Kafka 3.6 the broker can reject messages with a timestamp that's too old or in the future, using log.message.timestamp.before.max.ms and log.message.timestamp.after.max.ms. I still keep the business time (occurredAt) as a separate field inside the payload; the Kafka timestamp is technical metadata.
Sign up to mark studied questions and save your progress.