Hibernate Cache
As we all know, there are two types of cache available in Hibernate.
- 1st Level Cache - Hibernate Session
- 2nd Level Cache(L2) - SessionFactory along with the external cache providers
* From here on i refer 2nd Level cache as L2 cache
L2 Cache
L2 cache can store Entities, Collections, Query results & Timestamps. Since Hibernate 3.3 each type gets a separate region to store the data. The data goes into the cache also varies based on the type. Preview on the same are below,
Entities:
- Entity cache does not store instances of an entity, Instead it stores the entities in 'Dehydrated state' (minus associations)
{id -> {attribute1,attribute2,attribute3}}
{1 -> {"name",20, null}}
{2 -> {"a name",30, 4}}
Collections:
- Collection of primary key IDs not the actual dehydrated entities
Query Cache:
- Query cache does not cache the state of the actual entities in the result set;
- It caches only identifier values and value type of the result
- Query along with the parameters are used as a key
{query,{parameters}} -> {id of the entity}
{"from Employee as e where e.joinedDate=:date", [12/07/2011]} -> [3423]
Timestamps:
- Last updated timestamp for each entities.
{"tablename":"timestamp"}
Hibernate 3.3 cache SPI
- Removed synchronization in the Hibernate cache plumbing.
- Provides finer grained control over cache region storage and cache strategies
- Cache providers are deprecated
Distributed cache behaviours
Local vs. Replication vs. Invalidation
Local - As it implies data stored in the cache will be local to the server instance. Data will not be replicated to the other instances in the cluster
Replication - On any change data stored in the cache will be copied to the other instances in the cluster.
Invalidation - On any change invalidation request will be sent to the other instances in the cluster along with the cache id, so that the data in the cache will be removed in other instances.
Synchronous vs. Asynchronous
Defines whether the replication is Sync or Async. If sync then the main thread needs to wait till the replication is over. If Async then the data will be inconsistent till the successfull replication.
Initial state transfer
To keep the newly up server to the same state
Eviction
Remove data from the local cache. If distributed cache then invalidation request will be sent to other instances
Cache provider selection
Note:Infinispan - a cache provider from JBoss, replaces Ehcache as a default cache provider for hibernate since Hibernate 3.5
About Ehcache 2.x
( copied from Ehcache site)
Ehcache 2.0 provides a new Hibernate 3.3 SPI caching plugin, JTA, Write-Behind, a new ultra-fast Bulk Loading API for clustered caches and dynamic runtime cache reconfiguration.
Ehcache 2.0 is fully backward compatible with earlier versions of Ehcache.
The Terracotta Server Array ("TSA") has also been re-engineered to dovetail with Ehcache to provide these features with cluster coherence,high availability and persistence.
This release also introduces some improvements to ehcache which reduce memory use (over 1.6 and 1.7) by the cache and improve the eviction algorithms.
Ehcache distributed cache
The following additional replicated caching mechanisms are available.
- RMI - Point-to-point protocol
- JMS - Not supported by > WAS 5
- Terracotta - Commercial product
- JGroups -Popular & stable, Based on IP Multicasting, JBoss, Tomcat clusters are based on this technology, Complex architecture
My preference would be JGroups than the others, so here i talk about JGroups more
The main features of JGroups
- Group creation and deletion.
- Group members can be spread across LANs or WANs
- Membership detection and notification about joined/left/crashed members
- Detection and removal of crashed members
- Sending and receiving of member-to-group messages (point-to-multipoint)
- Sending and receiving of member-to-member (Unicast) messages (point-to-point)
- Fragmentation of large messages
- Reliable unicast and multicast message transmission. Lost messages are retransmitted
IP Multicasting:
The sender sends a single datagram (from the sender's unicast address) to the multicast address, and the intermediary routers take care of making copies and sending them to all receivers that have registered their interest in data from that sender.
Hibernate 3.3 + Ehcache 2.0
Region factory assignment on session factory configuration,
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory</property>Ehcache + JGroupsConfiguraging JGroups replication needs the below changes in the ehcache.xml
CacheManagerPeerProviderFactory:
This change is common for all the cache types
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups
.JGroupsCacheManagerPeerProviderFactory"
properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;
mcast_send_buf_size=150000;mcast_recv_buf_size=80000):
PING(timeout=2000;num_initial_members=6):
MERGE2(min_interval=5000;max_interval=10000):
FD_SOCK:VERIFY_SUSPECT(timeout=1500):
pbcast.NAKACK(gc_lag=10;retransmit_timeout=3000):
UNICAST(timeout=5000):
pbcast.STABLE(desired_avg_gossip=20000):
FRAG:
pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;
shun=false;print_local_addr=true)"
propertySeparator="::"
/>CacheEventListenerFactory:Below change is unique for each cache types.
<cache
name="com.somecompany.someproject.domain.Country"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicateUpdates=true, replicateUpdatesViaCopy=false,
replicateRemovals=true" />
</cache>Ehcache Replication Properties
Here is the mapping of the Replication properties vs Ehcache properties
Best practices
Here is the best configuration options.
It is just a suggestion, these values should be derived based on the requirements.
Entities & Collections
Synchronous Invalidation
Less load on the network & cluster
[‘replicatePutsViaCopy’-Synchronous Invalidation is not supported by the Ehcache-JGroups impl]
Query cache
Local mode
Cache invalidation will happen based on the timestamps value. So we can disable the replication.
TimeStamps cache
Synchronous Replication