title: Database-less application using S3 for consistency and data-storage
body: <p>I am using a simple submission application which does following for each visitor:</p>
  
  <ul>
  <li>check the list of directories to find one with largest number in title</li>
  <li>increment that number by one and create new directory</li>
  <li>store the upload into that new directory</li>
  </ul>
  
  <p>I am aware that there is a possible classic race-condition of two simultaneous clients fetching the directory list and determining the same next largest number for them. In order to mitigate the issue, I am using following steps:</p>
  
  <ul>
  <li>first, get the largest number as before and increment it</li>
  <li>as S3's directories are files, store a random number to the newly created directory</li>
  <li>fetch the directory's contents (the actual file's contents) and compare with what was stored in it</li>
  <li>if the numbers differ, that means that some other thread has "won" the race and thus the process starts again from the step one.</li>
  </ul>
  
  <p>Does this look like enough steps? I was thinking, could network congestion cause a process A to store and read its own number and determine it has won and then a process B would come to the same conclusion. How can I mitigate this? I was thinking about introducing an explicit time-out for the operation - e.g. the directory creation needs to take maximum of n seconds and that would be the time a process would wait before checking for the random number. </p>
  
  <p>Or is there any way to do atomic operation of checking the FS contents and creating the directory, or at least doing create-but-not-replace operation? Or create that would fail instead of replacing existing file?</p>
  
  <p>And btw - I am using PHP and the official SDK.</p>