title: Database cluster - asynchronous tasks
body: <p>Let's say I have a couchDB database called "products" and a frontend with a form.
  Now if a user opens a document from this database in the form I want to prevent other user from editing this specific document.</p>
  
  <p>Usually pretty simple:</p>
  
  <pre><code>-&gt; read document from couchDB
  -&gt; set a variable to true like: { edit : true }
  -&gt; save (merge) document to couchDB
  -&gt; if someone else tries to open the document he will receive an error, becaus of edit:true.
  </code></pre>
  
  <p>BUT, what if two user open the document at the exact same time?
  The function would be called twice and when the second one opens the document he would falsely receive an edit:false because the first didn't had enough time to save his edit:true. So how to prevent this behaviour?</p>
  
  <p>First solution would be: 
  Build an array as a cue for database requests and dont allow parallel requests, so all requests would be worked off one after another. But in my opinion this is a bad solution because the system would be incredible slow at some point.</p>
  
  <p>Second solution:
  Store the documentIDs of the currently edited documents in an local array in the script. This would work because this is no asynchronous process and the second user would receive his error immediately.</p>
  
  <p>So far so good, BUT, what if some day there are too many user and this system should run in a cluster (the node client server, not the database) - now the second solution would not work anymore because every cluster slave would have its own array of documentIDs. Sharing there would end in another asynchronous task and result in the same problem above.</p>
  
  <p>Now i'm out of ideas, how do big clustered systems usually handle problems like that?</p>