Apache Kafka
Ordering Guarantees
Know exactly where Kafka promises order — and where it does not.
Kafka guarantees ordering only within a single partition. Records appended to one partition are read in the same order, identified by their offsets. Across partitions there is no global ordering, because partitions are consumed in parallel by independent threads or consumers.
Several supermarket checkout lanes: each lane processes its own customers strictly in line, but there is no single order across lanes. Put one family in one lane (a key) and that family is always served in order.
Key Concepts
1
This is why keying matters: routing all events for one entity (one orderId) to the same partition is the standard way to preserve per-entity order. The trade-off is that ordering and parallelism pull in opposite directions — a single partition is perfectly ordered but caps throughput at one consumer.
2
A subtle producer pitfall: with retries and more than one in-flight request per connection, a failed-then-retried batch can land after a later batch, reordering records. The idempotent producer fixes this and is the recommended setting whenever order matters.
3
What the interviewer is probing.
What the interviewer is probing.
4
1. "What ordering does Kafka actually guarantee?" *Probing:* precision. *Stalls:* "Full
ordering." *Moves up:* within a partition only — across partitions there is no order at all.
1. "What ordering does Kafka actually guarantee?"
5
2. "How do you get ordering for a business entity?" *Probing:* the design. *Stalls:* "One
partition." *Moves up:* key by that entity so all its records land in one partition, which preserves
order without serialising the whole topic.
2. "How do you get ordering for a business entity?"
6
3. "What breaks ordering even within a partition?" *Probing:* the trap. *Stalls:* "Nothing."
*Moves up:* retries with max.in.flight above one and idempotence disabled, which can reorder on a
failed batch.
3. "What breaks ordering even within a partition?"
7
4. "What is the cost of ordering everything?" *Probing:* the trade. *Stalls:* "None." *Moves
up:* a single partition means a single consumer, so throughput is capped at what one process can do.
4. "What is the cost of ordering everything?"