title: Storing references between objects in database vs. in recommendation system?
body: <p>For a web app with a recommendation system powered by machine learning, we're pondering where to store references between objects; in our database tables or within our recommendation system (accessed via API)?</p>
  
  <h2>A simplified case</h2>
  
  <p>The system works like this: </p>
  
  <ol>
  <li><p>User selects one or more ingredients</p></li>
  <li><p>Corresponding uid's for selected ingredients sent to recommendation API</p></li>
  <li><p>API returns list of ingredient id's (the recommendations)</p></li>
  <li><p>Data for list of ingredients retrieved from database (e.g. names, images etc.) and displayed on webpage</p></li>
  </ol>
  
  <p>On that same webpage, for each ingredient listing, the following referenced information should show:</p>
  
  <ul>
  <li>category</li>
  </ul>
  
  <p><em>Note: This is an oversimplified example. In reality, we have to deal with hundreds of references like this</em></p>
  
  <hr>
  
  <p><strong>The conventional solution - To store references inside tables</strong></p>
  
  <p>In this case the API simple returns a list of recommended ingredient id's:  </p>
  
  <pre><code>{
      recommendations: [ingredient1, ingredient2 ...] 
  }
  </code></pre>
  
  <p>These id's are used to retrieve ingredient info and category information (names, images etc...) from our database:</p>
  
  <pre><code>- ingredient
      - id
      - name
      - imageUrl
      - categoryId --&gt; Reference to category table
  
  - category
      - id
      - name
      - iconUrl
  </code></pre>
  
  <hr>
  
  <p><strong>The alternative solution - To store references inside recommendation system</strong></p>
  
  <p>In this case, the API would sent back a list of ingredient objects <strong>incl. references</strong>:</p>
  
  <pre><code>{
      ingredientId1: {
          categoryReference: categoryId
          ...
      },
      ingredientId2: {
          categoryReference: categoryId
          ...
      }
      ...
  }
  </code></pre>
  
  <p>So that the database can be kept simple without references:</p>
  
  <pre><code>- ingredient
      - id
      - name
      - imageUrl
  
  - category
      - id
      - name
      - iconUrl
  </code></pre>
  
  <p>What we like about this "alternative" solution is that it makes changing references super easy. No hundreds of reference columns in database tables to maintain.</p>
  
  <p>Super keen on hearing your thoughts based on your past experiences.</p>
  
  <p>Thanks in advance</p>