Sobes.tech
Senior

How does the consumer commit offset in different modes?

sobes.tech AI

Answer from AI

In Go, when working with Kafka, consumer offsets can be committed in different modes, which affect delivery guarantees and performance:

  • Auto Commit: offset is committed automatically at a set interval. This is convenient but can lead to message reprocessing on failures.

  • Manual Synchronous Commit: after processing a message, a commit method is called, which blocks until confirmation. It guarantees that the offset is saved but reduces performance.

  • Manual Asynchronous Commit: offset is committed in the background without blocking processing. Faster, but commit loss can occur on failure.

Example of manual commit using the sarama library:

consumer.MarkMessage(msg, "") // Mark message as processed
err := consumer.CommitOffsets() // Synchronous commit
if err != nil {
    // handle error
}

The choice of mode depends on reliability and performance requirements.