I have to do transactions (begin, commit or rollback), locks (choose for update). How do i get it done inside a document model db?


The situation is:

  • I wish to run an auctions site.
  • And i believe how you can direct purchase too.
  • Inside a direct purchase I must decrement the amount area within the item record, but only when the amount is more than zero. That's why I want locks and transactions.
  • I'm not sure how you can address that without locks and/or transactions.

Can One solve this with CouchDB?

No. CouchDB uses an "positive concurrency" model. Within the easiest terms, this means that you simply send a document version together with your update, and CouchDB rejects the modification when the current document version does not match what you've sent.

It's stealthily simple, really. You are able to reframe many normal transaction based situations for CouchDB. You need to kind of get rid of your RDBMS domain understanding while studying CouchDB, though. It's useful to approach problems from the greater level, instead of trying to mold Couch to some SQL based world.

Monitoring inventory

The issue you layed out is mainly a listing problem. For those who have a document explaining a product, also it features a area for "quantity available", you are able to handle concurrency issues such as this:

  1. Retrieve the document, be aware from the _rev property that CouchDB transmits along
  2. Decrement the amount area, whether it's more than zero
  3. Send the up-to-date document back, while using _rev property
  4. When the _rev matches the presently saved number, be achieved!
  5. If there is a conflict (when _rev does not match), retrieve the latest document version

In cases like this, you will find two possible failure situations to consider. If the newest document version includes a volume of , you handle it exactly like you would inside a RDBMS and alert the consumer they can't really buy the things they desired to purchase. If the newest document version includes a quantity more than , you just repeat the operation using the up-to-date data, and begin back at the start. This can make you perform a little more work than an RDBMS would, and may obtain a little annoying if you will find frequent, conflicting updates.

Now, the solution I simply gave presupposes that you are likely to do things in CouchDB in exactly the same that you'd within an RDBMS. I would approach this issue a little in a different way:

I'd begin with a "master product" document which includes all of the descriptor data (title, picture, description, cost, etc). Then I'd add an "inventory ticket" document for every specific instance, with fields for product_key and stated_by. If you are selling one of hammer, and also have 20 of these to market, you may have documents with secrets like hammer-1, hammer-2, etc, to represent each available hammer.

Then, I'd produce a view that provides me a listing of accessible hammers, having a reduce function that allows me visit a "total". They are completely from the cuff, but should provide you with a concept of such a working view would seem like.



amplifier&lifier doc.stated_by == null ) )


This provides me a listing of accessible "tickets", by product key. I possibly could grab several these when someone really wants to purchase a hammer, then iterate through delivering updates (while using id and _rev) until I effectively claim one (formerly stated tickets can lead to an update error).


function (secrets, values, mix) 

This reduce function simply returns the entire quantity of unclaimed inventory_ticket products, to help you tell the number of "hammers" are for sale to purchase.


This solution signifies roughly 3.a few minutes of total thinking for that particular problem you've presented. There might be possible ways to do this! Nevertheless, it will substantially reduce conflicting updates, and decreases on the necessity to react to a conflict with a brand new update. Under this model, you will not have multiple customers trying to alter data in primary product entry. In the very worst, you will have multiple customers trying to assert just one ticket, and when you've snapped up a number of individuals out of your view, you just move onto the following ticket and check out again.