Query for past state of one object

Is it possible to query for the previous state of a specific object?

Commands have a @TargetAggregateIdentifier annotation and I can see the events being replayed in my aggregate when I send a command.
But how would I be able to do something like that for a projection and a temporal query?

Hi Robert,

Temporal querying is not build in to the framework and as such this would require a purpose build Query Handling Component that likely connects to the Event Store to retrieve the event up to a given time frame to rebuild the model.

To build something like this, I’d suggest you create a Query Model which you want to recreate up to a given state.
This model should be capable of handling events to adjust it’s own state.

From there, you can add a Query Handling Component which will handle the temporal query you have, likely containing some identifiers and the time frame you’re interested in.
That component will have a dependency on the Event Store to retrieve a stream events, which it can replay against your “Event Handling Query Model”.
To perform the latter, it’s easiest to wrap a fresh instance of your model in a AnnotationEventHandlerAdapter.
The AnnotationEventHandlerAdapter has a handle(EventMessage) method, so that it can handle the stream of events.
Lastly, the component creating the AnnotationEventHandlerAdapter and providing the event stream to it should have the smarts to cut of the stream at a given point in time.

So, it’s some self work at the moment, but shouldn’t be to hard.
Final note though.
This approach will mean that on an increase of you Event Store, this operation will take longer and longer.
Optimizing this wholly depends on the number of events you need to replay against your Query Model, something which is hard to deduce upfront.

That’s my two cents to the situation.


Hi Steven,

After Robert Malczewski in another post mentioned the EventStore, and some debugging Axon to see how it plays event on annotated methods, I created something like you described.

So it’s good to hear that my solution is nearly as simple as currently possible with Axon.

Are there any plans to support this more natively in Axon? I ask this, because temporal queries are often mentioned as one of the advantages of event sourcing.

Hi Robert,

Happy to hear you’ve tried this out and that it solved the problem you had!
I noticed the other thread with Sebastian and Robert Malczewski in it too, great that you extended the thread with the reply I gave you here.

From a roadmap perspective, I think we’ll eventually introduce something like this, yes.
The tough part however is to provide it as a generic feature which is usable for everybody, not just a solution to create query models which directly reference a single Aggregate instance.

As we prefer to provide a more thorough solution then just for Query Models based on a single Aggregate instance’s stream, we will require some more investigation from our part before it’ll be introduced.
Regardless, opening this thread points out that there’s still interest in a feature like this, and we definitely look at our community to drive the roadmap.

So in short…stay tuned I’d say!

I obviously cannot give you a time frame at the moment, sorry for that.