Kafka保证partition内的顺序,但不保证跨partition的顺序 — 这是一个关键概念。要获得相关事件的顺序,需要将它们路由到同一个partition(通过相同的key)。理解顺序对正确性很重要。
单partition内的顺序
Kafka guarantees:
✓ ORDER WITHIN a partition → events in a partition are strictly ordered (by offset);
consumers read them in order
✗ NO order ACROSS partitions → events in different partitions have no relative order
guarantee (they're processed in parallel by different consumers)
→ this is a FUNDAMENTAL Kafka property (and a common source of misunderstanding)
获得相关事件的顺序
To keep RELATED events ordered → send them to the SAME partition:
→ use the SAME KEY for related events (key → hash → same partition):
e.g. all events for orderId=5 use key=5 → same partition → ordered for that order
→ so per-entity ordering: key by the entity (userId, orderId, accountId)
→ choose the key so events that must be ordered share a partition
