What is JBOSS Cache ?
JBoss Cache is a replicated and transactional cache that can be used to manage Java objects within a local process or across distributed processes.
Need For JBoss Cache
With today’s large-scale enterprise applications, where scalability and high performance are required in-memory caching has a lead role to play. An in-memory cache can store either application state information or database query results. As many enterprise applications run in clustered environments, the cache needs to be replicated across the cluster. Further, if greater reliability is needed, the cache should also be persisted to the hard disk or database.
JBoss Cache provides a powerful and easy to use the system for caching frequently accessed data. JBoss Cache eliminates the time-consuming process of accessing the database for each request. JBoss Cache dramatically improves application performance by managing local copies of Java objects that are expensive to retrieve or create. Optimized database access decreases network traffic, improves application scalability, and increases application performance. Since JBoss Cache automatically replicates data synchronously or asynchronously, it enables data to be easily clustered across an entire group, thereby increasing the performance and minimizing operating costs. JBoss Cache is purely Java based and therefore it is interoperable with most operating systems capable of running a Java Virtual Machine (JVM); including Windows, UNIX, and Linux. Also, JBoss Cache is interoperable with any JDBC compliant databases including Oracle, SQL Server, MySQL, and others.
JBoss Cache enables the e-business applications to scale at peak traffic times without the cost of more powerful servers. Caching frees up valuable system resources by locally storing and distributing data across the application middle tier rather than storing it in one centralized place. Locally cached data reduces network latency, distributes system load, and eliminates bottlenecks – enabling organizations to better manage the costs associated with scaling up applications.
-
Tree Cache
-
Tree CacheAOP (POJO Cache)
JBoss Tree Cache
Tree Cache is a tree-structured replicated transactional cache, that can be used to replicate data transactionally across multiple processes. So if we add an element to one tree, it will appear in all the trees in the cluster. A cache can either be local or replicated. Replication can be either asynchronous or synchronous. Asynchronous replication blocks the caller until all trees in a cluster have been updated whereas the synchronous replication simply puts the modification(s) on the bus and returns immediately, with replication going on in the background. JBossCache can be used transactionally, meaning that if you have a transaction, JBossCache will collect all modifications and only replicate at the end of the transaction. We can configure the JBoss cache through a configuration XML file or we can set it programmatically through it’s get/set methods.
JBoss Tree Cache can be used in three modes :
1. as a purely local cache, without any replication going on
2. as a replicated cache, using asynchronous replication. Here there will be a primary server that updates the tree, and the updates are replicated to all backup servers either periodically, or immediately, but always in the background. It doesn’t block the caller that makes the modifications in the primary server.
3. as a replicated cache, using synchronous replication. Here, there will be 2 phase commit protocol running across all the trees in the cluster to ensure consistency between the trees. This is used when we require an acknowledgment to make sure the modifications have been applied to all trees in the cluster after returning from a call.
JBoss TreeCacheAOP
TreeCacheAop extends the functionality of TreeCache but behaves as a true object cache providing transparent and finer-grained object mapping into the internal cache. TreeCacheAop is a subclass of TreeCache, that uses instrumentation of class code to know when the state of an object has been changed. This helps to track changes to objects in the TreeCacheAop, and only replicate the changes at the end of a transaction. So if we have a big object and if we want to make some small changes in the object, so far we would have had to serialize the entire object and send it across the wire, wasting bandwidth and CPU cycles for serialization. In the TreeCache AOP case since we track changes to this object, only the changes will be replicated. So this will give us good performance, and not because AOP is so fast, but because we can reduce serialization and unnecessary network traffic.
Cache Loaders
A Cache Loader is the connection of JBossCache to a (persistent) data store. The Cache Loader is called by JBossCache to fetch data from a store when that data is not in the cache, and when modifications are made to data in the cache the Cache Loader is called to store those modifications back to the store. A CacheLoader is a class implementing org.jboss.cache.loader.CacheLoader. It is configured through an XML file.
JBossCache with a CacheLoader allows a user to maintain a bounded cache for a large backend datastore. Frequently used data is fetched from the datastore into the cache, and the least used data is evicted, in order to provide fast access to frequently accessed data. All this is configured through an XML file, and the programmer doesn’t have to take care of loading and eviction.
Some of the Cache Loader implementations supported by JBoss cache are :
FileCacheLoader: This implementation uses the file system to store and retrieve data. JBossCache nodes are mapped to directories, subnodes to subdirectories etc. Attributes of a node are mapped to a file data inside the directory.
JDBCCacheLoader: This implementation uses the relational database as the persistent storage.
ClusteredCacheLoader: This implementation queries the rest of the cluster, treating other servers’ in-memory state as a data store.