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:
- get key to see if it exists (or exists and contains "free" value);
- set key to current timestamp + client ID;
- wait 1-2 seconds;
- get the key to verify it still has this client's ID and a timestamp that is 1-2 seconds old;
- only then consider the lock acquired. If #1 or #4 fails, consider locked by other client.
Is there a well-known algorithm like that?