title: How to handle db concurrency?
body: <p>Let's say I've got 3 tables (<code>LiveDataTable</code>, <code>ReducedDataTable</code>, <code>ScheduleTable</code>).</p>
  
  <p>Basically I've got a stream of events -> whenever I receive an event I write extracted data of this event to <code>LiveDataTable</code>.</p>
  
  <p>The problem is that there's a huge number of events that's why <code>LiveDataTable</code> may become really huge, so I've got another <code>ReducedDataTable</code> where I combine rows from <code>LiveDataTable</code> (think of selecting 100 rows from <code>LiveDataTable</code>, reducing it to 1 row and write it to <code>ReducedDataTable</code> and then deleting these 100 rows from <code>LiveDataTable</code>).</p>
  
  <p>In order to determine the right time of performing these <code>reducing operations</code> there's <code>ScheduleTable</code>. You may think that 1 row <code>ScheduleTable</code> corresponds to <code>1 reducing operation</code>.</p>
  
  <p>I want to be able to support <code>List&lt;Data&gt; getData()</code> method from <code>Interface</code>. There're 2 cases: I should either read from <code>ReducedDataTable</code> only or merge the results from <code>ReducedDataTable</code> and <code>LiveDataTable</code>.</p>
  
  <p>Here's how my caching works step-by-step:</p>
  
  <ol>
  <li>Read 1 row from <code>ScheduleTable</code></li>
  <li>Read from <code>LiveDataTable</code></li>
  <li>Write to <code>ReducedDataTable</code> (at least 4 rows)</li>
  <li>Remove (&lt;= INT_MAX) rows from <code>LiveDataTable</code></li>
  <li>Remove 1 row from  <code>ScheduleTable</code></li>
  </ol>
  
  <p>The problem is I want to determine whether I should read from <code>LiveDataTable</code> and <code>ReducedDataTable</code> programmatically when receiving <code>getData()</code> request. For every step (before #3) I want to read from <code>LiveDataTable</code> and then I'd like to read from <code>ReducedDataTable</code>. How do I determine what step I'm currently at when receiving <code>getData()</code> request?</p>
  
  <p>The reason I asked this questions I believe this's a common problem in DB when handling concurrency.</p>