-
Notifications
You must be signed in to change notification settings - Fork 141
The best practice of handling offsets of asynchronous consuming? #106
Comments
You could use a channel to tell the main grouting about messages that have been completely processed. However, you should be careful with asynchronously processing messages. Because not all messages will take equally long to process, a message with offset 3 could be done before a message with offset 2. What do you want to do in this case?
|
Thanks for quick reply. I need to consume message at high concurrency, so I fork many goroutines to do that. Due to avoid the risk of offset management of asynchronous consuming message, I figure out a more safer way: commit the offset immediately, and if fails to upload the message, save it for re-consume. for {
select {
case m, ok := <- consumer.Messages():
if ok {
if err := consumer.CommitUpto(m); err == nil {
go func(m *sarama.ConsumerMessage) {
exportMessage := worker.Ingest(m)
// TODO: Uploads the consumed message. If fails to upload, saves the message for re-consume.
}(m)
}
}
}
} Is this a possible solution? |
The standard solution to consume faster is by having more partitions and more consumer instances. If that doesn’t work for you, it really depends on what kind of guarantees your application need. Willem
|
Yes, that may be a better approach. More standard and less complexity. Assumes that there are 2 partitions in topic1, would it look like the following? consumer1, consumerErr1 := consumergroup.JoinConsumerGroup("main-group", []string{"topic1"}, zookeeperNodes, kafkaConfig)
consumer2, consumerErr2 := consumergroup.JoinConsumerGroup("main-group", []string{"topic1"}, zookeeperNodes, kafkaConfig)
go func() {
for {
select {
case consumer1.Messages():
// Process message...
}
}
}()
go func() {
for {
select {
case consumer2.Messages():
// Process message...
}
}
}()
// ...... |
No, you just call |
Cleared. Thank you! |
Hi, thanks for sharing the great library!
I have a case as the following, the consumer consumes the message asynchronously, so the offset may not be committed serially. What is the best practice of handling offsets to guarantee the offset committing correctly?
The text was updated successfully, but these errors were encountered: