Reputation: 1759
My program has users and groups. Use-case: Users have credits they can give to the groups.
I want no credits to be lost.
The problem with RDBMS-like schema is the atomicity of Mongo is document-level. I reduce the credits of the user and then increase the credits of the group. If the application crashes between the two the credits, that are sent, become lost. (If I replace the two actions credits can appear without being withdrawn from the user.)
One solution could be using of a collection called transactions. Then the credits of the user can be calculated from the send credits of the user subtracted from the credits of the user. The credits of the group can be calculated as the sum of the sent credits. The problem is if the database grows the sum will take a while.
Can you give me any acceptable, scalable and robust solution for this with NoSQL? (I know it is really easy in RDBMS)
Upvotes: 0
Views: 54
Reputation: 1296
Two phase commits in MongoDB
You can perform multi-document updates or multi-document transactions using a two phase commit approach.
Using two-phase commit ensures that data is consistent and, in case of an error, the state that preceded the transaction is recoverable. During the procedure, however, documents can represent pending data and states.
For more details about Two phase commits you can refer to the docs here
NOTE
Because only single-document operations are atomic with MongoDB, two-phase commits can only offer transaction-like semantics. It is possible for applications to return intermediate data at intermediate points during the two-phase commit or rollback.
Upvotes: 1