-
-
Notifications
You must be signed in to change notification settings - Fork 94
Description
Problem
I need snapshot support because there are domain models with a large number of events and a long lifecycle. Initially, I tried to split the models into 'daily models'; this solved the long lifecycle problem but added complex and fragile day-closing/opening logic, which is not part of the business logic and rather exists as a technical workaround. Also, some instances of these models can still remain 'highly loaded' during the day, so the problem is only partially solved.
I would like to add support for 'Rolling snapshots', but before that, I want to discuss the implementation to make sure it aligns with your vision and that the PR will be accepted. I have seen several issues related to snapshots, and in some cases, changes were even suggested, but they were rejected. Also, these issues are quite old, and the perspective may have changed by now.
Here's how I see the implementation
It is assumed that rolling snapshots are stored as events in the stream along with the rest of the domain model events.
The first thing we need to do is somehow distinguish snapshot events from other events. Here, I suggest adding an attribute HasRollingSnapshots where you can pass a list of event types that are snapshots. This attribute should be applied to the State type.
Next, in the LoadState and LoadAggregate methods, we try to get the HasRollingSnapshots attribute and pass a list of types to the ReadStream method. If the list contains at least one type, ReadStream starts reading the event stream from the end until it finds an event whose type matches one of the types in the list. Also, ReadStream does not return events that come before the snapshot event.
The question remains as to how the model determines when to apply snapshots and when not to. But it seems to have everything it needs for this. In the case of aggregates, there is the Original field, which allows you to check the number of events or the time elapsed since the last snapshot, and in the case of a model without an aggregate, the list of events is also passed to the decider function (possibly this is precisely why they were added).