Pergunta

I'm trying to learn EventStore, I like the concept but when I try to apply in practice I'm getting stuck in same point. Let's see the code:

foreach (var k in stream.CommittedEvents)
{ 
   //handling events
}

Two question about that:

  • When an app start ups after some maintenance, how do we bookmark in a safe way what events start to read? Is there a pattern to use?

  • as soon the events are all consumed, the cycle ends... what about the message arriving run time? I would expect the call blocking until some new message arrive ( of course need to be handled in a thread ) or having something like BeginRead EndRead.

Do I have to bind an ESB to handle run time event or does the EventSore provides some facility to do this?

I try to better explain with an example Suppose the aggregate is a financial portfolio, and the application is an application showing that portfolio to a trader. Suppose the trader connect to the web app and he looks at his own portfolio. The current state will be the whole history, so I have to read potentially a lot of records to reproduce the status. I guess this could be done by a so called snapshot, but who's responsible for creating it? When one should choose to create an aggregate? How can one guess a snapshot for an aggregate exists ? For the runtime part: as soon the user look at the reconstructed portfolio state, the real time part begin to run. The user can place an order and a new position can be created by succesfully execute that order in the market. How is the portfolio updated by the infrastructure? I would expect, but maybe I'm completely wrong, having the same event stream being the source of that new event new long position, otherwise I have two path handling the state of the same aggregate. I would like to know if this is how the strategy is supposed to work, even if I feel a little tricky having the two state agents, that can possibly overlap. Just to clarify how I fear the overlapping:

  • I know events has to be idempotent, so I know it must not be a problem anyway,
  • But let's consider the following:

I subscribe an event bus before streaming the event to update the state of the portfolio. some "open position event" appears on the bus: I must handle them, but maybe the portfolio is not in the correct state to handle it since is not yet actualized. Even if I'm able to handle such events I will find them again when I read the stream.

More insidious: I open the stream and I read all events and I create a state. Then I subscribe to the bus: some message on the bus happen in the middle between the end of the steram reading and the beggining of the subscription: those events are missing and the aggregate is not in the correct state.

Please be patient all, my English is poor and the argument is tricky, hope I managed to share my doubt :)

Foi útil?

Solução

The current state will be the whole history, so I have to read potentially a lot of records to reproduce the status. I guess this could be done by a so called snapshot, but who's responsible for creating it?

In CQRS and event sourcing, queries are served by projections which are generated from events emitted by aggregates. You don't use the aggregate instance as reconstituted from the event store to display information.

The term snapshot refers specifically to an optimization of the event store which allows rebuilding the aggregate without replaying all of the events.

Projections are essentially event handlers which maintain a denormalized view of aggregates. Events emitted from aggregates are published, possibly out of band, and the projection subscribes to and handles those events. A projection can combine multiple aggregates if a requirement exists to display summary information, for instance. In case of a trading application, each view will typically contain data from various aggregates. Projections are designed in a consumer-driven way - application requirements determine the different views of the underlying data that are needed.

With this type of workflow you have to embrace eventual consistency throughout your application. For instance, if an end user is viewing their portfolio and initiating new trades, the UI has to subscribe to updates to reflect updated projections in an asynchronous manner.

Take a look at here for an overview of CQRS and event sourcing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top