Faheem Sultan
Faheem Sultan

Reputation: 283

Infinispan distributed cache mode with K8

i am using infinispan cache to communicate multiple services deployed in K8. i want these caches to be

  1. replicated, to only a few services (not all) just so that when a service dies, it should be able to recover its caches from another service when it restarts)
  2. accessible for read/write/evict in specific services.

below is the cache configuration i am using, this configuration is shared in all services using common library.

@PostConstruct
    public void init(){
        if(defaultCacheManager==null){
            defaultCacheManager = cacheManager();
        }
        for(String cacheName:cacheNames){
            createCache(defaultCacheManager,cacheName);
        }
    }

public Cache<String, String> createCache(DefaultCacheManager cacheManager, String cacheName) {
        return this.buildCache(cacheName, cacheManager, cacheExpiringEvictingConfig(cacheManager));
    }

    private Configuration cacheExpiringEvictingConfig(DefaultCacheManager cacheManager) {
        ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        confBuilder.expiration().lifespan(200, TimeUnit.HOURS);
        confBuilder.memory().maxCount(1000000).whenFull(EvictionStrategy.REMOVE);
        confBuilder.clustering().cacheMode(CacheMode.DIST_ASYNC);
        return confBuilder.build();
    }

 private <K, V> Cache<K, V> buildCache(String cacheName, DefaultCacheManager cacheManager, Configuration configuration) {
        cacheManager.defineConfiguration(cacheName, configuration);
        Cache<K, V> cache = cacheManager.getCache(cacheName);
        return cache;
    }

i'd appreciate if someone can go through this and give me a better configuration which can provide optimal performance (faster read/writes) as my services are heavily relying on caches. also i can see distributed working fine in my local machine with single jvm, but when deployed in k8. caches are not synced.

Upvotes: -1

Views: 415

Answers (1)

Ryan Emerson
Ryan Emerson

Reputation: 406

In order for multiple Infinispan instances to connect to each other you need to configure transport in the GlobalConfiguration of the DefaultCacheManager.

To configure a transport that works in Kubernetes, you can take advantage of the jgroups-kubernetes.xml stack that's included with Infinispan. You can configure this as follows:

  GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
    .transport()
        .defaultTransport()
        .addProperty("configurationFile", "default-configs/default-jgroups-kubernetes.xml")
        .build();
  DefaultCacheManager cacheManager = new DefaultCacheManager(globalConfig)

This configuration will utilise the JGroups DNS_PING protocol to establish cluster membership based upon a k8s service. You need to create a headless k8s service that references all of your application pods containing Infinispan and then configure the DNS_PING protocol by setting the java property -Djgroups.dns.query=$SERVICE_NAME.$NAMESPACE.svc.cluster.local.

For a working example, please see our Kubernetes Simple Tutorial.

Upvotes: 0

Related Questions