confluentinc / confluent-kafka-go

Confluent's Apache Kafka Golang client
Apache License 2.0
4.67k stars 660 forks source link

[Question] The best way to efficiently produce messages while ensuring their order #1319

Open lcy0321 opened 1 month ago

lcy0321 commented 1 month ago

Hi Experts,

We are developing a service that requires the Kafka producer to support the following features:

I've read the INTRODUCTION.md of librdkafka and realize that the Idempotent Producer is close to our needs. However, there seems to be a very small chance that one or more messages in a queue could be lost due to a timeout. Is my understanding correct?

Currently, we are meeting these requirements by sending the messages synchronously, like this:

for {
    // Some retry logic here
    ...

    err := sendMessage(p, message)
    if err != nil {
        continue
    }
    break
}

func sendMessage(p *kafka.Producer, message *kafka.Message) error {
    deliveryChan := make(chan kafka.Event)
    err := p.Produce(message, deliveryChan)
    if err != nil {
        return err
    }
    e := <-deliveryChan
    // Check if the message was delivered; if not, return an error
    ...
    return nil
}

But producing messages one by one synchronously is heavily affecting the throughput.

Could you please provide recommendations for more efficient ways to meet these requirements?