Hazelcast Near Cache Not Invalidating After 60 Seconds as Configured

119 views Asked by At

I have set up Hazelcast 3.12 near cache for my application (java spring), and according to my configuration, it should invalidate the cache entries after 60 seconds. But for some reason, it doesn't seem to be doing so.

According to the documentation, both time-to-live-seconds and max-idle-seconds are set to 60 seconds, which means the entries should be invalidated after that time. However, I am finding that the collection is not empty (size = 4) even after waiting for 60 seconds, i even made it 3 seconds and still the data is in the cache map. enter image description here

I've double-checked the configuration, and everything seems to be correct. I'm wondering if I'm missing something or if there's a known issue related to this.

Any insight or help would be greatly appreciated!

Here's my configuration:

<near-cache name="near-cache-map">
  <in-memory-format>BINARY</in-memory-format>
  <invalidate-on-change>true</invalidate-on-change>
  <eviction eviction-policy="LRU" max-size-policy="ENTRY_COUNT" size="10000"/>
  <time-to-live-seconds>60</time-to-live-seconds>
  <max-idle-seconds>60</max-idle-seconds>
  <local-update-policy>INVALIDATE</local-update-policy>
</near-cache>
1

There are 1 answers

0
Orçun Çolak On

The purpose of NearCache is to avoid network calls. It is useful when the IMap is mostly read-only. See the explanation in the documentation

Near Cache works only when you access data via map.get(k) or cache.get(k) methods.

So NearCache only caches the result of get() call. All other calls are still delegated to original IMap. Let's see an example. On the server side create an IMap and update it every second.

public static void main(String[] args) {
  Config config = new Config();


  String mapName = "mostlyReadMap";
  MapConfig mapConfig = config.getMapConfig(mapName);

  HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
  IMap<String, Integer> map = hazelcastInstance.getMap(mapName);
  String key = "1";
  map.put(key, 1);

  Timer timer = new Timer();

  // Create a timer task
  TimerTask task = new TimerTask() {
    @Override
    public void run() {
      int i = map.get(key);
      map.put(key, ++i);
      System.out.println("Timer expired! " + i);
    }
  };
  // Schedule the timer task to run every 1 second
  timer.scheduleAtFixedRate(task, 0,1000);
}

We are going to see the value changing every 1 second

Timer expired! 225
Timer expired! 226
Timer expired! 227
...

Now on the client side have this XML

<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
                  http://www.hazelcast.com/schema/client-config/hazelcast-client-config-5.3.xsd">
    <instance-name>barClient</instance-name>

  <near-cache name="mostlyReadMap">
    <time-to-live-seconds>10</time-to-live-seconds>
    <max-idle-seconds>10</max-idle-seconds>
  </near-cache>

</hazelcast-client>

Start the client with a NearCache and a timer

public static void main(String[] args) {

  HazelcastInstance hazelcastInstanceClient = azelcastClient.newHazelcastClient();
  IMap<String, Integer> nearCache = hazelcastInstance.getMap("mostlyReadMap");

  Timer timer = new Timer();

  String key = "1";
  TimerTask task = new TimerTask() {
    @Override
    public void run() {
      int i = nearCache.get(key);
      log.info("Timer expired! " + i);
    }
  };
  // Schedule the timer task to run after 1 second
  timer.scheduleAtFixedRate(task, 0,1000);
}

You are going to see that the client reads the same IMap value for 10 seconds even though it was updated on the server. Because it is using the locally cached value for NearCache. This is the purpose of NearCache. And then 10 seconds later after the local entry expires the client will fetch the new value from the remote IMap

I hope this clarifies the situation

Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 211
Timer expired! 221
Timer expired! 221