Sobes.tech
Junior — Middle

How can the fact of reading a message in Kafka be determined?

sobes.tech AI

Answer from AI

In Kafka, the fact that a message has been read is determined by offsets in the topic partitions. Each consumer tracks the offset up to which it has read messages in each partition.

To determine that a message has been read, you need to:

  • Ensure that the consumer has successfully processed the message.
  • Commit the offset of this message in Kafka, which means informing the broker that all messages up to this offset have been processed.

In Go, using a Kafka client (such as sarama or confluent-kafka-go), you can explicitly commit offsets after processing messages. If the offset is committed, the message is considered read and will not be delivered again to this consumer.

Example with sarama (simplified):

consumer.MarkMessage(msg, "") // Mark the message as read
consumer.CommitOffsets()       // Commit the offsets

Thus, the fact of reading is a successful commit of the offset in Kafka.