Reputation: 3215
Is there a way to create locks in Couchdb while performing bulk save? I believe there is no inbuilt method to achieve this, but would it be possible to solve this with a workaround? I have a scenario similar to following (I use Node.js to interact with couchdb database):
User pays another user creating a new document object as follows:
{
_id: SOMEID
type: "PAYMENT"
username: SOMEUSER
paidto: ANOTHERUSER
amount: 10
}
When this is saved I also need to update remaining balance the user has in his/her account document
(subtract 10 from his/her wallet). Then I save both of these documents at once with bulk_docs (newly created transaction document
and the updated account document
).
But if during this process the user makes changes to the account document
through some other method (let's say in another tab) we have an issue where the new transaction document
is saved while the account document
cannot be updated. This creates a big issue and consistency problem.
To solve this we will have to lock account document
until the bulk save process ends, during which process in second tab waits for the lock to be released.
I am trying to deploy Couchdb is an environment where consistency between documents is quite important but without locks, this is turning out to be very difficult.
Upvotes: 2
Views: 285
Reputation: 3852
When this is saved I also need to update remaining balance the user has in his/her account document (subtract 10 from his/her wallet).
Don't. Banking is a very well documented example for document databases. Best practices is to store only transactions as documents, and consider account balance as a query to a view (of transactions indexed by accounts).
function(o) {
if (o.paidto && o.amount && o.username) {
emit(o.paidto, o.amount);
emit(o.username, -o.amount);
}
}
_sum
GET /mydb/_design/mydesign/_view/myview/?group=true&key=SOMEUSER
Upvotes: 1