Suppose there are 2 microservices at play. Service A and service B. Both have their own set of aggregates, let’s say service A has FamilyAggregate and service B has HouseAggregate (both are event sourced).
At one point, FamilyAggregate generates an event, called NewChildBornEvent. HouseAggregate on the other hand, holds information about the number of occupants in the given building (because internally it’s used to do some calculations and conditions for it’s own commands; whatever - it needs that piece of information).
How can HouseAggregate update it’s internal state, when a new child is born? I can think of 2 possible solutions:
- Service B has also an @EventListener method for the NewChildBornEvent, which in turn issues a UpdateNoOfOccupantsCommand that HouseAggregate listens on, and that in turn generates OccupantsUpdatedEvent, which finally sets the internal state.
So you’ve got:
FamilyAggregate -> NewChildBornEvent -> EXTERNAL_EVENT_LISTENER -> UpdateNoOfOccupantsCommand -> HouseAggregate -> OccupantsUpdatedEvent
This seems like an overkill.
- Less likely scenario, is to bring those two aggregates to one service and either make one an aggregate member of the other, or create a new aggregate and make both FamilyAggregate and HouseAggregate members of the new aggregate.
This doesn’t seem like a universal solution however, as one can easily imagine a scenario where there will be 2 aggregates such that it makes no sense putting them together.
For the lack of better ideas I’ve been practising the 1. solution. I just think there should be an easier way to do this, like HouseAggregate listening directly on NewChildBornEvent. Perhaps I’ve missed the trick somewhere?
Let me know how you practise similar scenarios and maybe point me to a better solution. Thanks