Choosing a Kafka ordering key: route one entity’s records to one partition
Kafka guarantees order within a partition, not across an entire topic. With the default producer partitioner, records that share a key are assigned by key hash, so updates for one entity can share a partition and retain that partition’s order. Choose an ordering key that represents the business entity whose changes must stay ordered, not an arbitrary convenient ID.
Kafka guarantees order within a partition, not across an entire topic. With the default producer partitioner, records that share a key are assigned by key hash, so updates for one entity can share a partition and retain that partition’s order. Choose an ordering key that represents the business entity whose changes must stay ordered, not an arbitrary convenient ID.
What the key does and does not guarantee
A Kafka partition is an ordered log. By default, a record with a key is mapped using a hash of that key. Without a key, the default producer uses a sticky partition that can change after a batch threshold. Different partitions have no total topic-wide order. A key therefore groups records for partition assignment; it does not establish business causality, global ordering, or deduplication.
For example, use order_id when order updates must remain ordered. Using event_id instead may scatter events for the same order because each event can have a different ID.
Select a business key
Identify the smallest entity whose events need order, such as an order, account, or device. A single global sequence may conflict with Kafka’s partitioning and parallelism.
Serialize a stable key consistently across producers, for example tenant_id:order_id. Encode composite keys unambiguously; different serialized bytes may map differently.
Avoid null keys when per-entity ordering matters, unless a custom partitioner is explicitly verified.
Monitor hot keys. A popular tenant or entity can bottleneck one partition. Sharding the key may increase throughput but loses that entity’s single-partition order unless you add a reordering scheme.
Plan partition-count changes. With the default hash mapping, increasing partitions can move new records for a key to another partition, breaking assumptions that old and new records share one ordered log.
Preserve order in consumers too. Parallel work or out-of-order offset commits can reorder business effects even when records were read from one partition in order.
| Strategy | Benefit | Cost |
|---|---|---|
| Entity ID | Per-entity order with parallelism across entities | Hot entity can bottleneck |
| Event ID | Distributes event identities | Usually does not group one entity’s events |
| No key | Simple batching/distribution | No stable per-entity partition assignment |
| Explicit partition | Direct control | Client must manage distribution and scaling |
Verify the design
Log the producer key and serialized bytes; confirm same-entity records map to the same partition. Send ordered test records with one key and compare partition offsets. Review partition expansion in staging and check consumer parallelism, retries, and offset commits. These are verification steps, not tests performed for this article.
Sources and scope
[Apache Kafka Introduction](https://kafka.apache.org/documentation/) — partition ordering, keyed partitioning, and no topic-wide total order.
[Apache Kafka Producer Configs: partitioner.class](https://kafka.apache.org/41/configuration/producer-configs/) — default keyed hash and sticky partition behavior without a key.
[Apache Kafka Design: Producer load balancing](https://kafka.apache.org/44/design/design/#producer_loadbalancing) — semantic partition keys.
[Apache Kafka Basic Operations](https://kafka.apache.org/44/operations/basic-kafka-operations/) — partition expansion can change key distribution and ordering assumptions.
This covers Kafka default partitioning. Custom partitioners, client versions, producer transactions, and consumer processing require deployment-specific verification. A key alone does not guarantee exactly-once business effects or topic-wide order.