title: synchronizing an external state (e.g. from a db) in erlang
body: <p>Let's say that there's an online ticket-shop that has 100 tickets for sell.
  If there can be many requests that purchase on the tickets at the same time, there's a risk of a race condition, because there can be 2 (or more) requests at the same time trying to buy the last ticket. And if the software counts the tickets already sold, it is possible that the counter will return 99 (so one ticket is left) for two requests coming in at the same time.
  So there's a risk that the software will sell 101 tickets (instead of only 100 tickets).
  In most imperative languages you will probably use something like the keyword &quot;synchronized&quot; (e.g. in Java), to make sure, that counting and selling is &quot;an atomic&quot; operation where no other request can come in, before the previous request has finished processing, right?</p>
  <p>But, how would you do this in erlang?</p>
  <p>I mean there's no &quot;state&quot; in a function, right? And many requests can come in at the same time getting the number 99 from a counter that counts the tickets (e.g. from a db) that are already sold...</p>
  <p>So, what's the way to go in erlang?</p>