Quick revision
The short answer
Kafka guarantees order only within a single partition, not across a topic. Without a key, the producer spreads records over partitions, so CustomerCreated and CustomerUpdated for the same customer can land on different partitions and be consumed in either order. The fix is to use the customer id as the record key, so all of a customer's events hash to the same partition and keep their order, as long as the partition count does not change.
What to remember
In this answer, we saw that Kafka guarantees order only within one partition. Records without a key are spread over partitions in sticky batches, so events for one customer can land on different partitions and be read out of order. Using the customer id as the key sends all of a customer's events to one partition, as long as the partition count stays the same. Listener concurrency keeps each partition on one thread, and the idempotent producer, enabled by default, prevents retries from reordering records.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Kafka guarantees order only within a single partition, not across a topic. Without a key, the producer spreads records over partitions, so CustomerCreated and CustomerUpdated for the same customer can land on different partitions and be consumed in either order. The fix is to use the customer id as the record key, so all of a customer's events hash to the same partition and keep their order, as long as the partition count does not change.
Why this question is asked
Ordering bugs like this one are confusing because they appear only now and then, and only under load. The producer sends events in the right order, the consumer reads them in the wrong order, and nobody changed anything. Interviewers ask it because it shows whether we know what Kafka actually promises and how keys connect to partitions. In this answer, we will recap partitions and offsets, see how records without a key are spread, reproduce the out-of-order read, fix it with a key, and check that consumer concurrency and producer retries do not break the order again.
1A Quick Recap: Order Lives in a Partition
A Kafka topic is split into partitions. Each partition is an append-only log: records are added at the end, and each gets the next offset, 0, 1, 2 and so on. A consumer reads a partition from lower offsets to higher ones, so within one partition records are always read in the order they were written. That is the only ordering Kafka guarantees.
Across partitions there is no order at all. Partitions live on different brokers and are read by different consumers or threads at different speeds. Record 10 on partition 0 and record 3 on partition 1 have no relationship that the consumer can see, even if one was produced a millisecond before the other.
2Where Records Without a Key Go
When a record has a key, the producer's default partitioner hashes the key's bytes with murmur2 and takes the result modulo the number of partitions. The same key always gives the same partition. When a record has no key, there is nothing to hash, so the producer chooses a partition itself.
Since Kafka 2.4, and in its current form since Kafka 3.3, keyless records use sticky partitioning: the producer fills a batch for one partition, sends it, and then switches to another partition for the next batch. That gives large, efficient batches, but it means two events sent a few milliseconds apart can easily fall into different batches and therefore different partitions.
| Event | No key: partition | Key = customer id 42: partition |
|---|---|---|
CustomerCreated 42 | 2 | 4 (wherever the key "42" hashes) |
CustomerUpdated 42 | 5 | 4 |
CustomerUpdated 42 (again) | 0 | 4 |
Now the two events sit on different partitions. If the consumer of partition 5 happens to be ahead of the consumer of partition 2, it sees CustomerUpdated before CustomerCreated, and the update fails or creates a half-empty customer.
3The Fix: Key Every Event by the Customer Id
We make the customer id the record key. Every event for customer 42 then goes to the same partition, so it is appended there in the order sent and read in that order. Different customers still spread over all partitions, so throughput is not lost.
There is one condition: the partition count must stay the same. The mapping is hash modulo the number of partitions, so going from 6 to 12 partitions moves many keys to new partitions. Events written after the change go to the new partition while older ones are still waiting on the old one, and for a short time order can break again. So we choose the partition count with growth in mind, and treat a change as a planned migration.
4Does Consumer Concurrency Break the Order?
A common worry is that running the listener with concurrency = 3 means three threads, so events must get mixed up. They do not. Spring Kafka's ConcurrentMessageListenerContainer creates three consumers, and Kafka assigns each partition to exactly one consumer in the group. Each consumer processes its partitions on its own single thread, one record after another.
So partition 4, with all of customer 42's events, is always handled by one thread in order. Order can only break if our own code hands records to another thread pool, for example by calling an @Async method or submitting to an executor inside the listener. That parallelism has to be keyed too, or avoided.
5Can Producer Retries Reorder Records?
There is one more way to break order inside a partition: retries. A producer may have several batches in flight to the same partition, up to max.in.flight.requests.per.connection, which defaults to 5. If batch 1 fails with a temporary error and batch 2 succeeds, a plain retry of batch 1 would land after batch 2.
The idempotent producer solves this. The broker tracks a producer id and a sequence number per partition and only accepts batches in sequence, so a retried batch takes its correct place. Idempotence has been on by default since Kafka 3.0 (enable.idempotence=true, with acks=all), and it keeps order with up to 5 in-flight requests. So with modern defaults retries are safe; we just must not turn idempotence off or set acks=1 without understanding this.
6 messages in order. Play them, or step through them one at a time.
The broker refuses to append a batch ahead of the one before it.
6Follow-up questions
What if two services publish events for the same customer?
Each producer keeps its own order, but there is no order between two producers. If both publish CustomerUpdated for customer 42 at nearly the same moment, the broker's arrival order decides. When that matters, put a version number or timestamp from the source of truth into the event and let the consumer ignore older versions.
Should we use a single partition to get total order?
Only for very low-volume topics. A single partition can be read by only one consumer in a group, so throughput is capped. Most systems need order per entity, not total order, and keys give exactly that while still scaling.
Why did the bug appear only sometimes?
Because with sticky partitioning two events for the same customer go to the same partition when they fall into the same batch, and to different partitions when a batch boundary falls between them. It also depends on how far behind each partition's consumer is at that moment, which varies with load.
7Conclusion
In this answer, we saw that Kafka guarantees order only within one partition. Records without a key are spread over partitions in sticky batches, so events for one customer can land on different partitions and be read out of order. Using the customer id as the key sends all of a customer's events to one partition, as long as the partition count stays the same. Listener concurrency keeps each partition on one thread, and the idempotent producer, enabled by default, prevents retries from reordering records.
Open with
Kafka only guarantees ordering within a partition. Across partitions of the same topic there's no order, because they are read by different consumers at different speeds. In this case the team sends events without a key, so the producer chooses the partition itself.
Then, if they want more
With the default sticky partitioner it fills a batch for one partition and then switches, so CustomerCreated and CustomerUpdated for the same customer can end up on different partitions, and if the consumer of the second partition is ahead, it sees the update first. The fix is to use the customer id as the record key.
The default partitioner hashes the key, so all events for that customer go to the same partition and are read in the order they were written, while different customers still spread across partitions. The one caveat is that this mapping depends on the partition count, so adding partitions later moves keys and needs to be planned.
On the consumer side, a listener with concurrency greater than one is still fine, because each partition is assigned to one consumer thread and processed sequentially; I'd just avoid handing records off to another thread pool.
And on the producer side, retries could reorder batches in flight, but the idempotent producer, which is on by default since Kafka 3.0, uses sequence numbers so the broker keeps them in order.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Kafka 2.4, Kafka 3.0, Kafka 3.3
- 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, Spring for Apache Kafka reference
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 "What ordering does Kafka guarantee? A team sends events for the same customer without a...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/d6fbb5c0b8/