- The producer is configured with one or more bootstrap server addresses. Those bootstrap servers are used to fetch cluster metadata (brokers, topics, number of partitions).
- Using that metadata, the producer applies a partitioning strategy to route each message to a partition.
- The default partitioning strategy for Kafka’s Java client hashes the key (using MurmurHash2 via
org.apache.kafka.common.utils.Utils.murmur2), converts the hash to a non-negative integer, and maps that integer to a partition using modulo arithmetic.
- Producer sends a message with a key and value.
- Producer fetches topic metadata (once or on change) from a bootstrap broker to learn the number of partitions.
- Producer computes
hash = murmur2(key)and converts to a positive integer (Kafka internally callstoPositive()to avoid negative values). - Producer computes:
- Producer sends the message to the broker that is the leader for that partition.

- MurmurHash2(keyA) = 10 →
10 % 3 = 1→ partition index 1 - MurmurHash2(keyB) = 15 →
15 % 3 = 0→ partition index 0 - MurmurHash2(keyC) = 22 →
22 % 3 = 1→ partition index 1 - MurmurHash2(keyD) = 35 →
35 % 3 = 2→ partition index 2
Note: Partition indices are zero-based (
0, 1, 2, …). When explaining partitions informally you may see them labeled 1, 2, 3, but Kafka internally uses 0-based indexing.
Best practices
- Use a meaningful key when ordering per entity (userId, sessionId) is required.
- Avoid putting high-cardinality or constantly-changing values as keys if you want even distribution.
- If a key becomes a hotspot, consider a composite key or additional sharding to distribute load across partitions.
- Monitor partition sizes and consumer lag to detect uneven distribution.
- Kafka producer partitioning: https://kafka.apache.org/ (search “partitioner” in the client docs)
- MurmurHash2 reference:
org.apache.kafka.common.utils.Utils.murmur2(Kafka Java client source)