Kafka also gives its users the ability to specify their own custom logic for how partitioning should take place. Users can specify the property partition.class when creating the producer object, similar to how the key and value serializer properties are specified. There are many possible reasons to have a custom partitioner, the most notable of which is balancing load across partitions. For instance, say we use a customer’s home country as the message key and 30% of our customers are from the USA alone. The partition with US customers will be overburdened as it will receive messages for US customers as well as messages for customers from other countries whose keys hash to the same partition. To alleviate such a situation, we can write a custom partitioner that directs all US data to a particular partition. The customer partitioner class must implement the interface org.apache.kafka.clients.producer.Partitioner.
An example implementation of the partitioner for the scenario we discussed appears below: