Why not just use the database to handle the &quot;locking&quot; for you? For example, to ensure that an email with ID=123 gets sent only once, just check if &quot;email 123 sent&quot; is in the database, otherwise commit it to the database, wait for the transaction to be committed, and send the email.<p>Edit: Why is this downvoted? It is a serious question.