memcache is not working on multiple nodes

442 Views Asked by At

I am having one micro-service let's say 'X' in which i implemented Memcache (aws node) using spy-memcache. Now i have one dependency let's say 'Y' which is having spring cache (in memory) implementation.Now i have 2 end points sometimes both the end point requests lands on same node sometimes its on different. 1st request will put the data inside memcache and 2nd request will try to get the data stored by the first end point.This scenario is working fine until i put dependency 'Y' inside pom after that my implementation of memcache only works when both the request lands on same node otherwise its always a cache miss .

I tried to create separate memcahe cache manager

@Bean("XYZ")
public CacheManager cacheManager() {
    ExtendedSSMCacheManager ssmCacheManager = new ExtendedSSMCacheManager();

    List<SSMCache> cacheList = new ArrayList<SSMCache>();

    // First cache: A
    SSMCache partnerCache = createNewCache(memcachedAddresses, "A", expiration);

    // One more  cache :B
    SSMCache nonceCache = createNewCache(memcachedAddresses, "B", expiration);

    cacheList.add(A);
    cacheList.add(B);

    // Adding cache list to cache manager
    ssmCacheManager.setCaches(cacheList);

    return ssmCacheManager;
}
1

There are 1 best solutions below

0
Saša On

Maybe you could try using memcached-spring-boot library instead of having your own cache configuration. It supports multiple memchached server instances. E.g. configuration if using AWS:

memcached.cache:
    servers: mycluster.example.com:11211
    mode: dynamic
    expirations: 86400

You could also check a demo app here which could help you setup your project in the similar way.