Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is @Cacheable aware of the 'Thundering Herd' problem?

The "thundering herd" problem occurs in a highly concurrent environment (typically, many users). When many users make a request to the same piece of data at the same time, and there is a cache miss (the data for the cached element is not present in the cache) the thundering herd problem is triggered.

I couldn't find an evidence that ehcache-spring-annotations is adressing this problem.

Do I've to write a wrapper and use the explicit locking mechanism?

like image 570
stacker Avatar asked Sep 01 '11 15:09

stacker


1 Answers

The short answer to your question is "no". Cache annotations are intended to be somewhat general-purpose, whereas solutions to problems like "thundering herd" are implementation-specific.

By your followup comment I'm going to assume you're using Ehcache as the implementation. The page you referenced that describes the problem offers a couple of solutions, like using BlockingCache as a decorator for the underlying cache. (The fact that they document such solutions implies that Ehcache does not handle the "thundering herd" problem by default.)

BlockingCache seems like the most direct solution, so I'd start with that. Using BlockingCache programmatically is easy enough, but using it by configuration takes a little more work on your part. You will need to write your own BlockingCacheDecoratorFactory by extending Ehcache's CacheDecoratorFactory. Once you've done that you can configure it in ehcache.xml for any cache that needs it. But do so with care; turning a cache into a BlockingCache needlessly can adversely affect performance.

Say you wrote your own decorator factory org.stacker.cache.BlockingCacheDecoratorFactory and you've got a cache named "adImages" that you want to protect from the "thundering herd" problem. Your new ehcache.xml entry might look like this:

<cache name="adImages"
           maxElementsInMemory="5000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="3600"
           overflowToDisk="false">
  <cacheDecoratorFactory class="org.stacker.cache.BlockingCacheDecoratorFactory" />
</cache>

Check the user's guide at http://ehcache.org/documentation/user-guide/cache-decorators to read about cache decorators in Ehcache. I hope this helps.

like image 89
RichW Avatar answered Sep 24 '22 00:09

RichW