Using projections to create past state models


I have stream with 50 events. Can I use projections to create a state model of that stream at a given event number? Let’s I want to know wthat changed between event #42 and #43.

How do I tell the projection to not use all the events in the stream? Or should I use a query for this?

I would use a read model for that or with such a trivial number of events build on demand.

That’s just the thing, the projections already contains all the logic we need, which is quite complex. We would only need to “feed” the projection a subset of the stream and it would produce a readmodel.

We have a frontend UI which already is capable of showing the diff of two readmodels. If you say, use a readmodel, do you mean to build up that readmodel in Eventstore or externally?

externally to some storage mechanism (usually you also want to be able to query over the state etc)

Yes, I save the readmodel in Elasticsearch. Are you saying I need to keep revisions of the readmodel in Elasticsearch and use those? Now I do a full update of the readmodel in Elasticsearch, so no versioning right now. I was under the impression that by using Eventstore I could get older versions of the readmodel and I would not have to save those in an external system.

It totally depends on what your read model does and how it works. You can always get a read model to any point in time but there are many ways of modeling how you get there. As example you can always replay the entire read model to a point in time, this will always work. You can also model temporally your read model to allow for temporal queries on it. All of these choices are completely orthogonal to ES.

This is indeed exactly the use case I’m looking for, just one more question: how do I use the projections in Eventstore to produce a readmodel to a certain point in time?

You usually would not want to. Usually you want an external read model for this.

Depending on sizes/queries it is also quite common to build such a model on demand in memory for a given query (eg I have 100 events and you want to know an arbitrary point in time query, I load them up, fold over them and return a result). There are many different ways of handling this that all have pros and cons.

Maybe you need to split your stream into multiple streams?
Sounds like you might need to partition it on some sort of aggregate model or other criteria.

E.g. if your events are customer events, then it makes sense to split that stream into customer-1, customer-2 etc… streams.

Then your logic can act on each one as a whole.

We have this situation. All streams are partitioned.