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.

  • 相关阅读:
    11.爱吃皮蛋的小明(斐波那契数列)
    10.二叉树最大宽度和高度
    9.二叉树的序遍历
    8.递归第一次
    6.数的计算(递归算法)
    5.十进制转m进制
    数论——快速幂(C++)
    最短路径——SPFA算法(C++)
    最小环——Floyd变种算法(C++)
    数论——质因数分解(C++)
  • 原文地址:https://www.cnblogs.com/netfocus/p/2945450.html
Copyright © 2011-2022 走看看