Junior — Middle
How does a Kafka producer choose a partition to send a message to?
sobes.tech AI
Answer from AI
The Kafka producer selects a partition for sending a message based on the message key and partitioning settings. If the message has a key, Kafka uses a hash function of this key to determine the specific partition, ensuring that all messages with the same key go to the same partition. If there is no key, the producer typically distributes messages across partitions in a round-robin manner to balance the load.
Example in Go using the sarama library:
producer, _ := sarama.NewSyncProducer(brokers, config)
msg := &sarama.ProducerMessage{
Topic: "my_topic",
Key: sarama.StringEncoder("userID123"), // key for partitioning
Value: sarama.StringEncoder("message payload"),
}
partition, offset, err := producer.SendMessage(msg)
Here, the key "userID123" will be used to select the partition so that all messages with this key go to the same partition.