Search code examples
kafka-producer-api

Kafka producer with message key and partition


I am curious to know which partition the messages will be written, if we provide the message key as well as the partition. Will the message be posted based on the partition key or to the specific partition in Apache Kafka?


Solution

  • If you provide the partition number in one of the constructor methods that allow you to supply the partition number in ProducerRecord object, then the partition number will be used by default.

    The key is only used if the partition number is NOT provided.

    Here's a couple of references suggesting this:

    1. A blog from Confluent that suggests this.
    2. A tutorial from learning journal.

    Edit - added one more tutorial reference .