2

Is there an algorithm to allow reliable mutex using a key-value storage that only supports set() and get() operations plus current time check?

Some service API offers in-memory storage with only simple set(key, value) and get(key) methods. I learned that Redis has a very nice distributed locks algorithm, that relies on the NX "non-existing" flag on insertion. This service does not have that feature - it will always set.

There is also a utility method to get current server's time with seconds precision. It is possible to create simple stored procedures that run on server, so network delays can be ignored.

I was thinking of some delayed implementation:

  1. get key to see if it exists (or exists and contains "free" value);
  2. set key to current timestamp + client ID;
  3. wait 1-2 seconds;
  4. get the key to verify it still has this client's ID and a timestamp that is 1-2 seconds old;
  5. only then consider the lock acquired. If #1 or #4 fails, consider locked by other client.

Is there a well-known algorithm like that?

Serge
  • 1,531
  • 2
  • 21
  • 44

0 Answers0