Question 19 of 50
How does ordering work?
Question
"How does message ordering work in Kafka?"
What the interviewer wants to assess
A correct, complete understanding of the ordering mechanism — not a vague "yes, Kafka is ordered" answer, but an explanation of where that guarantee actually lives.
Resposta rápida
Kafka guarantees that messages written to the same partition are read in exactly the order they were written. This comes from the log structure: a partition is a linear, immutable sequence, so write order is, by construction, read order.
Resposta nível Sênior
Ordering in Kafka is a property of the partition, not the topic. Each partition is an append-only log, where every record gets an increasing sequential offset — since the structure is strictly sequential, reading from start to end exactly reproduces write order. A topic with multiple partitions, though, isn't a single sequence: it's a set of independent sequences, each with its own offset clock. Two messages in different partitions have no defined ordering relationship between them. It's the key (Chapter 4) that decides which partition a message lands on, and therefore which set of messages actually stays ordered relative to each other.
In-depth explanation
See "Ordering within a partition — and the absence of global ordering" in Chapter 4.
Exemplo financeiro
Two events for the same contaId — "PIX received" followed by "balance debited due to chargeback" —
reach the consumer in the correct order because they share the same key and, therefore, the same
partition. Events for different accounts, on different partitions, have no ordering relationship between
them — which is acceptable, because there's no real dependency between them.
"Kafka processes everything in the order it was published"
That statement ignores that a topic with multiple partitions spreads messages across them — global order across different partitions isn't guaranteed, even if the events were published very close together in real time.
Pode vir a seguir
Likely follow-ups: "does Kafka guarantee global ordering?" and "what would happen if I used a random UUID as the key?".
Related chapters