Tags

, , , , , , , , , , , ,

The default caching option in JForum is a simple Java Map which obviously will be effective only within a single JVM and will work only when the application is running on a single instance of the web server. But in a clustered environment, each JVM will maintain its own cache (because its just a Java Map) and the application will start to behave, well, weirdly !

However, JForum provides many other options for caching and for a clustered environment, JBossCache is readily available. But JBoss requires some amount of configuration and looks complex for the simple caching scenarios that occur in a forum application like JForum. On the other hand, memcached looks simple and easy to use. There are a number of client APIs available and there is no need for any additional configuration.

Since JForum allows developers to use their own caching solutions, I wrote a simple CacheEngine (just a wrapper class for the reads and writes !) and tried it out with JForum. It worked fine but with some data inconsistencies due to the way in which memcached maintains the cached data.

Usually an object stored in, say, a Java array A can be modified in place and A[i] need not be written into again with the modified object since Java handles everything by reference. But when an object is stored into the memcache, it is serialized in-memory and hence when the object is modified it needs to be stored explicitly into the cache again for the new value to get reflected in the cache reads.

This can be a major disadvantage for using memcached in existing applications because that requires changes NOT in the CacheEngine implementation but in the code that modifies cached data and not write it back into the cache(a very common scenario). JForum also had the same problem but I had a go at changing the code and was able to make it work only because of the nice way in which the source code was written – just awesome !

Note: Special Thanks to S for his suggestion to use memcached when I was stuck looking for a simple solution and also for his push to make me document my work on memcached and JForum. This post is at least a start !

Advertisements