Tuesday, September 08, 2009

Tuning ehcache and hibernate

Hibernate mapping files are mapped to cache "regions" and each region has some configuration options that is configured in the ehcache.xml. A simple strategy for tuning is to create three different classes of objects:

  1. short lived entities
  2. long lived, but not eternal entities
  3. eternal entities
Which class does your entity fit into? Eternal entities will never change while your system is running. Long lived entities are accessed repeatedly, for example by a logged in user, for an extended period of time. Most other objects are short lived, and usage is confined to a relatively short period of time, perhaps across only a handful of transactions.

The number of elements stored in a cache region will vary by your application and the entity.

Short Lived Entity
Notice that the time to live is on the order of seconds to a few minutes
<cache
name="myregion"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="120"
overflowToDisk="false"
diskSpoolBufferSizeMB="0"
maxElementsOnDisk="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="0"
memoryStoreEvictionPolicy="LRU" />


Long lived Entity
Notice that the time to live is on the order of minutes to hours
<cache
name="myRegion"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
overflowToDisk="false"
diskSpoolBufferSizeMB="0"
maxElementsOnDisk="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="0"
memoryStoreEvictionPolicy="LRU"
/>



Eternal Entity
Time to live is set to 0 so that the cache entries persist forever.
<cache
name="myRegion"
maxElementsInMemory="10000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskSpoolBufferSizeMB="0"
maxElementsOnDisk="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="0"
memoryStoreEvictionPolicy="LRU"
/>


Verifying your Settings
Well with the above information you can make some pretty good guesses, but you can actually check how well things are working using JConsole.

JConsole can connect to Tomcat instances and cache statistics can be monitored. You must configure Tomcat to open a port for JConsole to connect. Add the following to Catalina OPTS in your startup script:

-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1612 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.snmp.port=16666 -Dcom.sun.management.snmp.acl=true

You will also have to configure a management bean for your application. For Spring applications, you can use the following XML:

<!-- Stuff for hibernate and ehcache mbeans -->

<bean id="ehCacheMBeanRegistration"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

<property name="staticMethod"
value="net.sf.ehcache.management.ManagementService.registerMBeans"/>
<property name="arguments">
<list>
<ref bean="cacheManager"/>
<ref bean="mbeanServer"/>
<value>true</value>
<value>true</value>
<value>true</value>
<value>true</value>
</list>
</property>
</bean>

<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="true"/>
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>

<!-- end of stuff for hibernate and ehcache mbeans -->


Now start Tomcat.

Navigate to your JDK's bin directory. Type jconsole tomcat_hostname:port.

A dialog will appear asking you to login. Enter username/password credentials (configured in Tomcat_home/conf/tomcat-users.xml -- connect as a user with the manager role).

Navigate to the MBeans tab.

Expand net.sf.ehcache

Expand cachestatistics

There will be one more sub folder within which your regions will be listed. Each region will have a set of Attributes. You can monitor cache hits and misses and tune your settings to minimize the number of misses.

3 comments:

SwissT said...

Very helpful, it worked the first time. Thanks!

Peter V said...

Thanks, glad you got some use from this information. It was painful to figure out on my own!

Unknown said...

C for Cat
C for Cat
C for Cat
C for Cat
C for Cat
C for Cat

Labels

Blog Archive

Contributors