Query for state at given point in time

Hello,

I stumbled across Axon when I was searching for an event sourcing framework.
More precisely I need the functionality to query my data for its state at specific times in the past.
So event sourcing would enable me to recreate the state of my data at any given point in time.

So far so good, but after hours of web search I didn’t find a proper way in Axon to dispatch a query in the form: “Give me the state of the data at this time.”
Shouldn’t that be possible in an event sourcing framework?
I know that I can use a TrackingEventProcessor to replay the event store, but as pointed out in this stackoverflow question, Axon doesn’t cover the scenario to replay to a given date, that has to be done on the query side.

With that in mind, my closest solution to the mentioned behaviour is to use a TrackingEventProcessor to replay the event store and wait on the query side for the event with the TrackingToken at a given time.
But this seems more like a workaround. Furthermore, this approach doesn’t facilitate me to query for multiple times in the past at once because then the TrackingEventProcessor has to be restarted and the previous state is lost.

Is Axon not suited for my use-case or am I missing something?

Thanks

Hey,

since you can decide how your projection looks like and how many of them you want,
you could simply design a table with a date or timestamp in the primary key.
The query could then be made with a WHERE MYDATE < ?.
Even if the event handler keeps writing into it, you could always query using a date.

Best regards
J

Hi Steff,

the challenge you have is one where event sourcing helps, but isn’t the “plain and simple answer”. Event sourcing just helps you guarantee that you have all the state changes of your system, so that you have the ability to go back in time and check the data. It doesn’t mean that Axon simply keeps a snapshot of each state your system was ever in.

Depending on your performance requirements, you could build an ad-hoc view of a certain state at a moment in time (indeed by reading events up to a specific moment) or you can pro-actively create a new version for each state in your view model. As a “middle ground” option, you can store these states at particular intervals, and get more detailed state by reading events from the last one included in that state inward to the time you wanted to take the snapshot from.

Note that for this building of the view model, you wouldn’t use a Tracking Processor. You should simply open a stream from the event store and populate a view model using that. Tracking Processors are meant to process a single view model and keep that up-to-date as events arrive.

Hope this helps.
Cheers,

Dear Allard Buijze,

thanks very much for your answer. To open streams directly from the event store instead of using a Tracking Processor, was the crucial point for me.

I simply created a class that opens as many streams of the event store as I need and use them to replay the events by myself to the time I need.
Although this solution seems pretty easy and obvious after all, I couldn’t figure it out by myself. You helped me a lot, thanks!

Regarding the performance, I still have to run more tests and examine if this approach is efficient enough.

Kind regards
Steff