Search code examples
javaspringcachingconfigurationignite

How to replace an ignite cache?


Im using ignite version 2.2.0 and I have the following cache configuration:

private CacheConfiguration<String, Product> getProductCacheConfiguration() {
    final CacheConfiguration<String, Product> cacheConfiguration = new CacheConfiguration<>(Identifiers.PRODUCT_IGNITE_CACHE);

    cacheConfiguration.setName(Identifiers.PRODUCT_IGNITE_CACHE);
    cacheConfiguration.setIndexedTypes(String.class, Product.class);
    cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    cacheConfiguration.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(new ProductCacheStoreAdapter(this.databaseConfiguration)));
    cacheConfiguration.setReadThrough(true);
    cacheConfiguration.setWriteThrough(true);
    cacheConfiguration.setCopyOnRead(false);
    cacheConfiguration.setCacheMode(CacheMode.PARTITIONED);
    cacheConfiguration.setOnheapCacheEnabled(true);

    return cacheConfiguration;
}

Initially, when the applicaiton (spring application) is starting, I load all data from a database into my ignite cache. After that I start a spring scheduler task which is responsible for loading new data and replacing the old one. By replacing I do not mean to replace single items which are existent in the current cache - I mean replacing the whole cache - removing all current entries and adding all the new.

The problem I have here is that during this replacement the application wont be useable since I would first have to remove all the data and after that add the new data. So there is a small timeframe when there is no data available. I do not want that.

Is there a way to replace caches without having a timeframe with no data?


Solution

  • Consider having two caches in Ignite and switching between them.

    When the scheduled task is triggered, you can create a new cache and start loading data into it. Reading processes can still use the old cache. Once the loading is finished, you can make reading processes switch to the new cache, and after that – clean or destruct the old one.

    You will have to implement the switch over yourself though. You can use a replicated cache to store a name of the cache, that is currently considered active, for example.