Opensource Cache/Caching Solutions in Java
By Vishal Sharma
Background:
In computer science, a cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (due to slow access time) or to compute, relative to the cost of reading the cache. In other words, a cache is a temporary storage area where frequently accessed data can be stored for rapid access. Once the data is stored in the cache, future use can be made by accessing the cached copy rather than re-fetching or recomputing the original data, so that the average access time is lower. Cache, therefore, helps expedites data access otherwise the CPU would need to fetch from main memory. Cache have proven to be extremely effective in many areas of computing because access patterns in typical computer applications have locality of reference.
Purpose :
The main purpose of the caching service is to improve server performance by managing static and non static data members. The performance gain is from reducing the number of trips to the database or other external sources of information, avoiding the cost of repeatedly recreating data (objects in java), sharing data between threads in a process and, when possible between processes, and efficient use of process resources.
Advantages:
- In a multi-tiered (n-tier) application, data access is an expensive operation, compared to any other task. By keeping the frequently accessed data around and not releasing it after the first use, we can avoid the cost and time required for the reacquisition and release of the data. This results in greatly improved performance, since we won't be hitting the database every time we need the data.
- Scalability is another reason of using. Since cached data is accessed across multiple tiers, sessions, components, caching can become a big part of a scalable application design.
Disadvantages:
- Synchronization complexity: Depending on the kind of data, complexity increases, because consistency between the state of the cached data and the original data in the data source needs to be ensured. Otherwise, the cached data can get out of sync with the actual data, which will lead to data inaccuracies.
- Durability: Changes to the cached data can be lost when the server crashes. This problem can be avoided if a synchronized cache is used.
- Memory size: JVM memory size can get unacceptably huge if there is lot of unused data in the cache and the data is not released from memory at regular intervals.
Opensource Caching solutions in Java:
There are lots of caching solution in Java, free and non free. This is a list of opensource caching solutions available in Java/J2EE/JEE domain for software development or developing enterprise applications.

