2

Have a Map which contains objects that I want to keep in sync across multiple servers, such that if objects in the map are created, deleted, or modified - this is reflected immediately (ie. within a second or two) across all servers, in a way that can potentially scale up to tens of servers.

Is there a lightweight open source Java tool that can do something like this? I'm aware of Terracotta but it is rather heavy weight for what I need.

edit: The map is backed by a DB (specifically, Apache Cassandra) - but I need my clients to be informed of any changes to it within seconds, which would mean that I would need to poll Cassandra very frequently unless there is some other means to notify clients that the map has changed.

sanity
  • 35,347
  • 40
  • 135
  • 226

5 Answers5

3

You can use Hazelcast. If you are trying to share the same Map, Hazelcast will automatically do this for you. Here is the code for this:

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

All JVM's using "customers" map will see exactly the same data, even if it is updated frequently. If you also want to be notified whenever Map is changed then you can add listener. Here is 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");
        })
Fuad Malikov
  • 2,325
  • 18
  • 20
2

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • As I mentioned in the question, I'm aware of Terracotta but I'm hoping for something more lightweight. – sanity Jun 17 '10 at 20:17
1

I doubt you would find anything more lightweight than Terracotta. Why not use DB with second level caching?

Georgy Bolyuba
  • 8,355
  • 7
  • 29
  • 38
1

You would have to be more specific, For example, is the map backed by a DB, or is it just within the app memory (like in the session or something)?

I've seen cache implementations that do something similar via JMS message queues etc (JCS from apache for example).

If you're in the mood, you could roll your own solutions that involves JMS topics, listeners and publishers, but this is not very trivial in practice. And I'm not sure if it will yeild the performance you're looking for.

Commercial grade cluster/cache solutions are probably you're best solution, as the other posters have suggested.

Java Drinker
  • 3,127
  • 1
  • 21
  • 19
  • Added a paragraph to answer your question – sanity Jun 17 '10 at 20:27
  • I've seen (although not all too familiar with it) distributed caches used for this purpose. The cached object is backed by the db, and there is some normal refresh rate (30 minutes etc). When one server modifies the cached data, the local cache is flushed and reloaded. At the same time, a message is sent to the other servers to refresh their cached objects from the db again. There is no constant polling. Saw this used for user role data. It is ideal for things that are read often but are updated rarely. Otherwise the overhead is not worth the benefit. – Java Drinker Jun 17 '10 at 20:35
1

Maybe Hazelcast could help. It has some distributed collection implementations.

Mark
  • 28,783
  • 8
  • 63
  • 92