Caching the Hot Stuff with Terracotta
As I’ve been blogging about recently, we have been developing an exam-taking web application at Terracotta to demonstrate the Session Clustering capabilities of Terracotta. Since one of the requirements of this web app is that we support 40,000 concurrent users, we thought we’d better cache the hottest exams (using Ehcache) rather than fetch them from Hibernate each time. Since modifying an exam should occur far, far less frequently than taking an Exam, and since Terracotta already supports Ehcache, this was a no-brainer.
There is an ExamService service, configured as a Spring bean, with DaoExamService being the default implementation:
The findById method is expected to be the most frequently-invoked method. The other two methods shown are administrative functionality, not expected to be used frequently. We want to cache the results of all three methods in a single, clustered Exam cache.
My approach was to write a new service, CachingWrapperExamService, a proxy which owned the cache and which delegated to another ExamService instance:
The following were straightforward changes:
- Once again, our use of interfaces have reaped dividends: Introducing this new Caching
ExamServicewas as easy as tweaking the Spring XML file – the change was completely transparent to all users of theExamServicebean. It also made unit testing easy, as I could create a mockExamServiceto test caching with. - The Maven
pom.xmlhad to be changed to note that Ehcache is now a compile-time dependency, not just a runtime dependency. - The Terracotta config file
tc-config.xmldid not have to be modified, as we were already using the Ehcache TIM, and so our CacheManager was automatically clustered.
And just like that, we have a clustered cache of the hottest exams being used.
Paul Davis:
Pretty cool. I’ve done something similar but, I made the cache transparent to the application.
By using Spring and Spring Modules Cache, I was able to wrap caches around the interfaces for the service that used Hibernate.
https://springmodules.dev.java.net/docs/reference/0.8/html/cache.html
Just a few extra lines in the Spring config and the app is un-aware of the cache. If you want to get *really* crazy, you can pull the cache parameters from JNDI to allow fine tuning without rebuilding the application.
3 December 2008, 11:33 pmScott:
@Paul thanks for the comment. We did actually investigate Spring Modules but decided against it because (a) if I recall, Spring Modules caches on a per-method basis, and one of our methods returned a Collection, so it wasn’t a great fit, (b) a bunch of Spring Modules Maven dependencies weren’t available on a public repository as of September, and (c) the Examinator is only a reference app, so we decided to take the straightforward route in this case.
I loved your recent blog about killing the db w/ Terracotta, by the way.
4 December 2008, 10:16 amTerris Linenbach:
Interesting. If you have terracotta what do you need ehcache for?
22 October 2009, 5:00 pm