Basic Distributed Counter using Java Sockets

881 views Asked by At

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?

2

There are 2 answers

1
D. Wood On

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:

0
David On

It sounds like you need a PNCounter from Akka's Distributed Data library. It uses Gossip to communicate the counter's state to the network. You also have fine grained control over read and write consistency. So, for example, you can do a ReadMajority where "the value will be read and merged from a majority of replicas".

Incidentally, the PNCounter works as you describe, using two distributed counters to maintain increments and decrements.