Kafka - How to commit offset after every message using High-Level consumer?

Hongyi Li picture Hongyi Li · Aug 13, 2014 · Viewed 25.3k times · Source

I'm using Kafka's high-level consumer. Because I'm using Kafka as a 'queue of transactions' for my application, I need to make absolutely sure I don't miss or re-read any messages. I have 2 questions regarding this:

  1. How do I commit the offset to zookeeper? I will turn off auto-commit and commit offset after every message successfully consumed. I can't seem to find actual code examples of how to do this using high-level consumer. Can anyone help me with this?

  2. On the other hand, I've heard committing to zookeeper might be slow, so another way may be to locally keep track of the offsets? Is this alternative method advisable? If yes, how would you approach it?

Answer

Yueheng Li picture Yueheng Li · Apr 29, 2015

You could first disable auto commit: auto.commit.enable=false

Then commit after fetching the message: consumer.commitOffsets(true)