Reputation: 4081
I want to create a payment system and I choose mongodb for database I want to know how can I achieve high consistency?
I want to use Multi-Document Transactions but when I have replica set , the secondaries node may have stale docs , and If I read from stale doc , My system is broken
In mongodb docs , https://docs.mongodb.com/manual/replication/#read-operations
By default, clients read from the primary [1]; however, clients can specify a read preference to send read operations to secondaries. Asynchronous replication to secondaries means that reads from secondaries may return data that does not reflect the state of the data on the primary.
AND
[1] (1, 2) In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with { w: "majority" } write concern. The node that can complete { w: "majority" } writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. When this occurs, clients that connect to the former primary may observe stale data despite having requested read preference primary, and new writes to the former primary will eventually roll back.
Even if I force client to read from primary , In some circumstances two nodes in a replica set may transiently believe that they are the primary, So client may observe stale data
How can I design a payment system in mongoDb? consistency is very important
Upvotes: 0
Views: 1390
Reputation: 18835
In some circumstances two nodes in a replica set may transiently believe that they are the primary, So client may observe stale data
For high consistency of read with multi-document
you can utilise Multi-document transactions (MongoDB v4.0+).
You can specify different transaction read concern to control the consistency and isolation properties of the data read.
Set the read concern to majority and write concern to majority, so that the transaction operations are guaranteed to have read majority-committed data.
How can I design a payment system in mongoDb?
This is a broad question, as it depends on the requirements and use case etc. If you were able to modify the data model design in such way to use single-document
model, you may also consider to use causal consistency.
A causally consistent session denotes that the associated sequence of read and acknowledged write operations have a causal relationship that is reflected by their ordering. Applications must ensure that only one thread at a time executes these operations in a client session.
Upvotes: 1
Reputation: 77
See if you want to build a payment system with MongoDB database you need to maintain transactions manually at some level.
Proposed solution:
Maintain two collection for transactions.
For example, collection 1: Balance : {"account_number" : 123, "amount" : 200}
Now suppose user requested and you need to update amount in balance collection. If you directly apply changes in balance collection it may cause problem of dirty read (for concurrent transaction)
create collection 2: Transaction: {"account_number" : 123 , status : true , last_updated : ISODate("..")}
Transaction collection will maintain on-going transactions in system. if status is true that indicates some transaction for this user is already in process and you need to hold 2nd concurrent request until status of the account number 123 in transaction gets false.
Note: There are many other ways to handle this issue but you can think in this direction based on your exact business requirement.
Upvotes: 0