Event sourcing: avoid projects duplicated events while replaying events and listen new incoming events

时光怂恿深爱的人放手 提交于 2019-12-24 09:48:50

问题


In the scenario where it needs to build a new view, we could replay all the events from the EventStore. As a result we'll have the new view projected.

So, the idea is deploy a new projection that projects all old events (by replaying) and also listen new incoming events and project them.

I think that races conditions can occur while reading olds events and listen new incoming events. As a result we could project the same event more than once and then corrupt the new view.

So, what is good approach to draw a line between events to be replayed and new incoming events?

Thanks!


回答1:


EventStore supports this scenario via Catch-up Subscriptions

You would specify the very start of the stream as the starting point for the subscriber, it would then process all events up until "now" and then start listening to events that come in.

Connection.SubscribeToStreamFrom(
    StreamName, 
    checkpoint, 
    resolveLinkTos,
    OnEventReceived,
    OnLiveProcessingStarted, 
    OnSubscriptionDropped);

You even get an event for when you start processing live. :)

More general advice for this sort of thing:

  1. Make the process idempotent, so it doesn't matter if you receive the same event twice
  2. De-duplicate on the receiving end - you'd need to keep a record of which events had already been processed



回答2:


From a Greg Young talk, I can summarize that EventStore works similar to an Atom feed. So projections (aka subscribers) poll events since an id tracking which subscriber maintain.

Then EventStore knows nothing about if a new projection needs replay all events or not. EventStore, in Greg Young words, receives

Give me 'n events' after 'x'



来源:https://stackoverflow.com/questions/38197712/event-sourcing-avoid-projects-duplicated-events-while-replaying-events-and-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!