Question & Answer
How does EhCache work in a clustered environment
EhCache is a third party tool used to maintain cache across all the nodes in a clustered environment.
EhCache in itself is not a type of cache and is just a tool. Also, EhCache doesn't not maintain the cache, it just triggers some of the actions/services across the nodes so that each node can re-fresh/update its own cache.
The techniques used for dealing with the cache coherency issues across the cluster nodes include:
1. Pull data from the store (DB) every time it is needed (no caching.) In general, it is an expensive operation and has performance implications in a multi-user web based application.
2. Keep local caches up to date by polling and refreshing at a pre-configured interval.
3. Replicating the cache data across the cluster nodes. When elements are added, modified or deleted from the cache replicate the data across the cluster members and keep them consistent (cache coherency).
4. Keep the local caches up-to-date based on a Distributed Event that notifies the members of the cluster of the changes. When a cached item is modified or a significant event happens that raises an event that is broadcasted to the cluster members which in turn take the necessary actions to bring the local copy up-to-date.
MCS uses the solutions 2, 3 and 4, out of which #2 is a generic implementation. EHCACHE provides underlying support for #3 and #4 and hence the same is used by the application.
GlobalCache (MCS implementation) is used to cache data and optionally expire it based on time to idle parameter.
A system preference "GlobalCache.globalCacheImplClass" dictates the GlobalCache implementation, the default implementation of which is in memory local cache. If the implementation is changed to "com.comergent.globalcache.DistributedCache", it will replicate the caches across the cluster nodes.
Internally, Distributed Cache uses 2 different kinds of caches:-
-- "DistributedGlobalStore" for caching the data to be stored for eternity and,
-- "DistributedGlobalCache" for caching the data where different expiry policies can be applied as supported by EhCache. Expiry policies are specified in the cache configuration file.
The DistributedGlobalCache-Config.xml manages the configuration for the above caches.
Distributed Event Service and a Listener are used to notify the updates to other nodes as well as listen for update notifications from other nodes in the cluster. This service provides a mechanism for notifying cluster members by publishing distributed events. Based on the event, it invalidates a cached attribute based on the business logic. Also it is the individual local cache’s responsibility to invalidate/refresh the local copy upon receiving the update notification.
The DistributedEventService-config.xml manages the above configuration.
Ehcache uses multicast to propagate changes between nodes. DistributedCache leverages this capability and so depends on the proper configuration of ehcache and multicast. MCS uses automatic discovery and multicast host/port that is implemented using the RMI.
(Please refer to FAQ3003 - "What should be the appropriate MultiCastGroup settings in MCS" for more information about MultiCastGroup Settings)
DistributedCache in turn uses ehcache, an open source package from sourceforge (now acquired by Terracotta) as the low level caching infrastructure. Ehcache provides an underlying support for the distributed caching. It is a production ready implementation.
Q. What versions uses EhCache functionality?
A. Version 7.1.1 and above uses EhCache.
Q. Where can more communication options used by EhCache be found?
A. ehcache.xml has detailed explanation for communication options available. Out of the box, MCS uses automatic discovery and multicast host/port in the configuration.
Q. Can AppContextCache if desired be used, as long as one is willing to accept a synchronization delay between nodes because of the polling interval?
A. No, you cannot use AppContextCache in clustered env. You have to use DistributedCache.
Q. What are the types of cache that utilize EhCache functionality?
A. The following uses EhCache:
For further details on EHCACHE, please refer to
Was this topic helpful?
16 June 2018