Caching In Web Applications


 

Caching may help in increasing application performance. Here we will explore ways that will help improve the performance of a Web Application.

What is Cache?

Wiki defines a cache as "a store of things that will be required in future, and can be retrieved rapidly." That is the nub of it. In computer science terms, a cache is a collection of temporary data which either duplicates data located elsewhere or is the result of a computation. Once in the cache, the data can be repeatedly accessed inexpensively.

An application requests data from cache using a key. If the key is not found, the application retrieves the data from a slow data source and puts it into the cache. The next request for a key is serviced from the cache.

cache-hit: When a data element is requested of the cache and the element exists for the given key, it is referred to as a cache hit (or simply ‘hit’).

cache-miss: When a data element is requested of the cache and the element does not exist for the given key, it is referred to as a cache miss (or simply ‘miss’)

clip_image002

Target Usage Areas in a Web Application.

Common factors for which caching can be considered are How many times the cached object can be reused and how long is the response time. Other factor include like how much up to date data the user need. These are some of the factor to be kept in mind before considering caching.

Data Access Layer Caching:

Depending on the data if the data is not changing often there are many queries that can be qualified to be cached and can be served from cache instead of hitting the database.

Data Retrieval from Data Warehouse:

One of the important candidate for cache. As we know there are limited connections to DW and we often get connection limit exceeded error. As DW data is a day old and hitting DW for every call doesn’t make sense. Adding a refresh per day on the cache entity will be good enough.

Web Services Caching

Web services caching can be applied both to client side or server side. The areas specifically are authentication information caching, filtering out calls that result in similar results etc. ( to be investigated ).

Open Source Cache Solutions:

Memcached :

The architecture of Memcached consists of two pieces.

– First is a Memcached server that runs in its own process.

– The Memcached client, the second piece of the Memcached system, does know about each of the servers. The client is responsible for picking up the server for each cache entry and either storing or getting the cache entry

Open source Java caching framework

The Cache framework runs within JVM. The caching framework would create the Cache Manager objects in the same JVM where your application is running.

JSR 107: JCACHE – Java Temporary Caching API :

Specifies API and semantics for temporary, in memory caching of Java objects, including object creation, shared access, spooling, invalidation, and consistency across JVM’s.

The specification request was proposed in 2001 to standardize java application caching. The project is still pending. http://www.h-online.com/open/features/The-H-Half-Hour-Cache-back-Greg-Luck-and-the-return-of-JSR-107-1413380.html

Java Caching System (JCS):

JCS is a distributed caching system written in java. It is intended to speed up applications by providing a means to manage cached data of various dynamic natures.

Like any caching system, JCS is most useful for high read, low put applications. It allows both caching in memory or on disk. JCS supports cluster environment.

EHCache:

EHCache is a fast, lightweight, and easy-to-use in-process cache. It supports read-only and read/write caching, and memory- and disk-based caching.

EHCashe can be used for caching Hibernate (second-level cache), data access objects, security credentials, web pages.

It can also be used for SOAP and RESTful server caching, application persistence, and distributed caching.

OSCache

OSCache is another open-source caching solution. It is part of a larger package, which also provides caching functionalities for JSP pages or arbitrary objects.

It is a powerful and flexible package, which, like EHCache, supports read-only and read/write caching, and memory- and disk-based caching.

It also provides basic support for clustering via either JavaGroups or JMS.

SwarmCache

SwarmCache is a simple cluster-based caching solution based on JavaGroups. It supports read-only or nonstrict read/write caching.

SwarmCache is designed to be integrated in to the persistence layer of a database-driven Java application.

This type of cache is appropriate for applications that typically have many more read operations than write operations.

Comparing Cache Solutions:

EHCache

OSCache

SwarmCache

Java Caching System (JCS):

Cache4J

Fast and Light Weight

Simple, Small foot print, Minimal dependencies

Simple, Small foot print, Minimal dependencies

Simple and small footprint the whole app size is less than 2 MB

fast, reliable, and highly configurable ,

Small and easy to use API Mostly used for Storing POJO objects

Scalable

Provides Memory and Disk stores for scalability into gigabytes,
Scalable to hundreds of caches,
Tuned for high concurrent load on large multi-cpu servers
Multiple CacheManagers per virtual machine

OSCache is capable of caching data in memory (so it is very fast), and/or to disk

