CodePark/ questions / Distributed locks
START PRACTICINGPRACTICE
Backend question · Redis & caching › Distributed locks

Older code acquires a lock with SETNX lock:job 1 followed by EXPIRE lock:job 30. What is the risk?

If the client crashes between the two commands, the lock has no expiry and stays forever

If the client crashes between the two commands, the lock has no expiry and stays forever. The two commands are not atomic. A crash or network failure after SETNX leaves a lock with no TTL. It blocks everyone until someone deletes it by hand. SET key token NX PX 30000 does both in one atomic command.