Grails local ehcache configured in combination with remote Terracotta

148 Views Asked by At

We have ehcache configured in our Grails environment and I'm trying to ascertain how a local cache can be configured with a remote terracotta cache.

The scenario is that we have some data that is minimally expensive to calculate and benefits from a local in-memory cache but the benefit is minimized when using the remote terracotta cache.

The config is currently pretty simple:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" >
  <diskStore path="java.io.tmpdir"/>
  <cacheManagerEventListenerFactory class="" properties=""/>
  <defaultCache
       maxElementsInMemory="20000"
       eternal="false"
       timeToLiveSeconds="12000"
       overflowToDisk="false"
       diskPersistent="false">
    <terracotta />
  </defaultCache>
  <terracottaConfig url="${com.ngs.app.tc.host}:${com.ngs.app.tc.port}" />

  <cache name="org.hibernate.cache.UpdateTimestampsCache"
      maxElementsInMemory="10000"
      timeToIdleSeconds="300"/>

  <cache name="org.hibernate.cache.StandardQueryCache"
      maxElementsInMemory="10000"
      timeToIdleSeconds="300"/>
</ehcache>

The questions are:

  1. Given the config above, does this imply that a cache put / get will always do a round trip to the terracotta server?

  2. Is there a possible configuration where it will use a local "hot" cache before doing the round trip to the server?

  3. If a local "hot" cache is to be used, this would need to be implemented programmatically using a different cache configuration, not backed by terracotta?

Thanks for any advice ...

1

There are 1 best solutions below

1
On BEST ANSWER

A few points:

  • The configuration above only declares the default cache to be clustered, the named caches are not clustered.
  • Ehcache works with a tiering system, so that means that even for a clustered cache, the setting of maxElementsInMemory will apply to a on-heap tier of the cache, local to each JVM part of the Terracotta cluster. Note: maxElementsInMemory is deprecated and should be replaced by maxEntriesLocalHeap in recent Ehcache versions.

Given this, here are the basic operations and their relation to the clustered bit of the cache:

  • Get with a local hit: only touches the on-heap tier, no Terracotta round trip
  • Get with a local miss: will go try to find the value in the cluster - Terracotta round trip
  • Put: will need to update the cluster - Terracotta round trip
  • Remove: will need to update the cluster - Terracotta round trip

For these last two operations, the default configuration decouples the local operation from the clustered one. If you cannot handle reading stale value, you can play with the consistency setting.

This means that answers to your questions are:

  1. See above
  2. Yes
  3. Yes and no: if you need only the local part, you can have a local only cache, just omit the terracotta element in the cache configuration. If you need the clustered bit but want most cache hits to be local hits, you need to tweak your clustered cache configuration.