Apache Kafka’s default partitioner hashes a record key and maps the result to a topic partition. Records with the same key usually land together, preserving their order within that partition. Without a key, Kafka spreads records across partitions, helping balance the flow.

Multiple Choice

By default, how is the target partition chosen for a record with a key in Kafka?

Kafka’s default partitioner uses a hash of the message key to pick the target partition. This hashing is done on the key’s bytes and the result is mapped to one of the available partitions (typically by taking the hash modulo the number of partitions). The payoff is that all records with the same key consistently land in the same partition, which preserves per-key ordering within a topic. If there is no key, the partitioner instead distributes messages across partitions in a round-robin fashion to balance load.

How Kafka Uses Record Keys to Choose Partitions

A Kafka record’s key may look like a small detail, but it can shape how data moves through an entire system. When a producer sends a keyed record without naming a partition, Kafka’s default partitioning logic uses a hash of the key to choose one. Records with the same key therefore tend to land in the same partition—a simple rule with a big effect on ordering and data processing.

Think of a topic as a set of parallel lanes. Each lane is a partition, and each partition stores records in order. The key helps Kafka decide which lane a record should enter. Let’s see how that choice works, and why it matters once applications begin reading the data.

The short version: the key is hashed

A Kafka producer first serializes the record’s key into bytes. The default partitioner then calculates a hash from those bytes and uses the result to select a partition. In common Kafka producer implementations, that selection is based on the hash and the number of available partitions.

That means a key such as customer-42 will usually direct records to the same partition whenever the producer sends them to the same topic. If the customer’s address changes, for example, several events about that customer can still be grouped together in one partition.

The hash isn’t a human-readable label, and it doesn’t tell Kafka what a record means. It’s a number calculated from the key’s bytes. Kafka uses that number as a routing aid. You can picture it as a sorting machine that turns each key into a lane assignment.

Why keep records with the same key together?

Kafka guarantees order within a partition, not across every partition in a topic. This distinction is easy to miss. A topic with six partitions doesn’t have one single ordering that spans all six; it has six separate ordered logs.

That’s where keyed partitioning becomes useful. Suppose an online shop sends events such as OrderCreated, PaymentReceived, and OrderShipped. If each event uses the order ID as its key, Kafka will generally put events for that order in the same partition. A consumer reading that partition can then see those events in the order Kafka stored them.

This does not mean Kafka automatically understands the business sequence or fixes events that arrive late. The producer still needs to send events in a sensible order, and the application must handle its own logic. But keeping records for one entity together gives downstream services a clear, ordered stream to work with.

The same idea appears in many settings: account updates keyed by account ID, device readings keyed by device ID, or messages keyed by a session ID. Pick a key that represents the thing whose history needs to stay together.

What happens when there’s no key?

A record can have a null key. In that case, the default partitioning behavior differs from keyed routing because there’s no key to hash. Kafka distributes such records among the topic’s partitions to spread the load.

The precise behavior can depend on the Kafka client version and producer configuration. Some descriptions refer to round-robin distribution; newer producer implementations may use a sticky approach, sending a batch of records to one partition for a short time before moving to another. The aim is similar: distribute data while allowing efficient batches.

That batching detail matters. Producers can send records more efficiently when they collect a group for the same partition before transmitting it. So “spread records evenly” doesn’t always mean “switch partitions after every single record.” If a topic’s partition distribution looks a little uneven over a short window, batching may be part of the reason.

The key’s bytes matter

Kafka hashes the serialized key, not an abstract idea of the key. The serializer turns the value into bytes, and those bytes feed the partitioning calculation. This is one reason key serialization deserves attention when multiple services produce to the same topic.

For example, two producers might both appear to use the text key customer-42. If one serializes it as UTF-8 text and another encodes it in a different format, the resulting bytes may differ. Different bytes can produce different hashes, which can send records to different partitions. The values may look identical in a log or dashboard, yet the routing result can diverge.

Using consistent serializers across producers helps avoid that surprise. It’s a small configuration choice with a direct impact on how records are grouped.

Same key, same partition—but not forever

With the same topic, key bytes, partitioning method, and partition count, a given key will generally map to the same partition. That consistency is what makes keyed routing useful.

But partition count is part of the calculation. If a topic gains or loses partitions, the mapping can change for some or many keys. Imagine a set of numbered mail slots: if you change the number of slots, the rule that maps names to slots may send those names somewhere new. Records already stored in the old partition don’t move automatically, either.

This can affect consumers that rely on per-key order. Before changing a topic’s partition count, teams should consider how their applications handle records for a key appearing in more than one partition over time. Kafka preserves order within each partition, but it cannot provide a single shared order across partitions.

When might a team choose another approach?

The default partitioner works well for many systems, but it isn’t the only option. A producer can explicitly choose a partition, or a custom partitioner can apply different routing rules. That can help when a system needs a special distribution pattern or has a well-understood data model.

Still, custom routing brings extra responsibility. If a partitioning rule sends too many records to one partition, that partition can become a hot spot. One consumer may end up handling far more data than others, while the rest sit comparatively quiet. A good key spreads work across partitions while keeping related records together—a balancing act, not always a perfect one.

Choosing a key also shapes the system’s view of “related.” A broad key such as country may group a huge amount of traffic into a small number of partitions. A highly specific key may spread traffic well but offer less useful grouping. The right choice depends on which records need a shared order and how the workload is distributed.

A small rule with lasting effects

Kafka’s default behavior for keyed records is straightforward: it hashes the serialized key and uses that hash to choose a partition. Records with the same key are routed consistently under the same partitioning conditions, helping preserve their order within that partition.

The rule is simple, but its consequences reach far beyond the producer. Keys influence how consumers share work, how applications follow an entity’s history, and how changes to a topic affect data flow. In Kafka, a key is more than a label attached to a record. It’s also a routing decision—one that can quietly shape the whole stream.