Search code examples
cachingmemcachedhigh-load

How to deal with expiring item (due to TTL) in memcached on high-load website?


When you have peaks of 600 requests/second, then the memcache flushing an item due to the TTL expiring has some pretty negative effects. At almost the same time, 200 threads/processes find the cache empty and fire of a DB request to fill it up again

What is the best practice to deal with these situations?

p.s. what is the term for this situation? (gives me chance to get better google results on the topic)


Solution

  • If you have memcached objects which will be needed on a large number of requests (which you imply is the case), then I would look into having a separate process or cron job that regularly calculated and refreshed these objects. That way it should never go TTL. It's a common trade-off: you add a little unnecessary load during low traffic time to help reduce the load during peaking (the time you probably care the most about).

    I found out this is referred to as "stampeding herd" by the memcached folks, and they discuss it here: http://code.google.com/p/memcached/wiki/NewProgrammingTricks#Avoiding_stampeding_herd

    My next suggestion was actually going to be using soft cache limits as discussed in the link above.