How does Kafka keep messages in order, and what role do partitions play?
Kafka guarantees order only within a single partition. Messages with the same key go to the same partition, so ordering holds per key. More partitions give more parallelism but no global order.
Explain it simply
A Kafka topic is split into partitions, each an append-only log. Within one partition, messages have increasing offsets and are read in that exact order. Across partitions, there is no ordering guarantee.
The producer picks the partition by hashing the message key. All messages with key "order-17" land in the same partition, so events for that order (created, paid, shipped) are consumed in sequence. Different orders spread over partitions and are processed in parallel.
Consumer groups: each partition is assigned to one consumer in a group, so the maximum parallelism of a group equals the number of partitions. Extra consumers beyond that sit idle.
Design consequences:
- Choose the key as the entity whose events need ordering.
- Need a total global order? Use one partition and accept limited throughput.
- Increasing partition count later changes key-to-partition mapping for new messages, which can break per-key ordering during the transition.
Example
Topic with 3 partitions, key = order id.
P0: order-17 created, order-17 paid, order-17 shipped
P1: order-22 created, order-22 cancelled
P2: order-35 created
Consumer group of 3: one consumer per partition
Order within order-17 is preserved; order-17 vs order-22 is not.What to say in the interview
"Kafka orders messages per partition, not per topic. I key by the entity needing ordered events so they share a partition, and size partitions for consumer parallelism. If I need global order I accept a single partition's throughput."