Module 15 / Messaging and event logs

Module 15 · lesson 4 of 8 · Read 8 min

Ordering

Order within a partition, and choosing the key.

Picture the customer's tracking screen showing "Delivered", and then, two seconds later, "Rider picked up your order". The events arrived in the wrong order, and the screen believed the last one it saw.

Order matters for a lot of events. Kafka gives you a strong ordering guarantee, but a narrow one: messages are in order within a partition, and nowhere else. This lesson is about using that guarantee well, mostly by picking the right key, and about the few ways order still gets broken.

What Kafka promises

Inside one partition, messages are stored in the order the leader received them, and consumers read them in that order. If OrderPickedUp is at offset 500 and OrderDelivered at offset 501 in the same partition, every consumer sees them in that order.

Across partitions, there's no promise at all. Partition 3 might be read a little faster than partition 7. An event written later to partition 3 can easily be processed before an earlier one in partition 7.

So the real question is: which events need to be in order relative to each other? Put those in the same partition. Everything else can be spread out.

Kafka guarantees order only within a partition. The message key decides the partition, so the key decides which messages stay in order with each other.