Redis › Distributed Locks with Redis
What happens if the process holding a distributed lock crashes or hangs before it finishes its work and releases the lock?
IntermediateAnswer
This is exactly why a distributed lock should always be set with an expiration time, ensuring that even if the holding process crashes, hangs, or otherwise never explicitly releases the lock, it will automatically become available again after the timeout, preventing the resource from being permanently locked forever due to a single failure.
SET mylock 'owner_id' NX EX 30
-- Even if the owning process crashes,
-- this lock automatically expires after 30 secondsReal-world example A data processing job that unexpectedly crashed while holding a lock does not permanently block future job runs, since the lock's expiration time ensures it becomes available again automatically a short time later.