Without a key, the producer uses the sticky partitioner (Kafka 2.4+): it sticks to one partition until the batch is full, then switches. This maximizes batching efficiency vs round-robin. With a key, the default partitioner uses murmur2 hash of the key modulo partition count. Custom partitioners implement the Partitioner interface. Note: partition count changes will cause the same key to map to a different partition!
Key configuration
partitioner.class=org.apache.kafka.clients.producer.internals.DefaultPartitioner
partitioner.ignore.keys=false — respect key-based routing