title: Activity streams / feeds, to denormalize or not?
body: <p>I know variations of this question have been asked many times before <em>(and I've read them, 2 of them being: <a href="https://stackoverflow.com/questions/202198/whats-the-best-manner-of-implementing-a-social-activity-stream">1</a>, <a href="https://stackoverflow.com/questions/1443960/how-to-implement-the-activity-stream-in-a-social-network">2</a>)</em>, but I just can't wrap my head around anything that just feels like the right solution.</p>
  
  <p>Everything has been suggested from many to many relations, to fanout, to polymorphic associations, to NoSQL solutions, to message queues, to denormalization and combinations of them all.</p>
  
  <p>I know this question is very situational, so I'll briefly explain mine:</p>
  
  <ul>
  <li>Many activities that trigger many events.
  
  <ul>
  <li>Following, creating, liking, commenting, editing, deleting, etc.</li>
  </ul></li>
  <li>A user can follow another user's activity <em>(the events they trigger)</em>.</li>
  <li>The most requested events will be the most recent events.
  
  <ul>
  <li>The ability to view past events is desired.</li>
  </ul></li>
  <li>No sorting or searching of the feed is desired past ordering by date desc.</li>
  <li>Scalability is a concern <em>(performance and expandability)</em>.</li>
  </ul>
  
  <p>For the mean time, I ended up going with a denormalized setup basically being made up of an events table consisting of: <code>id</code>, <code>date</code>, <code>user_id</code>, <code>action</code>, <code>root_id</code>, <code>object_id</code>, <code>object</code>, <code>data</code>.</p>
  
  <p><code>user_id</code> being the person that triggered the event.<br>
  <code>action</code> being the action.<br>
  <code>root_id</code> being the user the <code>object</code> belongs to.<br>
  <code>object</code> being the object type.<br>
  <code>data</code> containing the minimum amount of information needed to render the event in a user's stream.</p>
  
  <p>Then to get the desired events, I just grab all rows in which the <code>user_id</code> is the id of a user being followed by whose stream we're grabbing.</p>
  
  <p>It <em>works</em>, but the denormalization just <em>feels</em> wrong. Polymorphic associations seem similarly so. Fanout seems to be somewhere in between, but feels very messy.</p>
  
  <p>With all my searching on the issue, and reading the numerous questions here on SO, I just can't get anything to click and feel like the right solution.</p>
  
  <p>Any experience, insight, or help anyone can offer is <strong>greatly</strong> appreciated. Thanks.</p>