ehcache with spring @CacheEvict not invalidating in all AWS mutli instances

507 views
Skip to first unread message

devula...@gmail.com

unread,
Apr 10, 2018, 3:40:42 PM4/10/18
to ehcache-users
Could someone please help to resolve the issue  which is high priority for us.

Issue :  The application is hosted in AWS with 3 instances for HA;  when user logged in into the portal  and browse the application , the below method verifyToken() gets triggered and  the token gets stored in cache by using spring annotation @Cacheable and then gets evicted by using @CacheEvict when user LOGGED OUT from the portal ; we are able to still perform API operations by using that token with method verifyToken() since  when CacheEvict is performed  the token getting deleted from only one of the AWS instance  and other 2 AWS nodes still having the token details and being returned until it gets expired.  Question is  how to get clear the token details from cache from all the nodes. These 3 AWS instances are running behind ELB

Please check below details for more details and let me know for any further clarifications.

Code :


@Cacheable(value = "sessions", key = "#token")
@Transactional
@SuppressWarnings("unchecked")
public Object verifyToken(final String token) {

return o;
}



@CacheEvict(value = "sessions", key = "#token")
@SuppressWarnings("unchecked")
public void clearToken(final String token) {
final List<Login> result = entitymanger.createNamedQuery("persis.find.token").setParameter("token", token).getResultList();
if (result != null && !result.isEmpty()) {
for (final Login pl : result) {
entitymanger.remove(pl);
}
}
}

@CacheEvict(value = "sessions", key = "#token")
public void evictSessionCache(final String token) {
// no implementation on purpose
}

  1. What version of Ehcache you are currently using;   
  • ehcache-spring-annotations-1.2.0.jar ; hibernate-ehcache-5.0.7.Final.jar;ehcache-core-2.6.11.jar
    2. Paste the configuration for the Cache/CacheManager you have an issue with; 
  •       ehcache.xml  as follows 
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
  maxElementsOnDisk="10000000" diskPersistent="false"
  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
 
<cache name="sessions" eternal="false" maxElementsInMemory="1000"
   overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
   timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU"/>
 
</ehcache>

3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate); 
  •  spring version 4.3.12 
  • hibernate version  5.0.7
4. Providing JDK and OS versions maybe useful as well.
  Java 8

Jonathan Tew

unread,
Apr 12, 2018, 6:27:23 AM4/12/18
to ehcache-users
Not sure where the 4:37 AM reply to this thread went that I saw on my phone.  Not seeing it on the groups interface, but I figured I would reply.

The approach that we've taken in preparation for an AWS deployment is to build out own caching framework with layers.  The layers consist of EHCache 3.x as layer one and optionally Redis as layer two.  We are using Redis pub/sub commands to broadcast eviction events to other nodes since 3.x doesn't have RMI replication any more (plus broadcast protocols dont work in AWS).  It's worked beautifully so far.  No need to run any other type cluster software other than Redis which we already run.  No cross node communication about eviction like the old RMI replication days.

devula...@gmail.com

unread,
Apr 12, 2018, 10:11:34 AM4/12/18
to ehcache-users
Thank you Jonathan for the response. Not sure why its not visible on groups interface.

reg you response, are you suggesting me to use EHCache 3.x to resolve the issue ? I'm little confused about your suggestion optionally use Redis .  Of course I can go even with AWS memcache or Redis  aws managed service instead of EHCahce.   

 But as I'm using EhCahce , could you please clarify further all I need to do is upgrade the EHCache from 2.x to 3.x  to resolve the issue ? will there be any specific configuration to be followed considering my scenario for that version? do you have any idea  on high level design implementation in 3.x version ?

Thanks
Reply all
Reply to author
Forward
0 new messages