title: Generate an auto incrementing string with multiple executions
body: <p>I'm trying to generate invoice numbers that auto-increment: 2019-0001, 2019-0002, and so on.</p>
  
  <p>The code works fine, but the problem arises with race conditions. If payments happen simultaneously I'm sometimes left with duplicate invoice numbers. </p>
  
  <p>I've thought of just creating an auto_increment field in the database but we have a requirement that allows the user to set the starting number of the invoice. For example a user can set their invoices to start with 2019-0002 and then increment from there.</p>
  
  <p>Is there a simple way to avoid duplicates? </p>
  
  <p>Bonus points if the answer is compatible with WordPress as well, since the invoice numbers are custom post type titles, and I can't change the table design of posts. </p>