Java Hazelcast Near Cache getting evicted very frequently

47 Views Asked by At

I have created the Hazelcast Config along with NearCache config as below:

 EvictionConfig evictionConfig = new EvictionConfig()
            .setEvictionPolicy(EvictionPolicy.NONE)
            .setSize(1_000_000)
            .setMaxSizePolicy(MaxSizePolicy.ENTRY_COUNT);

    NearCacheConfig nearCacheConfig = new NearCacheConfig().setInMemoryFormat(InMemoryFormat.OBJECT)
            .setInvalidateOnChange(true)
            .setSerializeKeys(true)
            .setEvictionConfig(evictionConfig)
            .setCacheLocalEntries(true);

Further i have created the iMap using these config as

        config.getMapConfig(Constants.CacheNames.EMPLOYEE_CACHE).setReadBackupData(true).setNearCacheConfig(new NearCacheConfig(nearCacheConfig));

The issue what i am seeing is that even for small data 50 in number seeing lot of invalidation events for this iMap.

Not able to figure out what i am missing in this config.

1

There are 1 best solutions below

0
Orçun Çolak On

I think you are looking at the events by attaching a listener to IMap.

  • If you call IMap#addLocalEntryListener() this will attach a listener for events on the cluster member
  • If you call IMap#addEntryListener() this will attach a listener for events on the whole cluster

If you are doing like this and seeing evictions, those events are for the IMap not for NearCache. There is no way to attach a listener to NearCache as far as I know.

Since you have EvictionPolicy.NONE and you have called setSize() the NearCache will cache the first 1_000_000 unique keys. Remember that for NearCache to work you should call IMap#get()

You can check NearCacheStats like this

LocalMapStats localMapStats = map.getLocalMapStats();
NearCacheStats nearCacheStats = localMapStats.getNearCacheStats();

When you check you should see nearCacheStats.ownedEntryCount <= 1_000_000
Dependes on how many get() calls you have made

If you have removed keys from IMap that exist in NearCache you may also see a non-zero value for nearCacheStats.invalidations

But you should see nearCacheStats.evictions as zero since you have disabled eviction for NearCache