Take the 2-minute tour ×

I have two app servers (in this case Tomcat but it needn't be this container) which are running the same application with a load balancer directing work to them.

Behind these servers I have a single database which both servers wish to connect to via Hibernate. I want to cache common object requests using EhCache. In a single server setup this is a trivial configuration change to Hibernate to use the EhCache provider.

However, I don't want each server to have its own cache, I think I want a central cache which both servers use.

Originally, I thought this would be a simple matter of setting up the standalone EhCache server and pointing the hibernate configuration at them. However, I have been unable to identify how this can be performed (if at all).

My questions are: Can this setup exist and how does one set it up?

If this isn't possible, is there some other way (or other caching provider) in which I maintain a common hibernate cache between applications?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I am not aware of any EhCache server that can be accessed from several machines. You might write it yourself and expose e.g. REST API, but you would also have to implement your own CacheRegionFactory for Hibernate to use the remote server behind the scenes. A lot of work and the result will most likely be unsatisfactory due to network latency.

Another approach is to have a shared CacheManager that can be used by several application within one JVM. But since you have several JVMs, this is not an option. Also it requires that EhCache.JAR is laoded only once by a parent class-loader since it uses simple static field to preserve single instance.

Finally, the answer to your question is either using cache replication or Terracotta. While Terracotta is very sophisticated product, EhCache cache replication is very mature and stable.

The idea is simple: each server has an independent instance of cache manager, but the cache managers are discovering themselves automatically using UDP and network broadcast. When a change occurs in one of the caches, it is propagated to other peers. It is not very scalable, but for two server it should work fine.

share|improve this answer
add comment

Your Answer


