node.js - mongo的事务一致性如何保持
怪我咯
怪我咯 2017-04-17 11:34:47
0
1
603

我现在有2个操作,一个insert,一个update,都是对一张表操作的,如何保证事务一致性? upinsert可以多条数据么? mongo官方文档上有Perform Two Phase Commits的文章。除了这个还有其他方案么?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
Ty80

Upsert means adding one piece of data when there is no data. So what does "multiple pieces of data" mean?

If set to true, creates a new document when no document matches the query criteria.

Back to consistency and this article, the Chinese community is very awesome and the official documents are also translated into Chinese! This article is to simulate the Atomic and Consistency of multiple operations in ACID in traditional databases. Atomic means that either all operations are executed or none are executed. Half of the operations cannot be executed. I have changed a few pieces of data and it seems wrong, so throw it away. The meaning of Consistency in the database field is different from that in distributed systems. It refers to whether the external constraints of the data are satisfied. For example, when transferring funds between accounts, the final sum cannot be negative or exceed the original sum.

Then what is the method in the document? To put it simply, before performing an operation, write down what you want to do so that you can redo it later. Then ensure that each operation is Impotent , which means that it will be fine if you execute it repeatedly. For example, assignment is idempotent, but adding a number is not. Utilize the atomicity of a single document provided by MongoDB, use some auxiliary data to achieve idempotence, and finally clear the auxiliary data. If your operation itself is idempotent, then there is no need for auxiliary data. If you want to undo, it is possible, so just note how to undo.

But there is no Isolation in ACID, which means that there is overlap between transactions, and other concurrent operations can see inconsistent states in the middle. The external constraints mentioned above can only be ultimately guaranteed. For example, transaction T1 包括 (张三:加100;李四:减100), transaction T2 包括 (张三:加200;李四:减200), if not restricted, the possible order is:

  1. T1 (Zhang San: add 100)
  2. T2 (Zhang San: add 200)
  3. T2 (John Doe: minus 200)
  4. T1 (John Doe: minus 100)

When T2 is executed, it can read and modify the intermediate results of T1. There is no big problem in the transfer issue, because addition and subtraction are exchangeable operations, and the sequence does not affect the final result. But if we change the transaction to T1 (张三 = 100;李四 = 100) and T2 (张三 = 200;李四 = 200), the final result may be (张三 = 200;李四 = 100), which may not satisfy the consistency. But if the application can guarantee the sequential execution of these two transactions, the problem will be avoided.

Everyone also knows that there are several levels of Isolation, as well as multiple versions and more complex ones. Traditional databases also make trade-offs between stronger isolation and performance on a single machine, providing different levels to choose from. This is called Consistency in distributed systems and is more expensive to implement, so MongoDB does not support it. But for most applications, this is not a big problem:

  1. Perhaps abnormal situations are logically acceptable, such as mass messaging on WeChat, and everyone receives it in a different order.
  2. Perhaps logical concurrency is impossible. For example, a user can only modify his own data, or only one thread can write data.
  3. Or put the required data into a document. For a single document, MongoDB guarantees atomicity, and other operations cannot see half of the document being changed.
  4. Or it can be discovered and solved in the upper layer of the application. For example, Alipay has implemented a set of consistency protocols for transfers, which can ensure that the data is consistent within one minute.

For your request, it would be convenient if the data model can be modified so that the changes can be contained in one document. It is best to look at your specific needs and application assumptions, analyze possible anomalies, and finally find a solution. There is another way. Your company can purchase MongoDB support services to provide different types of professional-level support based on your current product stage and needs (development, maintenance, consulting, training), rather than hiring a programmer/DevOp/DBA. Much cheaper and more reliable. I will tell you that they actually also have Chinese support?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template