Caching is a building block of modern JEE applications. In order to handle any significant load you will need to establish a 2nd level cache. Let's just clear a common confusion. A 2nd level cache is not the same as the cache implemented by databases. Those are 1st level caches and they usually cache data blocks, not specific objects. 2nd level cache is the cache implemented outside of the database.
In the JEE world caching is specified by the the JCache JSR. The two major open source providers of caching are TreeCache from JBoss and EhCache. Both allow the creation of a distributed cache and provide built in mechanisms to deal with dirty data. Also, both libraries work with Hibernate as 2nd level cache providers.
I have had good experiences with both libraries as an architect. The overhead of both libraries is small and they both implement JCache making them interchangeable. Both, EhCache and TreeCache work in the same JVM as the application server. Surprisingly, in the last few months I have encountered more and more JEE applications using Memcached instead of EhCache or TreeCache.
Memcached is a C++ program that acts as a 2nd level cache. It is the default standard for caching in the LAMP stack. Wikipedia and Facebook base their entire caching infrastructure on Memcached. Besides that, Memcached has APIs for PHP, Perl, Ruby and of course Java. Memcached can also work as cache inside MySQL to keep tables in memory. The Java APIs are pure Java APIs. Memcached also has a distributed mechanism implemented via smart hashing.
I can understand using Memcached in a JEE application to access Memcached infrastructure that many LAMP applications are using. But, to use Memcached as the first option for a JEE application just seems strange to me for the following reasons:
- The Memcached Java APIs are not JCache compliant. Which means you can't change your mind about them later on without paying a major price.
- The application server needs to connect to the Memcached process via sockets calls. This requires the serialization and transmission of objects between processes. Something avoided with the Cache being inside the application server.
- The JCache solutions just appear to be much faster with Java.
In order to see if my last point was valid I ran a small test. In a Linux dual core box with 4 Gigs of memory, I created a small Java application that tested both options. The application is a just a simple engine that puts and gets objects from the cache. The objects that it puts are a simple class representing personal information.
Memcached was running in the same machine as the Java program doing the testing. I first started by testing 10000 puts and 10000 gets. Then I moved to 20000 puts and 20000 gets. I kept doing the same until I got to 100000 puts and gets. I measured the average time in milliseconds to do 10000 puts and 10000 gets from the cache. I compared the results of the Memcached test against running the same test for EhCache. The object I was placing in the cache was exactly the same in both cases, and the engine did not know what type of cache it was using due to an abstraction. I used the spymemcache Java API for Memcached.
These are the results. The numbers represent the average time it takes to perform 10000 puts or 10000 gets in milliseconds.
| EhCache | Memcached | |
| put | 31 | 545 |
| get | 16 | 2072 |
As we can see EhCached is one order of magnitude faster for put operations and two orders of magnitude faster get operations.
I was planning to do a more complete test using various caching servers to test the distributed capabilities of each solution. But, after the results I decided I had enough information. I believe that we should not use Memcached for a JEE application unless, there is a necessity to interact with an existing Memcached cache.
Please email me if you want the source code of my tests.
Links
thank you for your sharing ,you did a good job
Posted by: uggs outlet | December 12, 2010 at 06:26 PM
Grand article.
Quelque chose que je n'avais jamais réalisé.
Nous les hommes ont besoin, dans les domaines d'enseignement.
Les garçons ont besoin de cette main lourde de l'ordre, qui vient naturellement à partir d'un homme en charge.
Aucun enfant doit prendre des médicaments soit.
En tant qu'homme, je me trouve entre les mains de beaucoup de femmes abusive tous les cas, toute ma vie.
Posted by: cheap supra shoes | December 22, 2010 at 12:57 AM
I would like to thank you for sharing this great information with us. I am really glad to learn about this because it helps me to increase my knowledge.
Posted by: buy zoloft | January 05, 2011 at 11:12 PM
He is a good friend that speaks well of us behind our backs.
*_*
Posted by: taobao shop | January 12, 2011 at 04:35 PM
The solutions shown in this article provide a solid foundation for developing JEE applications. JEE development can be faster, more agile and, maybe most important, more fun!
Payday loans overnight
Posted by: Payday loans overnight | January 15, 2011 at 02:10 AM
Can i Insatll Memcached on HP-UX server. If ok kindly suggest docs for the same.
Posted by: madhusudhan | February 28, 2011 at 06:41 AM
Can you please send me the source code of your tests?
Posted by: Nitin | March 08, 2011 at 03:28 AM
Any recommendation what would be the best Cache to use?
Currently we are using spring MVC and running in resin and solaris sparc 8G mem
Posted by: Orlando | April 01, 2011 at 08:42 AM
Thankfully there is another solution. JBoss has a Binding Manager service. You can configure this service to bind ports defined in a configuration file. JBoss also provides a sample configuration file with 4 sets of ports. In order to use this service you need to do the following (JBOSS_HOME represents the directory where JBoss is installed and SERVER_NAME represents the server instance you are working with.
Posted by: flexible led strips | May 20, 2011 at 02:33 AM
o me this test just does not make any sense, a single box test on a highly scalable distrubuted caching system like memcached? hmm.
Posted by: burberry sale | July 19, 2011 at 06:18 PM
How handy your blog is. But some pics may perhaps be missed as I can only see a number of. Is it my pc issues or anything at all else. I'm keen to view all the picutures.
Posted by: Cheap Vibram Five Fingers | July 19, 2011 at 11:46 PM
How handy your blog is. But some pics may perhaps be missed as I can only see a number of. Is it my pc issues or anything at all else. I'm keen to view all the picutures.
Posted by: Cheap Vibram Five Fingers | July 19, 2011 at 11:46 PM
If you would go up high , then use your own legs! Do not let yourselves carried aloft; do not seat yourselves on other people's backs and heads .
Posted by: Ed Hardy Outlet | July 28, 2011 at 12:00 AM
I really like the way you show to us, very wonderful and powerful about your article, and good to read it, please keeping it doing
Posted by: Ken Griffey Jr Shoes | August 12, 2011 at 06:12 PM
so good
Posted by: Dr Dre Solo | October 24, 2011 at 01:25 AM
Thats not cool - Memcached Java APIs are not JCache compliant
Posted by: cheap web hosting | October 27, 2011 at 05:11 PM
Online has a "be nice" commenting policy. Don't say anything here you wouldn't say to a person's face. We will use our discretion in removing comments we find offensive, spammy, self-promotional, or mean-spirited. Welcome to ouronline shopping mall about eletronic eventspage.Hope this helps and at least helps narrow down the search.
http://www.sencart.com
Posted by: nick | November 08, 2011 at 11:59 PM
Worldwide store is inside, you can choose all kinds of products that we provide different will
make you satisfied, welcome to our
website, http://www.sencart.com
Posted by: robin | November 09, 2011 at 06:37 PM
When a dog bites a man that is not news , but when a man bites a dog that is news.
Charls A.Dana, American journalist
Posted by: ralphlaurenfrr | December 01, 2011 at 11:25 PM
The poet's voice need not merely be the record of man , it can be one of the props , the pillars to help him endure and prevail.
William Fulkner, American writer
Posted by: moncleroutlet | December 01, 2011 at 11:26 PM
Better late than never
Posted by: outlet moncler | December 07, 2011 at 06:27 PM
What if the total cached objects exceed 2GB? Jcache and ehcache are serving as a quick solution more likely. however, when you need to deal with big data and distributed computation, memcached is probably a better off solution.
Posted by: Ligerdave | January 24, 2012 at 10:09 AM