javaspring-boothazelcasthazelcast-imap

Hazelcast Cluster: Inconsistent Entry Additions Across Two Instances


I'm running two instances of an application, and each instance is supposed to add its host information to a Hazelcast map named "hosts" during startup.

HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IMap<String, Void> set = hz.getMap("hosts"); set.set(host, Void, ttl, TimeUnit.SECONDS);

However, I've noticed an unexpected behavior. Instance 1 is correctly adding its host information to the map, but strangely, it's also adding the host information of instance 2. On the other hand, instance 2 does not add any host information to the map.

1- Both instances are using the same code to add entries to the "hosts" map. 2- I have verified that the host information retrieved by each instance is correct. 3- The cluster is correctly formed, as both instances are visible in the Hazelcast Management Center. 4- The Map is also created and has 2 members. member 1 adds 2 entries but member 2 adds 0.

I'm trying to understand why instance 1 is adding entries for both itself and instance 2, while instance 2 is not adding any entries. The logic to add entries seems correct, and there are no apparent issues with the cluster formation.


Solution

  • Insertion into a map is always going to be performed by the cluster member (and more specifically, the partition thread) than owns the key being inserted. In your case, the ‘host’ keys both hashed to values that ended up being owned by the same member. By having operations performed by the owning thread, rather than letting any member update any key, Hazelcast can ensure thread safe behavior without requiring that you lock the map before updating it. (The keys may or may not have hashed to the same partition, but it seems that they must have hashed to partitions residing on the same member).