title: How to prevent the race condition in this simple example?
body: <p>The code below will fetch a new address from an external API if the user doesn't yet have one:</p>
  
  <pre><code>def create_address
  
    if current_user.address.blank?
      data = AddressAPI.create_address
      current_user.update!(address: data['address'])
    end
  
    render json: { address: current_user.address }
  end
  </code></pre>
  
  <p>If two concurrent <code>create_address</code> requests come in, it's possible they'll both pass the <code>current_user.address.blank?</code> check and two addresses will be created (whatever will call <code>update!</code> last will override the other one).</p>
  
  <p>How do we prevent this? Do we need to use some sort of locking mechanism?</p>