CodePark/ questions / Kafka: topics, partitions & brokers
START PRACTICINGPRACTICE
Backend question · Message queues & streaming › Kafka: topics, partitions & brokers

How does a Kafka producer decide which partition a record goes to, with and without a key?

The producer picks the partition in this order:

  • If you set the partition yourself, Kafka uses it.
  • If the record has a key, Kafka hashes the key and picks a partition from the hash.
  • If there is no key, the producer uses sticky partitioning.

With a key: the default partitioner (the code that chooses the partition) hashes the serialized key. The Java client uses the murmur2 hash. It then takes the hash modulo the number of partitions. So the same key always goes to the same partition, as long as the partition count does not change. This is what gives you ordering per key. For example, all events for user 42 stay in order.

Without a key: modern clients use sticky partitioning. The producer fills one batch for one partition, then moves to another partition. This gives bigger batches and better throughput than sending each record to the next partition in turn. Over time, the load is still spread across all partitions.

You can also plug in your own custom partitioner. Choose the key carefully either way. Some keys have few different values, or one value that is very common (a "hot" key). These keys put too much data on a few partitions, so the partitions become unbalanced.

  • A partition you set yourself overrides every other rule.
  • With a key, Kafka uses hash(key) modulo the partition count, so the same key goes to the same partition.
  • This key-to-partition mapping holds only while the partition count does not change.
  • Without a key, sticky partitioning fills one batch per partition, then switches to another partition.
  • Keys with few different values, or one very common value, create hot partitions.