'Spring Data Redis resolving race conditions

Currently I am trying to develop an application using Spring Data Redis but I’m looking for a solution to the current race condition I am facing.

The general idea for the application is:

  1. Retrieve a key from redis where the value is null (redis will be prepopulated with many keys which are null)
  2. Update the value to something.

However I with multiple nodes of the application running concurrently, I envision that there could be a race condition where 2 nodes both retrieve the same key and both update the value meaning one node will overwrite the value set by the other node. Instead I would like both nodes to retrieve different keys to update.

I was wondering what would be the best way to avoid this race condition on Spring Data Redis.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source