Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No. CouchDB uses an "optimistic concurrency" model. In the simplest terms, this just means that you send a document version along with your update, and CouchDB rejects the change if the current document version doesn't match what you've sent.</p> <p>It's deceptively simple, really. You can reframe many normal transaction based scenarios for CouchDB. You do need to sort of throw out your RDBMS domain knowledge when learning CouchDB, though. It's helpful to approach problems from a higher level, rather than attempting to mold Couch to a SQL based world.</p> <p><strong>Keeping track of inventory</strong></p> <p>The problem you outlined is primarily an inventory issue. If you have a document describing an item, and it includes a field for "quantity available", you can handle concurrency issues like this:</p> <ol> <li>Retrieve the document, take note of the <code>_rev</code> property that CouchDB sends along</li> <li>Decrement the quantity field, if it's greater than zero</li> <li>Send the updated document back, using the <code>_rev</code> property</li> <li>If the <code>_rev</code> matches the currently stored number, be done!</li> <li>If there's a conflict (when <code>_rev</code> doesn't match), retrieve the newest document version</li> </ol> <p>In this instance, there are two possible failure scenarios to think about. If the most recent document version has a quantity of 0, you handle it just like you would in a RDBMS and alert the user that they can't actually buy what they wanted to purchase. If the most recent document version has a quantity greater than 0, you simply repeat the operation with the updated data, and start back at the beginning. This forces you to do a bit more work than an RDBMS would, and could get a little annoying if there are frequent, conflicting updates.</p> <p>Now, the answer I just gave presupposes that you're going to do things in CouchDB in much the same way that you would in an RDBMS. I might approach this problem a bit differently:</p> <p>I'd start with a "master product" document that includes all the descriptor data (name, picture, description, price, etc). Then I'd add an "inventory ticket" document for each specific instance, with fields for <code>product_key</code> and <code>claimed_by</code>. If you're selling a model of hammer, and have 20 of them to sell, you might have documents with keys like <code>hammer-1</code>, <code>hammer-2</code>, etc, to represent each available hammer.</p> <p>Then, I'd create a view that gives me a list of available hammers, with a reduce function that lets me see a "total". These are completely off the cuff, but should give you an idea of what a working view would look like.</p> <p><strong>Map</strong></p> <pre><code>function(doc) { if (doc.type == 'inventory_ticket' &amp;&amp; doc.claimed_by == null ) { emit(doc.product_key, { 'inventory_ticket' :doc.id, '_rev' : doc._rev }); } } </code></pre> <p>This gives me a list of available "tickets", by product key. I could grab a group of these when someone wants to buy a hammer, then iterate through sending updates (using the <code>id</code> and <code>_rev</code>) until I successfully claim one (previously claimed tickets will result in an update error).</p> <p><strong>Reduce</strong></p> <pre><code>function (keys, values, combine) { return values.length; } </code></pre> <p>This reduce function simply returns the total number of unclaimed <code>inventory_ticket</code> items, so you can tell how many "hammers" are available for purchase.</p> <p><strong>Caveats</strong></p> <p>This solution represents roughly 3.5 minutes of total thinking for the particular problem you've presented. There may be better ways of doing this! That said, it does substantially reduce conflicting updates, and cuts down on the need to respond to a conflict with a new update. Under this model, you won't have multiple users attempting to change data in primary product entry. At the very worst, you'll have multiple users attempting to claim a single ticket, and if you've grabbed several of those from your view, you simply move on to the next ticket and try again.</p> <p>Reference: <a href="https://wiki.apache.org/couchdb/Frequently_asked_questions#How_do_I_use_transactions_with_CouchDB.3F" rel="noreferrer">https://wiki.apache.org/couchdb/Frequently_asked_questions#How_do_I_use_transactions_with_CouchDB.3F</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload