title: How does data denormalization work with the Microservice Pattern?
body: <p>I just read an article on <a href="http://java.dzone.com/articles/microservices-and-paas-part-2">Microservices and PaaS Architecture</a>. In that article, about a third of the way down, the author states (under <strong>Denormalize like Crazy</strong>):</p>
  
  <blockquote>
    <p>Refactor database schemas, and de-normalize everything, to allow complete separation and partitioning of data. That is, do not use underlying tables that serve multiple microservices. There should be no sharing of underlying tables that span multiple microservices, and no sharing of data. Instead, if several services need access to the same data, it should be shared via a service API (such as a published REST or a message service interface).</p>
  </blockquote>
  
  <p>While this <em>sounds</em> great in theory, in practicality it has some serious hurdles to overcome. The biggest of which is that, often, databases are tightly coupled and every table has <em>some</em> foreign key relationship with at least one other table. Because of this it could be impossible to partition a database into <em>n</em> sub-databases controlled by <em>n</em> microservices.</p>
  
  <p>So I ask: <strong>Given a database that consists entirely of related tables, how does one denormalize this into smaller fragments (groups of tables) so that the fragments can be controlled by separate microservices?</strong></p>
  
  <p>For instance, given the following (rather small, but exemplar) database:</p>
  
  <pre><code>[users] table
  =============
  user_id
  user_first_name
  user_last_name
  user_email
  
  [products] table
  ================
  product_id
  product_name
  product_description
  product_unit_price
  
  [orders] table
  ==============
  order_id
  order_datetime
  user_id
  
  [products_x_orders] table (for line items in the order)
  =======================================================
  products_x_orders_id
  product_id
  order_id
  quantity_ordered
  </code></pre>
  
  <p>Don't spend too much time critiquing my design, I did this on the fly. The point is that, to me, it makes logical sense to split this database into 3 microservices:</p>
  
  <ol>
  <li><code>UserService</code> - for CRUDding users in the system; should ultimately manage the <code>[users]</code> table; and</li>
  <li><code>ProductService</code> - for CRUDding products in the system; should ultimately manage the <code>[products]</code> table; and</li>
  <li><code>OrderService</code> - for CRUDding orders in the system; should ultimately manage the <code>[orders]</code> and <code>[products_x_orders]</code> tables</li>
  </ol>
  
  <p>However all of these tables have foreign key relationships with each other. If we denormalize them and treat them as monoliths, they lose all their semantic meaning:</p>
  
  <pre><code>[users] table
  =============
  user_id
  user_first_name
  user_last_name
  user_email
  
  [products] table
  ================
  product_id
  product_name
  product_description
  product_unit_price
  
  [orders] table
  ==============
  order_id
  order_datetime
  
  [products_x_orders] table (for line items in the order)
  =======================================================
  products_x_orders_id
  quantity_ordered
  </code></pre>
  
  <p><strong>Now there's no way to know who ordered what, in which quantity, or when.</strong></p>
  
  <p>So is this article typical academic hullabaloo, or is there a real world practicality to this denormalization approach, and if so, what does it look like (bonus points for using my example in the answer)?</p>