Question

In a scenario where there are users that have posts, and each user has a view representing a news feed (much like with a logged in Tumblr account), and each post overview has a link to the comments with a comment counter per post, what is the best caching strategy here (On a Rails 4 stack)?

Assuming 5 users, A B C D E, with each being subscribed to the 2 users on their right (A is subscribed to B and C, B is subscribed to C and D etc.) and only having the users they've subscribed to showing up on their news feed view.

Edit:

Assume a fan-out-on-write approach is taken, where each user has a unique set (of post ids) in Redis, and on every post create, the id of the new post is appended to every of the post creator's friends' sets. The redis sets act as an index and a user's feed is fetched via a single SQL query.

Bearing this in mind, caching each feed should be a matter of this approach:

  1. Check set in redis (first hit)
  2. write @feed_array to memcached
  3. fetch posts with single SQL command and save to @feed
  4. write @feed to memcached
  5. Check set in redis (second hit)
  6. If set values match @feed_array then return @feed from memcached. Otherwise new SQL query and override @feed in memcached

This approach would mean easy cache use for the views when iterating through the @post divs, but how would one handle the comment counts?

Était-ce utile?

La solution

unrelated of the application stack that you are using, i don't think that a caching approach scales in your situation. twitter-like functionality is often handled by de-normalization.

in your situation, this could mean implementing a feed model for each user, appending new posts of the followers, so that it is fast to load the 'timeline' of a user from his own feed, instead of joining all his (possible thousands) of friends.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top