Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating integer atomically over multiple JVMs for every key

We have a requirement, where the problem can be narrowed down as.

  • There are multiple keys and each key maps to a integer.
  • When a key is received on a JVM, you need to retrieve the int value from the shared memory, increment it and then put the incremented value back on the shared memory.

So when two JVMs or two threads read the same value, then the update of one of them should fail consistently, so that you do not lose any increment done by any of the thread on any of the JVM.

Once an update fails, you read again from the shared memory, increment it and then update again till the update is successful or you have exhausted some 'N' number of retries.

Right now we are using infinispan with optimistic locking, but the behavior is not consistent. Please find the link to that thread.

https://developer.jboss.org/message/914490

Is there any other technology which will fit in well for this requirement.

like image 514
Abhishek Agarwal Avatar asked Dec 24 '14 03:12

Abhishek Agarwal


1 Answers

Synchronizing between threads is easy, but between JVMs is extremely hard, especially if you need to support multiple platforms. I would suggest centralising the update code using one of the following methods, both of which "contract out" the data update task:

  • Publish a trivial REST API from a single process that knows how to do the update task, and serialize the requests.
  • Use a relational database to hold the counts, and make sure the client code correctly rolls back transactions when they don't succeed.

Probably not what you wanted to hear, but either method will work well.

like image 124
kiwiron Avatar answered Oct 30 '22 21:10

kiwiron