views:

37

answers:

1

How scalable is memcache in an enviornment where a cache is potentially getting expired every second. In fact, my question is not just about scalability of memcached but about situations where a model is continuously changing and the best way to scale that type of environment. One might say, why cache if the cache is getting expired every second.

Consider this in an hypothetical app, where people are posting marking posts as favorites and let's just consider that there are thousands of people constantly marking posts favorite and creating a favorite record as a result. With each insertion the post view needs to be updated to show the current stats about posts, how many people made it favorite, a user's favorite count etc etc.

We were thinking this could be cached to show only a snapshot taken every x many minutes..but is there a good way to make this more real time in rails?

A: 

Try such algo:

  1. Update db records for post with +1 vote
  2. Create cache value || update existing cache +1 for post fav count
  3. Create cache value || update existing cache +1 for user fav count

Because cache has no transactions values may be not consistent with current DB values - rare parallel read and write race condition may occur. But these inconsistencies will expire as soon cache entry invalidates and values are recalculated from DB.

In above scenario cache sweeping operation may be performed every minute or even every hour - depending how accurate stats should be. Remember that you don't lose anything - all accurate data is kept in DB.

Most important here is that users see realtime value change for vote. Memcached read/write cost is definitely less than DB here because memcache is just multi-access hash store with no transactions.

gertas