stale data in the cache

120 views
Skip to first unread message

Cyndy Koobs

unread,
Jun 9, 2021, 10:33:04 PM6/9/21
to ehcache-users
Our project recently migrated from ehcache 2.x to ehcache 3.9. Hibernate was also migrated from 5.0.7 to 5.3.13. We don't initialize hibernate and caching programatically but rely on configuration files (ehcache.xml and hibernate.cfg.xml).

Here are the properties related to caching in hibernate.cfg.xml:

<property name="hibernate.cache.use_second_level_cache">false</property>
<propertyname="hibernate.cache.region.factory_class">jcache</property>
<propertyname="hibernate.javax.cache.provider">org.ehcache.jsr107.EhcacheCachingProvider</property>
<property name="hibernate.javax.cache.uri">file:config/etc/ehcache.xml</property>
<property name="hibernate.cache.auto_evict_collection_cache">true</property>
<property name="hibernate.cache.missing_cache_strategy">CREATE_WARN</property>

and this is the content of ehcache.xml:

<ehcache:config
    xmlns:ehcache="http://www.ehcache.org/v3"
    xmlns:jcache="http://www.ehcache.org/v3/jsr107">

  <ehcache:cache alias="configCache">
    <ehcache:expiry>
      <ehcache:tti unit="seconds">5</ehcache:tti>
    </ehcache:expiry>

    <ehcache:heap unit="entries">1000</ehcache:heap>
  </ehcache:cache>
</ehcache:config>

The data in the cache is stale. When a change is made, the database is getting updated but when the data is queried (should be from the cache), it does not reflect what is in the database. 

I am hoping that someone might see what I have missed in the configuration to cause the cache to have stale data.

Thanks to any and all help,
Cyndy Koobs

Chris Dennis

unread,
Aug 2, 2021, 12:27:10 PM8/2/21
to ehcach...@googlegroups.com

Nothing jumps out at me here. That said you probably don’t want to be using TTI for your cache. Either configure a TTL, or configure no expiry at all. Under the right workload that could lead to data in the cache beign more stale than expected. Short of that I would suspect that this is a Hibernate configuration issue, and that maybe the cache isn’t getting invalidated correctly.

 

Chris

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/af154a9d-ff3e-4b98-bca2-ca594eba3994n%40googlegroups.com.

Cyndy Koobs

unread,
Aug 19, 2021, 6:11:46 PM8/19/21
to ehcache-users
Hi Chris,

I checked for a bit after I posted this and didn't see any responses. So, I promptly forgot about it..... Thank you for your thoughts. I am still battling this problem having tried a million different ideas including the one you suggested. I will look into Hibernate and see if anything there jumps out at me. This did work when we used ehcache 2.x; the problem came up when we upgraded to 3.x. I can see that with the 2.x version all of the caches were being created "spontaneously' but I guess the default configuration that was used worked the way we wanted it to. Lucky. But with 3.x the behavior is different. I can see that the caches I have configured are being created, ie '

[EhcacheManager [] main]   Cache 'com.teramedica.config.entity.TMStoragePoolConfig' created in EhcacheManager.'

The hibernate properties are still the same but here is the configuration I am  now using:

<config
    xsi:schemaLocation="

 <service>
   <jsr107:defaults enable-management="false" enable-statistics="true"/>
 </service>
 
  <cache-template name="defaultCacheTemplate">
  <expiry>
      <ttl unit="seconds">10</ttl>
    </expiry>      
    <resources>
      <heap unit="entries">1000</heap> 
    </resources>
 </cache-template>
 
 <cache alias="com.teramedica.config.entity.TMDicomServerIdentifierConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMDicomServerPresSyntaxConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMJobManagerConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMOrganizationConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMStoragePolicyConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMStoragePoolConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMStoragePolicyViewConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMUIConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMDiagnosticConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMNotificationBroadcasterConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMCodeConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMContentSourceConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMHL7MetricsConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMNotificationPolicyConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMRuleSetConfig'" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMAffinityDomainConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMDicomListenerConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMRuleConditionConfig" uses-template="defaultCacheTemplate"/>
 <cache alias="com.teramedica.config.entity.TMEmailPropertiesConfig" uses-template="defaultCacheTemplate"/>
  
</config>

nothing complicated. 

thanks again for your time and knowledge. Much appreciated.
Cyndy 

dakks...@gmail.com

unread,
Aug 20, 2021, 1:46:52 AM8/20/21
to ehcache-users
Hi Cyndy,

One design idea can be to keep expiry in the value. And as a double check compare the expiry in the application code also. This may sound like a hack but gives other possibilities for eg, you can decouple the ehcache expiry with the content expiry.
Lets say you keep ehcache ttl to 24 hrs but runtime time check to 2 hrs. So In case DB is down you still can access the cache, you just need to modify the runtime cache expiry check.

class CacheValue{

long expiry;
Object data;

Chris Dennis

unread,
Aug 23, 2021, 9:48:07 AM8/23/21
to ehcach...@googlegroups.com

Hi Cindy,

 

If you can distill it down to a reproducible test case that you can share then I can have a look and see if there is anything I can see, short of that all I can do is wish you good luck. One thing that might help here would be to write a quick wrapper store that logs all the cache accesses somewhere so that you can see what operation Hibernate is performing against the cache (and when).

Reply all
Reply to author
Forward
0 new messages