Migrating from Hibernate-EHCache in-memory resource hell towards optimistic AWS ElastiCache or Memcached

Taking my discussion further away from Hibernate-EHcahe and their terracotta servers.

The in-memory implementation are for the faint hearted. The kind of load we unexpected, brought us to think of a better way. The implementation of hibernate-memcached looks fine but I am not very convinced.

So, what do I do ?  I take it out for a trial run.

Lets migrate the same application to offload the 2nd level cache to the ElastiCache/Memcached provisioned by AWS.

Step 1: Setup a Memcached local instance or Create an Amazon ElastiCache Node in AWS Public Cloud

Note: In AWS be sure to add the ElastiCache server in the same security group as the EC2 instance used for deployment.

Step 2: Update Maven POM

 <!-- Memcache Hibernate integration -->

Step 3: Update the Existing hibernate properties in the session-factory configurations

 <!-- AWS ElasiCache/Memcached Config -->
 <prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop>
 <prop key="hibernate.cache.use_query_cache">true</prop>
 <prop key="hibernate.cache.use_second_level_cache">true</prop>
 <prop key="hibernate.memcached.servers">mycache.whatever.cache.amazonaws.com:11211</prop>
 <prop key="hibernate.memcached.cacheTimeSeconds">300</prop>
 <prop key="hibernate.memcached.connectionFactory">DefaultConnectionFactory</prop>
 <prop key="hibernate.memcached.clearSupported">false</prop>

Issues: I still have those Cache annotations with the concurrency configuration which is not good with Memcached as mentioned here. I did get hopeful after an informative read which discussed on deploying hibernate-memcached at an enterprise scale.

Also, the cache timeout are small as i get the object expired exceptions frequently. Need to look into this.

WARN : org.hibernate.cache.ReadWriteCache - An item was expired by the cache while it was locked (increase your cache timeout)

Inference: Still not convinced, as i require more freedom to configure just like Ehcache. The benefits of limiting the memory resources in a pool configuration can not be neglected as I am unsure at this point on the overflow consequences in ElastiCache servers. Ehcache clearly configures as per the specified limit and even accepts percentages for that matter in each domain cache configuration.

However, scalability can be an issue but in terms of 2nd level caching in hibernate we should really be specific  as the data is structured here. Only after analyzing the memory footprint and load we should configure the cache parameters. We can scale mindlessly on objects using memcached on other layers say; static web pages, intermediate results, database internal cache distribution etc. Only if in-memory is really a blocker considering your load, should you move to memcached as ElastiCache service. In this scenario you need to dig into the internals of hibernate-memcached to verify your production usage needs. I am in the process of doing so right now !!!


One thought on “Migrating from Hibernate-EHCache in-memory resource hell towards optimistic AWS ElastiCache or Memcached

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