Persistent caching in a Java web app?

911 views Asked by At

I'm using Ehcache to cache the results of an expensive service call within a web application.

I want the cache to persist across JVM restarts.

I configured a 2-tier ehcache with heap and disk. Unfortunately Ehcache only saves the cache to disk when PersistentCacheManager.close() is called.

How can I get the persistence to work properly? Am I supposed to periodically close and reopen the cache? Should I be looking at something other than ehcache for this?

2

There are 2 answers

0
Anthony Dahanne On

I would recommend you to use at Terracotta cluster deployment (it's open source too), next to your web application JVMs.

You would then configure, on your web applications, 3 tiers : heap, offheap and clustered

<cache alias="clustered-cache">
  <resources>
    <heap unit="MB">10</heap> 
    <offheap unit="MB">512</offheap> 
    <clustered-dedicated unit="GB">2</clustered-dedicated> 
  </resources>
</cache>

The Terracotta server deployment would be 1 Active, 1 Passive - so that when you have a failure or a predicated maintenance on 1 server, the other still serves all the clients.

The benefits of using the open source clustered tier, versus the open source disk tier, is that in case of failure (web application container failure, terracotta server failure), your data is still intact, thanks to the redundancy of your setup.

0
Louis Jacomet On

Ehcache uses a tiered model - data written to the cache is always placed on the lowest tier - disk in your case. Then upon reads happening, frequently accessed values are pulled into the heap tier for faster retrieval. So the data will be written to disk during operations, providing a larger cache space than just heap.

In order to guarantee cache integrity and the ability to recover data after a JVM restart, PersistentCacheManager.close() needs to be called. But that does not mean it has to happen during your application's life, just that you need a proper web application shutdown sequence rather than simply killing the JVM.