title: How to handle data inconsistency
body: <p>My applications is on Spring MVC 4.2 with postgres database. In my application, we are consuming an API which is written using spring boot having its own database (mysql).</p>
  
  <pre><code>@Transaction(rollbackfor = Exception.class)
  updateOrder(Order order) {
  
      // This insert is part of my application
      update(order); //**STEP - 1**
  
     //This is not part of our application &amp;
     // happening in api written in spring boot.
     Integer transactionId = updateOrderWorkflow(order);// **STEP - 2**
  
     //Below updateOrderWithTransactionId is part of my application
     //Updates the order with the transaction Id
     updateOrderWithTransactionId(order, transactionId); //**STEP - 3**
  }
  </code></pre>
  
  <p>If STEP-3 fails, then I have to rollback the changes made in the consuming api. For rolling back I have written compensation/rollback method, which rolls back to the old workflow status.</p>
  
  <p><strong>Now the problem scenario:</strong></p>
  
  <p>If one process (PROCESS_1) is working on the above updateOrder() method and reaches to STEP-3, but before this process FAILS in STEP-3, another process (PROCESS_2) tries to access the updateOrder() method and updates STEP-2. Now PROCESS_1 fails in STEP-3 and it calls compensation/rollback method, but PROCESS_2 completes STEP-3 successfully.</p>
  
  <p>This creates data inconsistency. How to handle this situation?</p>