ryan-mars / stochastic

TypeScript framework for building event-driven services. Easily go from Event Storming → Code.
MIT License
6 stars 1 forks source link

Discussion: How bounded contexts treat external events #72

Open ryan-mars opened 3 years ago

ryan-mars commented 3 years ago

Imagine a situation where a read model is updated by events originating from within the bounded context, and some that originate from another bounded context. The question is: If I need to do an event-replay to rebuild that Read Model from scratch, should I rely on the external bounded context to replay its respective events properly OR should each bounded context store relevant events that originate from outside?

The tradeoffs I see are:

Here's an example scenario, Reservations should not book more people than a plan can hold, yet Scheduling is in charge of which aircraft types are on each flight. In order to maintain a correct count of seat availability I need to consume an event like ScheduledFlightAdded from Scheduling to set Item.SeatsAvailable to its initial value in my read model in DynamoDB (i.e. 318 for a Boeing 787-10). Then every reservation I sell in Reservations can decrement that number. If I need to rebuild this read model, Scheduling's local events will not be enough. I'll have to request replay from Reservations. If instead I've stored those events, then no problem.

So, the principle would be bounded contexts should store externally generated events they're concerned with, or no?

My gut tells me you need to save all external events you're concerned with, otherwise you might not be able to properly recreate the state of your read models. Perhaps this gives value to the idea that each StoreConstruct should have its own table. You could then have a Store for ExternalEvents thereby not risking adverse effects on your main event store? :thinking_face:

ryan-mars commented 3 years ago

Spoke with @michaellperry today and his response (summarizing) is: Yes we want to cache events from outside so we're not temporally coupled to another service (waiting).