Scrivio Interview library
ConceptFoundations5 min read · updated 13 November 2026

Every record a producer sends must go to one partition of the topic, and the rule that picks the partition decides both ordering and how evenly load spreads.

In current Java producers, how is the partition chosen for (a) a record with a key, (b) a record without a key, and (c) a record with an explicit partition? What changed in Kafka 3.3 for records without keys?

An explicit partition in the ProducerRecord is always used. With a key, the built-in partitioner hashes the serialised key bytes with murmur2 and takes the result modulo the partition count, so a key keeps its partition as long as the count does not change. Without a key, the producer sticks to one partition for a while and then moves on; since Kafka 3.3 (KIP-794) this uniform sticky logic is built into the producer, it switches after about batch.size bytes, it sends less to slower brokers by default, and the old DefaultPartitioner and UniformStickyPartitioner classes are deprecated.

Why this question is asked

The partition a record lands in decides two things we care about: which records stay in order relative to each other, and how evenly work spreads across brokers and consumers. Most production bugs about 'out of order' events or 'one hot partition' start with a misunderstanding of this rule. Interviewers ask it to check that we know the three cases precisely and know what changed in recent versions. In this answer, we will recap partitions and ordering, then go through explicit partitions, keyed records and keyless records in turn, and finish with the settings that change the built-in behaviour.

1A Quick Recap: Why the Partition Matters

A topic is split into partitions, and Kafka guarantees order only within one partition: records in the same partition are stored and read in the order they were written. Records in different partitions have no ordering relationship. Each partition is consumed by exactly one consumer in a group, so the spread of records over partitions is also the spread of work over consumers.

So the partitioning rule has two jobs that pull in different directions. To keep related events in order, they must go to the same partition. To spread load, records should go to many partitions.

2Case (a) and (c): The Order of Decisions

The producer decides in a fixed order. If the ProducerRecord names a partition, that partition is used, no matter what key it has and no matter which partitioner is configured. If not, and a custom partitioner is configured with partitioner.class, that class decides. Otherwise the built-in logic decides, based on whether there is a key.

Step throughHow the Java producer picks a partition
  1. Partition set in the ProducerRecord?
    YesUse that partitionNoContinue
  2. Custom partitioner.class configured?
    YesThe custom partitioner decidesNoUse the built-in logic
  3. Record has a key (and keys not ignored)?
    Yesmurmur2(key bytes) mod partition countNoUniform sticky: current sticky partition

3 steps in order. Play them, or step through them one at a time.

3Case (b): A Record With a Key

For a keyed record, the producer takes the key after serialisation, as bytes, hashes those bytes with murmur2, makes the result positive and takes it modulo the number of partitions. The same key bytes and the same partition count always give the same partition, on every producer instance and every restart.

Java
// What the built-in logic does for a keyed record (simplified)
byte[] keyBytes = "acct-1002".getBytes(StandardCharsets.UTF_8);
int p = Utils.toPositive(Utils.murmur2(keyBytes)) % 12; // 3
int q = Utils.toPositive(Utils.murmur2(keyBytes)) % 16; // 3 here, but most keys move when the count changes
producer.send(new ProducerRecord<>("accounts", "acct-1002", event)); // partition 3 of 12
producer.send(new ProducerRecord<>("accounts", 7, "acct-1002", event)); // partition 7: explicit wins

Notice two consequences. First, the partition depends on the bytes, not the logical value, so two producers that serialise the same id differently will disagree. Second, changing the partition count changes the modulo, and most keys move to a different partition. Events for one key written before and after the change can then be read out of order, which is why adding partitions to a keyed topic needs care.

4Case (b): A Record Without a Key, and What 3.3 Changed

Long ago, keyless records went round-robin, one record per partition in turn. That spread load but produced many tiny batches, because each partition's batch got only every Nth record. Kafka 2.4 introduced the sticky partitioner: keep sending keyless records to one partition until its batch is sent, then pick another. Batches filled up, and requests fell sharply.

Kafka 3.3 (KIP-794) reworked this into the uniform sticky logic built into the producer itself. The producer now stays on a partition until about batch.size bytes have been sent to it, then switches, which spreads data evenly even when some brokers are slower. With adaptive partitioning on (partitioner.adaptive.partitioning.enable=true, the default), it chooses the next partition with a preference for brokers that have less data queued, so a slow broker gets fewer keyless records. partitioner.availability.timeout.ms, if set above 0, also lets the producer skip a broker that has stopped accepting data for that long. The old classes, DefaultPartitioner and UniformStickyPartitioner, were deprecated; leaving partitioner.class unset gives the new behaviour.

5Ignoring Keys on Purpose

Some teams set keys only for information, for example to make records easier to find, and do not need per-key ordering. For them, partitioner.ignore.keys=true makes keyed records use the sticky logic as well. Load spreads evenly and batches get bigger, but records with the same key can now land on different partitions, so per-key ordering is gone. It is a deliberate trade, not a performance tweak to apply everywhere.

Java
partitioner.ignore.keys=false # default: keys decide the partition
partitioner.adaptive.partitioning.enable=true # default: fewer keyless records to slow brokers
partitioner.availability.timeout.ms=0 # default: off; e.g. 5000 skips a stuck broker after 5 s

6Follow-up questions

If keys are hashed, why do some partitions still get more traffic?

Because traffic is rarely spread evenly across keys. Hashing spreads keys evenly, but if one account produces a hundred times more events than others, its partition receives far more data. Hot keys are a data problem, solved by choosing a finer key or isolating heavy entities, not by the hash function.

Does Kafka Streams use the same rule?

By default yes: Streams produces with the same murmur2-based logic, which is why topics joined by Streams must have the same partition count and the same key serialisation. If another producer writes one of those topics with a different rule, joins silently miss records.

7Conclusion

In this answer, we saw the three cases: an explicit partition always wins, a keyed record goes to murmur2 of its serialised key bytes modulo the partition count, and a keyless record follows the uniform sticky logic. Since Kafka 3.3, that logic is built into the producer, switches partitions after about batch.size bytes, adapts to slow brokers by default, and replaces the deprecated DefaultPartitioner and UniformStickyPartitioner. partitioner.ignore.keys trades per-key ordering for even load.

What to say in the interviewAbout 90 seconds aloud

Open with

There are three cases, and the producer checks them in order. If the ProducerRecord has an explicit partition, that partition is used, full stop. If there's a custom partitioner.class, it decides.

Then, if they want more

Otherwise the built-in logic looks at the key. With a key, it takes the serialised key bytes, hashes them with murmur2, makes the result positive and takes it modulo the number of partitions. So the same key always lands on the same partition, which gives per-key ordering, as long as the partition count doesn't change.

If you add partitions, most keys move, which can break ordering across the change. Also, it's the bytes that count, so two producers that serialise the same id differently will disagree. Without a key, the producer uses the sticky approach: it sends keyless records to one partition for a while, which makes full batches, and then moves on.

What changed in 3.3, with KIP-794, is that this uniform sticky behaviour moved into the producer itself: it switches after roughly batch.size bytes to a partition, and with adaptive partitioning, which is on by default, it sends fewer records to brokers that are slower. partitioner.availability.timeout.ms can skip a broker that stops accepting data. The old DefaultPartitioner and UniformStickyPartitioner were deprecated.

And if you don't need per-key order, partitioner.ignore.keys=true makes keyed records use the sticky logic too.

About this answer

Last updated
13 November 2026
Versions it names
Kafka 3.3, Kafka 2.4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In current Java producers, how is the partition chosen for (a) a record with a key, (b)...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/771d12f05c/