Video in progress
When does Kafka guarantee message ordering?
Only within a single partition. Messages written to one partition are read in that same order; there is no guarantee of a global order across the whole topic.
📖 Answer
If the "credit" and "debit" events of account account-42 have to be processed one after the other, send them with the same key, and both land in the same partition.
Places where the order can break:
- sending without a key: messages are spread across different partitions;
- increasing the partition count: the partition for a given key changes;
- producer retries in old versions with
max.in.flight.requests.per.connection > 1(when idempotence isn't enabled); - handing messages off to a thread pool yourself on the consumer side.
The last one is the most common. Kafka delivered the order correctly, but if you call executor.submit(...) inside the listener, the processing order gets mixed up anyway.
If you need strict ordering across the whole topic, the only way is a single partition. But then parallelism is limited to one consumer too.
Sign up to mark studied questions and save your progress.