below is the scenario: here is a access statistic system, just like Blogger's overviewstats function. Statistic data is stored persistent in database(like MySQL), while using a key-value cache(now is memcache) to cache the access counts, each access only update the value in cache.
Now the question is how to synch back the latest count value to database? A normal solution is to write back after some interval, but memcache will discard items when there is no enough spaces, some updates may lost. so I think a better solution is if memcache can send a message(like JMS) when discarding an item, and then i can synch that item to database. It seems that memcache does not provide this function, is there any other key-value cache can do this? Or is there any better solutions?
Memcached is a cache, so you need to use it as one. When you update the access counts in memcached, you should also enqueue the updates so they can be written asynchronously to the database. That way, counts that fall out of the cache can be reloaded from the database.
I like the idea of memcached enqueuing items that are about to be discarded, but it's probably not going to happen in the main project due to performance considerations.