Kafkaはパーティション内での順序付けは保証しますが、パーティション間での順序付けは保証しないという重要な概念があります。関連するイベントの順序付けを保証するには、同じパーティションにルーティングします(同じキーを使用することで)。順序付けを理解することは、正確性のために重要です。
パーティション単位の順序付け
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
