Spring cache abstraction - distributed environment
Asked Answered
Q

2

12

I would like to use the spring cache abstraction in my distributed web application.

My web application runs on 3 different tomcats with a load balancer.

Now, My problem is how exactly can I @Evict cache in all tomcats when another tomcat preforms an update?

Does spring supports this kind of thing?

Thanks!

Quarters answered 4/2, 2014 at 14:4 Comment(0)
M
15

If it's EHCache that you've told Spring to use, then EHCache supports replication across multiple cache instances across different physical servers. I've had some success with the RMI Replicated Caching using multicast discovery. Evicting from one cache will automatically replicate across the other caches - and likewise when adding to a cache.

In terms of the Spring config, you'll need to set up the various config elements and beans:

<cache:annotation-driven />

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="cacheManager" />
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation"  value="classpath:/ehcache.xml"/>
</bean> 

The rest of the configuration is done in the ehcache.xml file. An example replicated cache from ehcache.xml may look something like this:

<cache name="example" 
        maxElementsInMemory="1000"
        eternal="false" 
        overflowToDisk="false" 
        timeToIdleSeconds="0"
        timeToLiveSeconds="600">
        <cacheEventListenerFactory 
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
        <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
            properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>
</cache>

And then you'll need to add the replication settings to the ehcache.xml which may look like this:

<cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.2,
                    multicastGroupPort=4455, timeToLive=1" />

<cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=localhost, port=40001, socketTimeoutMillis=2000" />

There are other ways to configure replication in EHCache as described in the documentation but the RMI method described above is relatively simple and has worked well for me.

Mcgean answered 4/2, 2014 at 16:3 Comment(5)
Is this configuration invalidates or sending the whole updated object via multicast?Quarters
Multicast is only used for peer discovery. Replication is achieved using point to point RMI. If you are unable to use multicast for peer discovery, then there are other mechanisms available (such as specifying the IP addresses of the servers manually in the configuration).Mcgean
Thanks, Final question, Is the RMI invalidate message from one cache to another is blocking or happens asynchronously?Quarters
It's asynchronous by default - but if you want it to be synchronous then you can set replicateAsynchronously=false in the listener factory properties. Worth checking out the docs for more detail though.Mcgean
Update : Managed to configure the cache today, next week will do the testingQuarters
A
1

You can try using Clustered caching, like Hazelcast. Ehcache also support clustered caching through Terracota server.

Let's say, you have 3 application nodes, in a load balanced environment. If you use Hazelcast, each application node will act as an cache node of hazelcast, and together they will give you an abstraction of a single Cache server. So whenever you update an entity in a node, the other nodes will get notify instantly, and update it's cache, if necessary. This way, you also won't have to evict your cached object.

Configuring this is also very easy, looks something likes this

        <tcp-ip enabled="true">
            <member-list>
                <member>machine1</member>
                <member>machine2</member>
                <member>machine3:5799</member>
            </member-list>
        </tcp-ip>

For further information, try reading this article here.

Atonality answered 17/10, 2019 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.