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

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…

主站蜘蛛池模板: 高台县| 山阴县| 哈巴河县| 海盐县| 奎屯市| 河东区| 会理县| 准格尔旗| 丰台区| 册亨县| 平江县| 扶沟县| 迁西县| 若尔盖县| 东乌珠穆沁旗| 拉萨市| 定州市| 鲁山县| 志丹县| 邳州市| 酉阳| 长汀县| 辉南县| 彭水| 海淀区| 通榆县| 成武县| 惠东县| 隆昌县| 临城县| 贡觉县| 贡嘎县| 德昌县| 绥阳县| 德安县| 永嘉县| 宜丰县| 昆山市| 聊城市| 井冈山市| 新龙县|