jackyfkc.github.io

教土豆学计算机

Distributed Lock

Version 1: Locking with SETNX

SETNX can be used as a locking primitive. For example, to acquire the lock of key foo, the client could try the following

SETNX lock.foo <current Unix timestamp + lock timeout + 1>

In order to making this locking mechanism more robust, a client holding the lock should always check the timeout did not expire before unlocking the key with DEL because client failures can be complex, not just crashing but also blocking a lot of time against some operations and trying to issue DEL after a lot of time (when the LOCK is already held by another client)

Handling deadlocks

What happens if a client fails, crashes, or is otherwise not able to release the lock?

Version 2:

Another property added here

Refers to