Kafka Partition Strategy Guide
A Kafka partition strategy is the rule a producer uses to pick a partition for each record. Records with a key are hashed, so the same key always lands on the same partition. Records without a key are batched onto one partition at a time, which is called sticky partitioning. The strategy decides ordering, load balance, and how far consumers can scale.
How Kafka assigns a record to a partition
| Strategy | When it applies | How the partition is chosen | Ordering you get |
|---|---|---|---|
| Explicit partition | The producer sets a partition on the record | Used as given | Whatever you send to that partition |
| Key hash | The record has a key | murmur2 hash of the key bytes, modulo the partition count | Per key |
| Sticky (default) | No key and no partitioner configured | Fill a batch for one partition, then move to another | None across records |
| Round robin | RoundRobinPartitioner is configured | The next partition for every record | None |
| Custom | partitioner.class names your own class | Your code | Whatever your code guarantees |
The keyless case is the one that changed. Older guides say Kafka spreads keyless records round robin, one record at a time, and that was true until Kafka 2.4. KIP-480 replaced it with the sticky partitioner: the producer keeps sending to one partition until the batch is full or linger.ms expires, then picks another. Batches are fuller, so there are fewer requests and lower latency.
Kafka 3.3 revised it again with KIP-794. The original sticky logic sent more data to slow brokers, because their batches took longer to fill and the producer stayed on them longer. The built-in partitioner now switches after batch.size bytes have gone to a partition, and it can send less to brokers that are falling behind. The old DefaultPartitioner and UniformStickyPartitioner classes are deprecated. Leave partitioner.class unset to get the current behavior.
Choosing a partition key
Kafka only guarantees order within a partition, so the key should be the thing whose events must stay in order: an order ID, an account ID, a device ID. Every event for that entity then lands on one partition and is read in the order it was written.
The risk is skew. If one key produces far more traffic than the rest (one large tenant, one busy device), its partition becomes hot while the others sit idle, and the consumer on it falls behind. A composite key such as tenant plus entity ID spreads a large tenant across partitions while keeping order per entity. If nothing needs ordering, send records without a key and let the sticky partitioner balance the load.
Two details catch teams out. Compacted topics need a key, because compaction keeps the latest record per key. And clients do not all hash the same way: the Java client uses murmur2, while librdkafka (the library under the Python, Go, and .NET Confluent clients) defaults to a CRC32-based partitioner. If producers in different languages write to one topic, set librdkafka’s partitioner to murmur2_random so a key maps to the same partition from every client.
How many partitions a topic needs
Partitions cap consumer parallelism. Within a consumer group each partition is read by one consumer, so a topic with six partitions can keep at most six consumers busy, and any extra ones sit idle.
Size the count from throughput. Measure how much one partition can take from your producers and how much one consumer can process, divide your target throughput by each, and use the larger result with some headroom. More is not free: every partition adds open files and replication work on the brokers, and leader elections take longer after a broker failure.
You can add partitions to a topic later, but you can never remove them, and adding them changes which partition a key hashes to. Events for one key written before and after the change can sit on different partitions, which breaks per-key ordering across that boundary. For keyed topics it is worth provisioning for growth up front.
Writing a custom partitioner
A custom partitioner is worth the code when hashing alone gives the wrong distribution, most often to isolate a heavy key. This one reserves the last partition for a known large tenant and hashes everyone else across the rest. It assumes every record has a string key.
public class TenantPartitioner implements Partitioner {
private static final Set<String> HEAVY = Set.of("tenant-42");
@Override
public int partition(String topic, Object key, byte[] keyBytes,
Object value, byte[] valueBytes, Cluster cluster) {
int count = cluster.partitionCountForTopic(topic);
if (HEAVY.contains((String) key)) {
return count - 1;
}
return Utils.toPositive(Utils.murmur2(keyBytes)) % (count - 1);
}
@Override public void configure(Map<String, ?> configs) {}
@Override public void close() {}
}
Register it with partitioner.class in the producer config. Keep the logic deterministic: a partitioner that looks at wall-clock time or current load sends the same key to different partitions and gives up ordering.
Finding and fixing partition skew
Skew shows up as one partition with far more bytes in, or far more consumer lag, than its neighbors. kafka-consumer-groups.sh --describe --group <name> prints lag per partition, and the same numbers are available as broker and consumer metrics for a dashboard.
Once you know which key is hot, the fixes are the ones above: widen the key, give the heavy key its own partition, or drop the key if ordering was never needed. Adding consumers does not help, because the hot partition still has one reader.
On the consumer side, the assignment strategy decides which consumer reads which partitions. CooperativeStickyAssignor moves only the partitions that have to move during a rebalance, so the rest of the group keeps processing.
Why partition strategy decides Kafka performance
Throughput, ordering, and consumer scaling all come from how records map to partitions, and that mapping is hard to change once a topic holds data. Decide the key from your ordering requirement, size the partition count from measured throughput, and watch per-partition lag so skew is caught early. For the wider picture, see Kafka as a message queue, Kafka dead letter queues, and Kafka vs RabbitMQ.