Search code examples
apache-kafkakafka-producer-api

What is the purpose of timestamp in kafka ProducerRecord


We can specify a timestamp to the kafka ProducerRecord constructor.

public ProducerRecord(String topic, Integer partition, Long timestamp, K key, V value, Iterable<Header> headers)

What is the intended use of this timesamp? Is it transferred along with the message to the kafka broker?


Solution

  • The record also has an associated timestamp. If the user did not provide a timestamp, the producer will stamp the record with its current time. The timestamp eventually used by Kafka depends on the timestamp type configured for the topic.

    1. If the topic is configured to use CreateTime, the timestamp in the producer record will be used by the broker.
    2. If the topic is configured to use LogAppendTime, the timestamp in the producer record will be overwritten by the broker with the broker local time when it appends the message to its log.

    In either of the cases above, the timestamp that has actually been used will be returned to user in RecordMetadata

    Reference - kafka.apache.org