Producers publish messages to a topic, which appends each message to the end of a partition. By default, if a message contains a key, the hashed value of the key is used to decide which partition receives the message. If the key is null, a round-robin algorithm balances the storage of messages across all partitions.
Kafka guarantees that as long as no new partitions are added, elements with the same key are stored in the same partition when using the default partitioner.
You can also implement a custom partitioning strategy to route messages to specific partitions using custom logic.