title: Dynamo DB - Concurrent Updates
body: <p>I need to manage concurrent updates in a Dynamo DB Table. After doing a bit of research, I came across two ways to do the same:</p>
  
  <ol>
  <li><blockquote>
    <p>Optimistic Locking through Version IDs - <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaVersionSupportHLAPI.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaVersionSupportHLAPI.html</a></p>
  </blockquote></li>
  <li><blockquote>
    <p>Conditional Save - <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html</a></p>
  </blockquote></li>
  </ol>
  
  <p>Are there any more ways to handle concurrent updates in Dynamo DB?</p>
  
  <p><strong>User Scenario</strong></p>
  
  <p><code>Table A - maintains some kind of state [STARTED, IN_PROGRESS, COMPLETE]</code></p>
  
  <p>The change of state can only be like this.</p>
  
  <pre><code>STARTED -&gt; IN_PROGRESS 
  IN_PROGRESS -&gt; COMPLETE
  IN_PROGRESS -&gt; STARTED (back step)
  </code></pre>
  
  <p>I think I can use both optimistic locking and conditional write here.</p>
  
  <p><strong>Optimistic Locking -</strong></p>
  
  <ol>
  <li>Have a version number. When Thread 1 updates state to IN_PROGRESS, the version number is increased to 1.</li>
  <li>Next if some Thread 2 changes the state back to STARTED, the version number will be increased to 2.</li>
  <li>Later if Thread 1 tries to change the state back to COMPLETE it is going to fail since the Versions would differ.</li>
  </ol>
  
  <p><strong>Conditional Save -</strong></p>
  
  <ol>
  <li>Have a condition to check that when we try to change State to COMPLETE the current state should be IN_PROGRESS and not STARTED.</li>
  </ol>
  
  <p>I think I can use either of the mechanism in this kind of situation. Is that correct? Or are there any specific situations where we should prefer one over the other?</p>