Is a snapshot supposed to store only the last event data? or does it store the actual aggregate state of all previous events? #478
-
I've been thinking of incoporating event-sourcing for an app I'm working on. But I haven't found any documenation of how exactly a snapshot works. After playing with the larabank-aggregate project, I ran the transactions seeder, and after that the "deposit" and "withdraw" became too slow and unresponsive due to the amount of events being fetched each time, but they worked as expected and everything was fine. However, after creating a snapshot to improve performance (as suggested by the docs), attempting to withdraw greater amounts of money would fail stating that the account cannot exceed the limit of -5000 even if the balance itself is much more than the requested "withdraw" amount. After debugging for a while, it looks like the balance is only equal to the last "add money" event value, not the actual balance of the account. I don't know if this is an issue with snapshot state, or the expected behavior of snapshots. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
After a bit of digging, turns out that the default |
Beta Was this translation helpful? Give feedback.
-
Yes in order for snapshots to work you must have a public property on the aggregate for each piece of state you want to keep. Note: Aggregates are rebuilt in memory at the time of retrieval either from scratch or the latest snapshot forward. So if you plan to use snapshots you should assert somewhere in your tests that the state you need exists in the snapshot. Projections only get rebuilt when replaying events. |
Beta Was this translation helpful? Give feedback.
After a bit of digging, turns out that the default
getState()
function of aggrigates which uses reflections, only returns thepublic
properties, and the larabank repository definesbalance
andaccountLimitHitInARow
asprotected
properties, and by doing so prevents the snapshot from having any state when persisted. I will probably make a PR fixing the issue for future users trying to test the larabank repo.