SwarmCache is an in-memory cache

Support both memory and disk caching

Cashe4J is an in-memory cache

Flexible

Supports Object or Serializable caching
Support cache-wide or Element-based expiry policies
Provides LRU, LFU and FIFO cache eviction policies
Provides Memory and Disk stores
Distributed
Dynamic, Runtime Configuration of Caches

Multiple caching algorithms are supported such as LRU (Least Recently Used),
FIFO (First In First Out), or unlimited.
It is also possible to plug in your own custom algorithm.

Supports Caching Algorithms, Least Recently Used (LRU),Automatic

Uses LRU (Least Recently Used) and is more like an object cache.

It supports LRU, LFU, and FIFO caching algorithms

Standards Based

Full implementation of JSR107 JCACHE API

Not standard.

not implemented based on standard JSR-107

JCACHE , JSR-107 But it depends on concurent util package before java 5.

Doesn’t follow standard

Distributed Caching

Support for replication via RMI or Jgroups

OSCache can easily be configured to cluster across multiple boxes

Cluster aware

Support distributed cache.

No Support for clustering

Quality

High Test Coverage
Fully documented
Trusted by Popular Frameworks

Project is no more active poor documentation.

Poor Documentation, last update was in 2003

Well documented

No Documentation ( only available in Russian).

JSP Tag Support

No Support

provides caching functionalities for JSP tags,
pages fragments cache, pages or arbitrary objects

No support

No Support

No Support

Licensing

Apache 2.0 license

Unknown open source.

Lgpl

Apache License

BSD license

Learning Curve and Ease of Usage

easy to configure and learn

easy to configure and learn

complex API

New Term usage and relatively high learning curve

Easy

 

 

Configuring EHCache in Web Applications

 

To use Ehcache in Web Application the following configuration is needed

– Create a simple ServletContextListener and create a new instance of Cache and store it as attribute in servlet context

public void contextInitialized(ServletContextEvent sce) {

ServletContext ctx = sce.getServletContext();

CacheManager singletonManager = CacheManager.create();

Cache memoryOnlyCache = new Cache("dbCache", 0, false, true, 86400,86400);

singletonManager.addCache(memoryOnlyCache);

memoryOnlyCache = singletonManager.getCache("dbCache");

ctx.setAttribute("dbCache", memoryOnlyCache );

}

– To use cache get it from context and pass it to DAO layer to cache the return of the query etc.

– To save an object in Cache

Element resultCacheElement = new

Element(resultSet.getString("EMPLOYEE_NUMBER"), emp);

cacheRef.put(resultCacheElement);

– To get an object from Cache

Element e = cacheRef.get(personId);

if (e != null) {

emp = (Employee)e.getObjectValue(); // get object from cache

}

Testing EHCache Performance

 

Below are the results of Ehcache performance. For the demo we created a simple web application. The application structure is similar to the design of a web application used by Sample Web Application. It utilizes connection pool of Glassfish Server. For testing purpose we made the following tests

1. Calling database 500 times and records the time it takes, similarly we will call Ehcache to get the same.

2. To test how much memory space it takes to store the entire employee from DB to Cache. To store 12K records it takes around 48 MB space.

3. Get 1000 records from database and compare the querying the same from Cache.

clip_image003

 

Sample Usage of EHCache

 

All usages of Ehcache start with the creation of a CacheManager

CacheManager manager = CacheManager.newInstance()

OR

Create a CacheManager specifying the path of a configuration file.

CacheManager manager = CacheManager.newInstance("src/config/ehcache.xml");

 

Performing CRUD operations

 

Put an element into a cache

Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1");
cache.put(element);

Update an element in a cache. Even though cache.put() is used, Ehcache knows there is an existing element, and considers the put an update for the purpose of notifying cache listeners.

Cache cache = manager.getCache("sampleCache1");
cache.put(new Element("key1", "value1"));
//This updates the entry for "key1"
cache.put(new Element("key1", "value2"));

Get a Serializable value from an element in a cache with a key of "key1".

Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Serializable value = element.getValue();

Get a NonSerializable value from an element in a cache with a key of "key1".

Cache cache = manager.getCache("sampleCache1");
Element element = cache.get("key1");
Object value = element.getObjectValue();

Remove an element from a cache with a key of "key1".

Cache cache = manager.getCache("sampleCache1"); cache.remove("key1");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: