zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    [LeetCode]Linked List Cycle
    ACM 整数划分(四)
    ACM 子串和
    ACM 阶乘之和
    ACM 组合数
    ACM 阶乘的0
    ACM 比大小
    ACM 擅长排列的小明
    ACM 重建二叉树
    cocos2dx 魔塔项目总结(一)
  • 原文地址:https://www.cnblogs.com/netfocus/p/2945450.html
Copyright © 2011-2022 走看看