官术网_书友最值得收藏!

Modeling data for atomic operations

MongoDB is relaxing many of the typical ACID (Atomicity, Consistency, Isolation, Durability) constraints found in RDBMSes. In the absence of transactions, it can be sometimes a pain to keep state consistent across operations, especially in event of failures.

Luckily, some operations are atomic at the document level:

  • update()
  • findandmodify()
  • remove()

These are all atomic (all or nothing) for a single document.

This means that if we embed information in the same document, we can make sure that they are always in sync.

An example would be an inventory application, with a document per item in our inventory, where we would need to total available items left, how many have been placed in a shopping cart in sync, and summing up to the total available items.

With total_available = 5, available_now = 3, shopping_cart_count = 2, this could look like:

{available_now : 3, Shopping_cart_by: ["userA", "userB"] }

When someone places the item in his/her shopping cart, we can issue an atomic update, adding his/her userId in the shopping_cart_by field and decreasing the available_now field by 1 at the same time.

This operation will be guaranteed atomic at the document level. If we need to update multiple documents within the same collection, the update may update some of the documents but not all of them and still go through.

This pattern can help in some cases but unfortunately not with every case. In many cases, we need multiple updates being applied all or nothing across documents or even collections.

A typical example would be a bank transfer between two accounts. We want to subtract x GBP from user A, then add x to user B. If we fail to do either of the two steps, we should return to the original state for both balances.

The details of this pattern are outside the scope of this book, but roughly, the idea is to implement a hand-coded two phase commit protocol. This protocol should create a new transaction entry for each transfer with every possible state in this transaction such as initial, pending, applied, done, canceling, and canceled and, based on the state that each transaction is left at, apply the appropriate rollback function to it.

If you find yourself absolutely needing to implement transactions in a database that was built to avoid them, take a step back and rethink why you need to do that…

主站蜘蛛池模板: 福贡县| 永兴县| 寿阳县| 盱眙县| 安丘市| 剑河县| 历史| 盐边县| 咸丰县| 望奎县| 松江区| 尼木县| 新平| 凯里市| 怀集县| 朔州市| 周至县| 潍坊市| 松潘县| 永吉县| 重庆市| 新乐市| 三门峡市| 巴林左旗| 错那县| 偏关县| 义乌市| 沛县| 建昌县| 高淳县| 新沂市| 泰来县| 泉州市| 驻马店市| 十堰市| 高安市| 华安县| 逊克县| 和硕县| 礼泉县| 广宁县|