I'm currently working on an application that partially relies on Apache Kafka (version 2.2.0). One thing I have to do is to keep track of what (and more importantly when) other consumers commit their current offsets. As far as I can tell, just using the Java client, there is no way to get the associated timestamps for the committed offsets because AdminClient's listConsumerGroupOffsets method eventually leads to an OffsetAndMetadata object, which does not include a timestamp. So instead, I simply started reading the messages from the __consumer_offsets topic. If there is a better way to do this, please let me know.
Now, if one reads the messages in __consumer_offsets directly, then one all of a sudden has two timestamps. One is the timestamp attached to the actual commit message and the other one is the commit_timestamp, which is part of the message's content. My first thought was that one of these is probably set by the broker and the other one might be set by the client that committed it (also, if you look at /config/topics/__consumer_offsets in ZooKeeper, it doesn't specify a LogAppendTime message timestamp, so one could assume that it just uses the default). Alas, a quick experiment with manually shifted system times shows that both are actually set by the broker. What's more, they don't always agree (the message's timestamp is sometimes slightly ahead of the commit_timestamp). I've tried to dive into the Kafka code to understand exactly what's going on but it is rather convoluted and I'm not familiar enough with it to quickly get a good grasp. So here are my questions:
- Why is the message timestamp in
__consumer_offsetsautomaticallyLogAppendTimeeven though that's not explicitly specified? Is it just that the producer that is used to send the commit message leaves the timestamp empty? - Why do the message timestamp and the
commit_timestampincluded in the message not agree? I seem to recall having read somewhere that it used to be possible to explicitly set thecommit_timestampand thereby manually control the retention of the committed offset. - More importantly: Is there any reason to use one over the other? For example, if it's still possible to set the
commit_timestampmanually, it would make much more sense to use the timestamp attached to the message.
I understand that this is a highly specific question and probably of little importance to most. But until now, I was always able to understand what's going on in the background by using Google and looking at Kafka's source code; yet, this one has me a bit stumped. So any insights are greatly appreciated.