Automatically update an integer across multiple JVMs for each key

We have a requirement in which the problem can be narrowed down as.

  • There are several keys, and each key is mapped to an integer.
  • When the key is received on the JVM, you need to get the int value from the shared memory, increase it, and then put the increased value back into the shared memory.

Therefore, when two JVMs or two threads read the same value, the update of one of them must be interrupted sequentially so that you do not lose any increment performed by any thread on any of the JVMs.

After the update fails, you read from the shared memory again, increase it and then update until the update is successful, or you have not exhausted the number of "N" attempts.

We are currently using infinispan with optimistic locking, but the behavior is incompatible. Please find a link to this topic.

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

Is there any other technology that is well suited to this requirement.

+6
source share
1 answer

Inter-thread synchronization is simple, but between the JVMs is extremely difficult, especially if you need to support multiple platforms. I would suggest centralizing the update code using one of the following methods, both of which “contract” the data update task:

  • Publish the trivial REST API from a single process that knows how to complete the update task and serialize the requests.
  • Use the relational database to store the counters and make sure the client code correctly rolls back transactions when they fail.

This is probably not what you wanted to hear, but any method will work well.

+1
source

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


All Articles