I have some Java processes(Socket programs) running on different servers, some on the same network and some on different networks. These processes together have the job to maintain a global counter. A client can connect to any of these processes and issue command to increase
, decrease
or get
the counter value. The global counter should be eventually consistent(Network partition can occur and we can recover from it).
The solution I have thought of so far is to maintain a count of increments and decrements on each node for all the nodes. When an increment command is issued on a node, it increments its own local copy of its counts of increments and then broadcasts its increment and decrement count. The nodes that receive this broadcast take the max of the received counts and their local copy of the sender's counts and stores the result as the latest count. When a get
command is issued on any node it gives the difference of the sums of all the increments and decrements. I assume this will take care of cases where broadcasts are received out of order and other unreliabilities. I don't want to use any persistence layer.
Is there a better way to implement this? What protocol should I use to broadcast the counts? Will gossip on UDP work? Any Java libraries that might help?
You may be aware of this design pattern, but it still may be inspiring: https://en.wikipedia.org/wiki/Observer_pattern
You could simply make all of the instances of the program observe all of the other instances, then they will all notify each other if any one changes (check out the diagram in that link).
As far as a Java libraries, check these out, see if any of them make your life easier:
http://mina.apache.org/
http://commons.apache.org/proper/commons-net/
http://hc.apache.org/