Reputation: 11
I have one microservice let's say StudentProfile, and deployed this in 2 nodes xxxx:8081 and yyyy:8081. In this microservice I have created hazelcast map where I am storing data of student with map.put(roll_number, student_object). Now when I am updating some student from xxxx:8081, its get updated here but when I try to fetch the data of same student from yyyy:8081, I get old data.
My question is that how do I sync(share) that Map in both the nodes so that If I make changes from one node then they should be reflected from another node as well. Is there some configuration changes which I need to make?
Please ask me further details if above explanation doesn't makes sense. I am new to it and trying to learn how it works.
Upvotes: 1
Views: 747
Reputation: 3150
The default behaviour of Hazelcast for IMap should give you what you want.
If the two nodes are connected, you will see a log line in each similar to
Members {size:2, ver:2} [
Member [1.2.3.4]:5701 - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
Member [5.6.7.8]:5701 - bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb this
]
If you find the map with HazelcastInstance.getMap("63149165")
, both nodes are using the same map with the given name.
If any node does map.put("hello", new Date())
, then any node can do map.get("hello")
to retrieve the value that was inserted.
Default behavior is for synchronous replication. You won't get the previous value unless you have modified the configuration, and if you have modified the configuration from default this won't be a surprise.
What is important to note is you get a defensive copy of the data in Hazelcast.
If you do map.get("hello")
you get a copy of the data value associated with that key. If you modify that copy but do not then re-insert it in the grid, the grid copy doesn't change. So if you do map.get("hello")
again you will continue to get the original value.
Upvotes: 1