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

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…

主站蜘蛛池模板: 遂溪县| 台中县| 富平县| 疏勒县| 六枝特区| 兰州市| 上饶市| 昭苏县| 德惠市| 邻水| 绍兴县| 洛扎县| 娄烦县| 惠东县| 安顺市| 克什克腾旗| 呼图壁县| 手机| 文昌市| 屯门区| 苏尼特右旗| 老河口市| 峨眉山市| 西乡县| 阿拉尔市| 大庆市| 潍坊市| 茌平县| 昌邑市| 天气| 炉霍县| 达州市| 柞水县| 商城县| 无锡市| 承德县| 房产| 山东省| 涞源县| 江油市| 讷河市|