Bamboo DHT - is a distributed hash table, or DHT, is a building block for peer-to-peer applications. At the most basic level, it allows a group of distributed hosts to collectively manage a mapping from keys to data values, without any fixed hierarchy, and with very little human assistance. This building block can then be used to ease the implementation of a diverse variety of peer-to-peer applications such as file sharing services, DNS replacements, web caches, etc.
cache4j - is a cache for Java objects with a simple API and fast implementation. It features in-memory caching, a design for a multi-threaded environment, both synchronized and blocking implementations, a choice of eviction algorithms (LFU, LRU, FIFO), and the choice of either hard or soft references for object storage.
FreePastry - is a modular, open source implementation of the Pastry p2p routing and location substrate. Pastry is a generic, scalable and efficient substrate for peer-to-peer applications. Pastry nodes form a decentralized, self-organizing and fault-tolerant overlay network within the Internet. Pastry provides efficient request routing, deterministic object location, and load balancing in an application-independent manner. Furthermore, Pastry provides mechanisms that support and facilitate application-specific object replication, caching, and fault recovery. The proponents of Pastry work for Microsoft, however they've made an excellent choice sticking with Java.
EHCache - is a pure Java, in-process cache. It started as a replacement for JCS. EHCache is faster than JCS. It has following features: Fast,Simple, pluggable cache for Hibernate, small foot print, minimal dependencies, fully documented and production tested.
Java Caching System (JCS) - is a distributed caching system written in java for server-side java applications. It is intended to speed up dynamic web applications by providing a means to manage cached data of various dynamic natures. Like any caching system, the JCS is most useful for high read, low put applications. Dynamic content and reporting systems can benefit most. However, any site that repeatedly constructs pages, drop downs, or common search results form a database that is updated at intervals (rather than across categories continuously) can improve performance and scalability by implementing caching. Latency times drop sharply and bottlenecks move away from the database in an effectively cached system.
It provides several important features, necessary for any Enterprise level caching system, features include Memory management, disk overflow, element grouping, quick nested categorical removal, data expiration, extensible framework, fully configurable run time parameters, remote synchronization, remote store recovery, non-blocking "zombie" pattern, optional lateral distribution of elements, remote server clustering and failover. These features provide a framework with no point of failure, allowing for full session failover including session data across up to 256 servers.
JBoss Cache - is a product designed to cache frequently accessed Java objects in order to dramatically improve the performance of e-business applications. By eliminating unnecessary database access, JBoss Cache decreases network traffic and increases the scalability of applications. But JBoss Cache is much more than a simple cache. JBoss Cache provides fully transactional features as well as a highly configurable set of options to deal with concurrent data access in the most efficient manner possible for your application. In addition, it is a clustered cache that replicates contents to other cache instances running on separate JVMs, servers or even entire networks, making JBoss Cache a highly efficient library used by application server developers to implement clustering features.
JBossCache consists of two modules
TreeCache - a replicated transactional tree-structured cache .
TreeCacheAop a subclass of TreeCache using AOP to dynamically replicate Plain Old Java Objects - POJO.
JCache - is an effort to make an Open Source version of JSR-107 JCache. Since the JSR-107 hasn't released any specs for years, This version still builds on the original functional specification. It is implemented by some geeks at ThoughtWorks.
Jofti - is a simple to use, high-performance object indexing and searching solution for Objects in a Caching layer or storage structure that supports the Map interface. The framework supports EHCache, JBossCache and OSCache and provides for transparent addition, removal and updating of objects in its index as well as simple to use query capabilities for searching. Features include type aware searching, configurable object property indexing, indexing/searching by interfaces as well as support for Dynamic Proxies, primitive attributes, Collections and Arrays
OceanStore - is a global persistent data store designed to scale to billions of users. It provides a consistent, highly-available, and durable storage utility atop an infrastructure comprised of untrusted servers. OceanStore caches data promiscuously; any server may create a local replica of any data object. These local replicas provide faster access and robustness to network partitions. They also reduce network congestion by localizing access traffic. Pond, the OceanStore prototype, is currently available for download.
Open Terracotta - is Open Source Clustering for Java. Features: HTTP Session Replication, Distributed Cache, POJO Clustering, Application Coordination across cluster's JVM's (made using code injection, so you don't need to modify your code)
OSCache - is a caching solution that includes a JSP tag library and set of classes to perform fine grained dynamic caching of JSP content, servlet responses or arbitrary objects. It provides both in memory and persistent on disk caches, and can allow your site to have graceful error tolerance (eg if an error occurs like your db goes down, you can serve the cached content so people can still surf the site almost without knowing). Take a look at the great features of OSCache.
ShiftOne - is a Java Object Cache library that implements several strict object caching policies, as well as a light framework for configuring cache behavior. It's strict in the sense that each cache enforces two limits in a very strict and predictable way.
SwarmCache - is a simple but effective distributed cache. It uses IP multicast to efficiently communicate with any number of hosts on a LAN. It is specifically designed for use by clustered, database-driven web applications. Such applications typically have many more read operations than write operations, which allows SwarmCache to deliver the greatest performance gains. SwarmCache uses JavaGroups internally to manage the membership and communications of its distributed cache. Wrappers have been written that allow SwarmCache to be used with the Hibernate and JPOX persistence engines.
WhirlyCache - is a fast, configurable in-memory object cache for Java. It can be used, for example, to speed up a website or an application by caching objects that would otherwise have to be created by querying a database or by another expensive procedure
NB: Image sourced from blogs.oracle.com/.
Other useful tips on Java, JEE/J2EE
- Planning SOA
- Java J2EE Interview Question Collection
- Serialization in Java/J2EE - Demystified
- Spring Framework - J2EE Development without EJB
- Creating PDF Documents Dynamically in J2EE, Java Applications
- J2EE Tutorial on JAX WS Style Webservices using JB...
- J2EE Tutorial on Webservices using JBoss
- Online Book on J2EE using JBoss and Eclipse
6 Responses to Opensource Cache/Caching Solutions in Java
Where would something like javaspaces fit into this?
Thanks for the list.
Note: 'E' comes before 'F' :-)
Is there a possibility to get the data which proves the statement that EHCache is faster than JCS Cache ?
Dude,
Did you just regurgitate my original post?
http://www.manageability.org/blog/stuff/distributed-cache-java
C
@C,
I have used quite a lot of different posts to research on and yes i did look at yr content its pretty good mate.
Vishal
The comments re: JBoss Cache are pretty outdated. Since JBoss Cache 2.x, TreeCache => Core Cache, and TreeCacheAOP => POJO Cache.
See http://www.jbosscache.org for more details.
Also, stuff around JSR 107 is inaccurate. JSR 107 is yet incomplete, and probably won't be finalised till Q1 or Q2 2008.
Something to say?