02 / 05

How does a record key influence partition selection?

Difficulty: 3/10
Partitioning and ordering

The default partitioner hashes the key bytes modulo the partition count, so a stable key keeps related records in one partition and therefore in order

When a producer sends a record, the partition is chosen in this order: an explicit partition on the ProducerRecord wins; otherwise, if there is a key, the default partitioner computes murmur2 over the serialized key bytes, makes it positive, and takes it modulo the number of partitions; otherwise (null key) the producer spreads records using a sticky strategy, filling a batch for one partition before switching, which is how Kafka keeps batches large. The consequence is the important part: the same key always maps to the same partition as long as the partition count does not change, and a single partition preserves order. So choosing the key is choosing your ordering domain, for example orderId or accountId.

javascript
  1. 1

    Keyed vs null key: keyed records give per-key ordering and are required for log compaction, but can skew load if keys are uneven. Null-key records spread evenly but have no ordering relationship.

  2. 2

    Explicit partition vs key: setting the partition manually gives full control, but you take over balancing and break the key-to-partition contract for anyone relying on it.

  3. 3

    Custom partitioner: useful for known hot keys or locality rules, but every producer for that topic must use the same logic or ordering guarantees silently break.

  4. 4

    Common mistake: expecting the mapping to survive adding partitions. Once the count changes, existing keys can hash to a different partition, so old and new records for one key can be in different partitions and order across that boundary is lost.

  5. 5

    Common mistake: using a key whose serialized bytes differ for equal logical values (for example different serializers, JSON field order, or case differences). Different bytes mean different hashes.

  6. 6

    Ordering also needs a healthy producer: with retries and max.in.flight.requests.per.connection above 1 and no idempotence, retried batches can reorder. Idempotence is the default in clients 3.0 and later and preserves order with up to 5 in-flight requests, but check older clients and explicit configs.

  7. 7

    Version note: the null-key strategy changed across releases (sticky partitioning from 2.4, an adaptive variant in 3.3+), so avoid asserting exact distribution behavior without checking your client version.

Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.