question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consumer.seek commits an offset, causing possible data and metadata loss

See original GitHub issue

In debugging one of our stream processing tasks we noticed that consumer.seek commits an offset in order to affect the consumer’s fetching position. This mixes controlling the consumer position with recording progress and can lead to offset-metadata getting lost.

Offset metadata can be very useful when working with replicating to and from local stores, especially for achieving exactly-once processing, and is used by KafkaStreams for exactly this purpose (emphasis mine):

  • When committing a task, after the state stores have all been flushed and before produce write the committed offset, we can choose to trigger the persistent state stores’ checkpoint mechanism to make a checkpoint for each persistent stores.
  • When writing the committed offset into the input-offset-topic, also pass in the reference (e.g. local state directory / files locations) of all the persistent store checkpoints into the offset metadata as part of the state triplet.
  • Then upon resuming, if the fetched last transaction contains the state store checkpoints information, we can skip the restoration step of the state stores from changelog but use the checkpointed image directly.

When seek commits an offset, this metadata is subsequently lost.

Another example is where you produce to the same topic that you’re consuming (eating your own tail), which is useful for things like implementing uniqueness constraints (described well here). There it can often be useful to seek back 1 from the last committed offset, to kickstart the process (as if you don’t, no messages are ever produced for you to continue from), especially when doing things like replicating from a store to a Kafka topic.

As far as I can tell, the Java KafkaConsumer does not commit the offset, but only changes the offset in memory so the next fetch happens from there.

Proposed solution

Eventually, I think we’d want to be in a situation where:

  • consumer.seek only affects what message is fetched next, not how progress has been made thus far.
  • consumer.commit can be used to record progress, possible after a seek, if that’s required. See #378.

However, this would constitute a breaking change, despite the committing of the offset is currently a bit of a side effect. So perhaps a flag passed to consumer.seek would be a way to opt-in to the new behaviour, as a way to verify the approach in real applications. That way if the API is to be broken in a new major version, we prevent the need for a revert.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Nevoncommented, Jan 28, 2021

@Nevon would the kafkajs project take sponsorships for this development work?

Thank you very much for offering! I’m personally not set up to take on contract work, but I’ll do you one better and put up a free PR . #1012

If you want to support my work, consider becoming a sponsor.

2reactions
Nevoncommented, Feb 1, 2021

The fix for this will be out in 1.16.0-beta.7 in a few minutes. Let me know if it solves your problem, @angelozerr.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Kafka's Consumer Auto Commit Configuration Can Lead ...
We take a look at how a hard shut down on your consumer service can lead to data loss or duplication if you're...
Read more >
Chapter 4. Kafka Consumers: Reading Data from Kafka
The alternative is “earliest,” which means that lacking a valid offset, the consumer will read all the data in the partition, starting from...
Read more >
KafkaConsumer (kafka 2.2.0 API)
A client that consumes records from a Kafka cluster. This client transparently handles the failure of Kafka brokers, and transparently adapts as topic ......
Read more >
Consuming messages - IBM Cloud Docs
If the committed offset overtakes the processing of the messages and a consumer failure exists, it's possible that some messages are not processed....
Read more >
KafkaConsumer (clients 3.3.1 API) - javadoc.io
The consumer can either automatically commit offsets periodically; ... have it be the case that even if a crash occurs that causes unsync'd...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found