How to keep data synchronization on multiple servers in Java?

You have a map that contains objects that I want to synchronize on several servers, for example, if objects on the map are created, deleted or modified - this is immediately reflected (i.e., after a second or two) in all servers, which can potentially scale up to dozens of servers.

Is there an easy open source Java tool that can do something like this? I know Terracotta, but it's pretty heavy for what I need.

edit: the map is supported by the database (in particular, Apache Cassandra), but I need my clients to be informed of any changes in it in seconds, which would mean that I would need to interrogate Cassandra very often, if there weren’t some other means to notify customers that the card has changed.

+2
source share
5 answers

You can use Hazelcast . If you try to use the same card, Hazelcast will automatically do it for you. Here is the code for this:

java.util.Map<String, Customer> distributedMap = Hazelcast.getMap("customers"); 

All JVMs using the “customers” card will see exactly the same data, even if they are updated frequently. If you also want to be notified when the Card is changed, you can add a listener. Here's how:

 com.hazelcast.core.IMap<String, Customer> distributedMap = Hazelcast.getMap("customers"); distributedMap.addEntryListener(new EntryListener() { public void entryAdded(EntryEvent entryEvent) { System.out.println("Entry added"); } public void entryRemoved(EntryEvent entryEvent) { System.out.println("Entry removed"); } public void entryUpdated(EntryEvent entryEvent) { System.out.println("Entry updated"); } public void entryEvicted(EntryEvent entryEvent) { System.out.println("Entry evicted"); }) 
+3
source

I think Terracotta VM clustering is the way to go. (I don't know how “multiple servers” combine with “light”)

+2
source

I doubt you will find anything lighter than Terracotta. Why not use a DB with second-level caching?

+1
source

Do you have to be more specific, for example, is it a card supported by the database, or is it only in the application memory (for example, in a session or something else)?

I have seen cache implementations that do something similar through JMS message queues, etc. (e.g. JCS from apache).

If you are tuned in, you can use your own solutions involving JMS topics, listeners and publishers, but in practice this is not very trivial. And I'm not sure if this will work on what you are looking for.

Commercial classes cluster / cache solutions, you are probably the best option, as suggested by other posters.

+1
source

Perhaps Hazelcast can help. It has some common implementations of collections.

+1
source

Source: https://habr.com/ru/post/1486935/


All Articles