Is multiple aggregates update in one transaction good?

简介:

I don't believe there is anything wrong with using multiple repositories to fetch data in a transaction. Often during a transaction an aggregate will need information from other aggregates in order to make a decision on whether to, or how to, change state. That's fine. It is, however, the modifying of state on multiple aggregates within one transaction that is deemed undesirable, and I think this what your referenced quote was trying to imply.

The reason this is undesirable is because of concurrency. As well as protecting the in-variants within it's boundary, each aggregate should be protected from concurrent transactions. e.g. two users making a change to an aggregate at the same time.

This protection is typically achieved by having a version/timestamp on the aggregates' DB table. When the aggregate is saved, a comparison is made of the version being saved and the version currently stored in the db (which may now be different from when the transaction started). If they don't match an exception is raised.

It basically boils down to this: In a collaborative system (many users making many transactions), the more aggregates that are modified in a single transaction will result in an increase of concurrency exceptions.

The exact same thing is true if your aggregate is too large & offers many state changing methods; multiple users can only modify the aggregate one at a time. By designing small aggregates that are modified in isolation in a transaction reduces concurrency collisions.

Vaughn Vernon has done an excellent job explaining this in his 3 part article.

However, this is just a guiding principle and there will be exceptions where more than one aggregate will need to be modified. The fact that you are considering whether the transaction/use case could be re-factored to only modify one aggregate is a good thing.


目录
相关文章
|
10月前
|
程序员 Go API
译|Don’t just check errors, handle them gracefully(一)
译|Don’t just check errors, handle them gracefully
52 0
|
10月前
|
Java Go API
译|Don’t just check errors, handle them gracefully(二)
译|Don’t just check errors, handle them gracefully(二)
63 0
2015-03-18 current note update logic in my task
2015-03-18 current note update logic in my task
73 0
2015-03-18 current note update logic in my task
Multiple Origin composition test - Opportunity Creation case
Sent: Wednesday, 3 December, 2014 2:48 PM 结论是:如果gateway系统上针对一个odata service维护了多个mark成default的backend system,在creation的case下,runtime时候gateway只会向第一个 Default system发起请求。
111 0
Multiple Origin composition test - Opportunity Creation case
2015-03-17 current note creation logic in my task
2015-03-17 current note creation logic in my task
87 0
2015-03-17 current note creation logic in my task
simulation pipeline after change not refresh issue
simulation pipeline after change not refresh issue
83 0
simulation pipeline after change not refresh issue
why My Lead OPA test add Lead fails
why My Lead OPA test add Lead fails
105 0
why My Lead OPA test add Lead fails
When should reread of cl_crm_bol_entity and $scope.$apply be called manually
When should reread of cl_crm_bol_entity and $scope.$apply be called manually
121 0
When should reread of cl_crm_bol_entity and $scope.$apply be called manually