Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What ordering does Kafka guarantee? A team sends events for the same customer without a key and wonders why a consumer sometimes sees CustomerUpdated before CustomerCreated. Explain and fix.

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

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.

Java
producer.send(new ProducerRecord<>("customers", null, created)); // no key -> partition 2 (current batch)
// batch for partition 2 is sent, producer sticks to a new partition
producer.send(new ProducerRecord<>("customers", null, updated)); // no key -> partition 5
Same events, without and with a key
EventNo key: partitionKey = customer id 42: partition
CustomerCreated 4224 (wherever the key "42" hashes)
CustomerUpdated 4254
CustomerUpdated 42 (again)04
With the key, every event for customer 42 is in partition 4 and keeps its order.

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.

Java
@Service
class CustomerEvents {
private final KafkaTemplate<String, CustomerEvent> kafka;
CustomerEvents(KafkaTemplate<String, CustomerEvent> kafka) { this.kafka = kafka; }
void publish(CustomerEvent e) {
kafka.send("customers", e.customerId(), e); // key = customer id -> same partition every time
}
}

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.

Step throughRetries with the idempotent producer
ProducerBroker (partition 4)
batch seq 0 (CustomerCreated)
batch seq 1 (CustomerUpdated)
seq 0 failed: not enough replicas
seq 1 rejected: out of order, expected 0
retry seq 0, then seq 1
both appended in order

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.

What to say in the interviewAbout 90 seconds aloud

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.
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/