title: Best way to handle concurrency issues
body: <p>i have a LAPP (linux, apache, postgresql and php) environment, but the question is pretty the same both on Postgres or Mysql.</p>
  
  <p>I have an cms app i developed, that handle clients, documents (estimates, invoices, etc..) and other data, structured in 1 postgres DB with many schemas (one for each our customer using the app); let's assume around 200 schemas, each of them used concurrently by 15 people (avg).</p>
  
  <p><strong>EDIT</strong>: I do have an timestamp field named last_update on every table, and a trigger that update the timestamp every time the row is update.</p>
  
  <p>The situation is:</p>
  
  <ol>
  <li>People Foo and Bar are editing the document 0001, using a form with every document details.</li>
  <li>Foo change the shipment details, for example.</li>
  <li>Bar change the phone numbers, and some items in the document.</li>
  <li>Foo press the 'Save' button, the app update the db.</li>
  <li>Bar press the 'Save' button after bar, resending the form with the old shipment details.</li>
  <li>In the database, the Foo changes have been lost.</li>
  </ol>
  
  <p>The situation i want to have:</p>
  
  <ol>
  <li>People Foo, Bar, John, Mary, Paoul are editing the document 0001, using a form with every document details.</li>
  <li>Foo change the shipment details, for example.</li>
  <li>Bar and the others change something else.</li>
  <li>Foo press the 'Save' button, the app update the db.</li>
  <li>Bar and the others get an alert 'Warning! this document has been changet by someone else. Click here to load the actuals data'.</li>
  </ol>
  
  <p>I've wondered to use ajax to do this; simply using an hidden field with the id of the document and the last-updated timestamp, every 5 seconds check if the last-updated time is the same and do nothing, else, show the alert dialog box.</p>
  
  <p>So, the page check-last-update.php should look something like:</p>
  
  <pre><code>&lt;?php
  //[connect to db, postgres or mysql]
  $documentId = isset($_POST['document-id']) ? $_POST['document-id'] : 0;
  $lastUpdateTime = isset($_POST['last-update-time']) ? $_POST['last-update-time'] : 0;
  //in the real life i sanitize the data and use prepared statements;
  $qr = pg_query("
      SELECT
          last_update_time
      FROM
          documents
      WHERE
          id = '$documentId'
  ");
  $ray = pg_fetch_assoc($qr);
  if($ray['last_update_time'] &gt; $lastUpdateTime){
      //someone else updated the document since i opened it!
      echo 'reload';
  }else{
      echo 'ok';
  }
  ?&gt;
  </code></pre>
  
  <p>But i dont like to stress the db every 5 seconds for every user that have one (or more...) documents opened.</p>
  
  <p><strong>So, what can be another <em>efficent</em> solution without nuking the db?</strong></p>
  
  <p>I thought to use files, creating for example an empty txt file for each document, and everytime the document is updated, i 'touch' the file updating the 'last modified time' as well... but i guess that this would be slower than db and give problems when i have much users editing the same document.</p>
  
  <p>If someone else have a better idea or any suggestion, please describe it in details!</p>
  
  <p><strong>* - - - - - UPDATE - - - - -  *</strong> </p>
  
  <p>I definitely choosen to NOT hit the db for check the 'last update timestamp', dont mind if the query will be pretty fast, the (main) database server has other tasks to fullfill, dont like the idea to increase his overload for that thing.</p>
  
  <p>So, im taking this way:</p>
  
  <ol>
  <li>Every time a document is updated by someone, i must do something to sign the new timestamp outside the db environment, e.g. without asking the db. My ideas are:
  
  <ol start="2">
  <li>File-system: for each document i create an empry txt files named as the id of the document, everytime the document is update, i 'touch' the file. Im expecting to have thousands of those empty files.</li>
  <li>APC, php cache: this will be probably a more flexible way than the first one, but im wondering if keeping thousands and thousands of data permanently in the apc wont slow down the php execution itself, or consume the server memory. Im little bit afraid to choose this way.</li>
  <li>Another db, sqlite or mysql (that are faster and lighter with simple db structures) used to store just the documents ID and timestamps.</li>
  </ol></li>
  <li>Whatever way i choose (files, apc, sub-db) im seriously thinking to use another web-server (lighttp?) on a sub-domain, to handle all those.. long-polling requests.</li>
  </ol>
  
  <p><strong>YET ANOTHER EDIT:</strong></p>
  
  <p>The file's way wouldnt work.</p>
  
  <p>APC can be the solution.</p>
  
  <p>Hitting the DB can be the solution too, creating a table just to handle the timestamps (with only two column, document_id and last_update_timestamp) that need to be as fast and light as possible.</p>
  
  <p>Long polling: that's the way i'll choose, using lighttpd under apache to load static files (images, css, js, etc..), and just for this type of long-polling; This will lighten the apache2 load, specially for the polling.</p>
  
  <p>Apache will proxy-up all those request to lighttpd.</p>
  
  <p>Now, i only have to decide between db solution and APC solution..</p>
  
  <p>p.s: thanks to all whom already answered me, you have been really usefull!</p>