How does Kafka guarantee message ordering?
Learn how Kafka guarantees message ordering per partition, why message keys matter, and how idempotent producers prevent retry reordering.
Expected Interview Answer
Kafka guarantees ordering only within a single partition, not across an entire topic: messages appended to one partition keep a strict, monotonically increasing offset and are delivered to consumers in exactly that order.
Because a topic is split into many partitions for parallelism, Kafka cannot order messages globally. Ordering is preserved per partition, so records that must stay in sequence have to land in the same partition — typically by giving them the same message key, which the default partitioner hashes to a fixed partition. On the producer side, enabling idempotence (and keeping max.in.flight.requests.per.connection at or below 5 with retries) prevents reordering caused by retried batches.
- Deterministic per-key sequencing
- High throughput via parallel partitions
- Predictable replay during recovery
- Simple keying model for related events
- Works with idempotent producers to avoid retry reordering
AI Mentor Explanation
Think of each partition as one over bowled by a single bowler: the six deliveries are numbered and always recorded in the exact sequence they were bowled, ball one before ball two. Kafka guarantees that within-over order perfectly. But if you split an innings across several bowlers bowling at once, you cannot say which stadium-wide delivery came first — so to keep a batter's balls in order, you send them all to the same bowler, the way a message key pins related records to one partition.
Step-by-Step Explanation
Step 1
Understand the partition boundary
Ordering is guaranteed only within a partition; a topic with many partitions has no global order.
Step 2
Choose a message key
Assign a key (e.g. customerId, orderId) so all related records hash to the same partition.
Step 3
Rely on the partitioner
The default partitioner maps identical keys to the same partition, keeping their sequence intact.
Step 4
Enable idempotent producers
Set enable.idempotence=true so retried batches do not duplicate or reorder within a partition.
Step 5
Bound in-flight requests
Keep max.in.flight.requests.per.connection <= 5 with retries so a failed batch cannot be overtaken.
What Interviewer Expects
- Ordering is per-partition, not per-topic
- Role of the message key and partitioner
- How idempotence prevents retry reordering
- Trade-off between ordering and parallelism
- Why a single consumer per partition preserves order
Common Mistakes
- Claiming Kafka orders an entire topic globally
- Forgetting that keyless messages spread across partitions
- Ignoring reordering from retries without idempotence
- Assuming multiple consumers in a group keep per-key order across partitions
Best Answer (HR Friendly)
“Kafka keeps messages in order inside each lane, called a partition, but not across the whole topic. If some events must stay in order, you tag them with the same key so they all go into the same lane, and Kafka delivers them in exactly the sequence they arrived.”
Code Example
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("enable.idempotence", "true");
props.put("max.in.flight.requests.per.connection", "5");
props.put("acks", "all");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
// Same key -> same partition -> guaranteed order for this order id
producer.send(new ProducerRecord<>("orders", "order-42", "CREATED"));
producer.send(new ProducerRecord<>("orders", "order-42", "PAID"));
producer.send(new ProducerRecord<>("orders", "order-42", "SHIPPED"));
producer.flush();Follow-up Questions
- What happens to ordering if you increase a topic's partition count later?
- How does enable.idempotence prevent reordering during retries?
- Why can two consumers in a group not preserve global order?
- How would you order events that span multiple keys?
- What is the effect of a custom partitioner on ordering?
MCQ Practice
1. At what level does Kafka guarantee message ordering?
Kafka only guarantees strict offset order within a single partition; a topic with multiple partitions has no global order.
2. Which producer setting best ensures related events stay in order in the same partition?
A consistent key hashes to the same partition, so all related events keep their sequence.
3. Why enable idempotence on the producer for ordering?
Idempotent producers deduplicate retries so a resent batch cannot land out of order within a partition.
Flash Cards
Where is Kafka ordering guaranteed? — Within a single partition, by strictly increasing offsets — never across an entire topic.
How do you keep related events ordered? — Give them the same message key so the partitioner routes them to the same partition.
What breaks ordering during retries? — Retried batches can be overtaken; enable idempotence and bound in-flight requests to prevent it.
Ordering vs parallelism trade-off? — More partitions mean more throughput but no global order — you order only per key/partition.