How to handle transaction in event driven architecture?

心已入冬 提交于 2019-12-08 13:47:31

Alright, here is my take on your question:

In event driven & CRQS architecture, it is also common to use event sourcing as a pattern.

That means that your votes will not be stored as a counter (state), but rather as vote up/down (fact).

In your scenario, to query how many votes an article have accumulated, you can go read all the events related to an aggregate and apply them one by one (in memory) in the order they happened and get a final state of that aggregate instance (article).

Sometimes when the volume of events are big, a snapshot is kept on the most recent facts resulting from applying those events to an aggregate.

Worth noting: Be careful of side-effects when applying events to get state. Because if a vote up/down event caused an article to be closed for example and sent an email to the author, then you don't want that email to be sent again when replaying those events.

Think of this as the financial equivalent to debit/credit events in a bank. Banks don't just store your balance. They actually store all your transactions, and later do a reconciliation and update your balance.

Some transactions happen immediately if the debtor/creditor accounts are within the same bank

What that gives you out of the box?

  • Scalability; this is a write-optimised architecture that can handle larger requests to modify application state faster without transactions & locking
  • Traceability; when inspecting your system, not only you will find out where you're at (current state) but also you will know how/why you got there (events)

Also, since you have a data stream of all user interactions with the system, you can later use that data in different ways for new features that you would not have thought of from the beginning of designing the system (Analytics for example?)

Further reading:

Aggregates define transaction boundaries. When you decide for event-driven architecture then you decide for eventual consistency. So you just have to choose: transaction OR event.

Note that in most cases, eventual consistency is completely fine for the business. That's only devs who have a transactional fetish implanted by RDBMS lectures/brainwash on the university ;)

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