Producer

The Kafka producer is conceptually much simpler than the Consumer, since it doesn't have to worry about group coordination.

Once the target partition is determined (via the partitioner), the producer sends a produce request to the leader of that partition.

  • therefore, all writes to the partition must go through the partition leader.

Partitioner

When a producer sends a message to a Kafka topic, the partitioner is invoked to determine the target partition based on certain criteria.

  • therefore, the partitioner is responsible for determining which partition a message will be written to.

The purpose of a partitioner is to provide an even distribution of messages across the available partitions of a topic.

The out-of-the-box Kafka partitioners guarantee that all messages with the same key will be sent to the same partition.

  • of course, if the key is null, there is no longer a guarantee

The default partitioner (DefaultPartitioner) uses a round-robin strategy to distribute messages across partitions

A custom partitioner can be made in order to achieve specific business rules or other requirements

  • ex. it can consider the message key, specific message attributes, or external factors such as load balancing, with the goal being to ensure that related messages are written to the same partition, or to achieve a specific partitioning strategy that aligns with your application's needs.