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?

Intermediate

Answer

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 seconds
Real-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.

Common follow-ups: How do you choose an appropriate expiration time that is neither too short nor too long?;Can you extend a lock's expiration time if the protected operation is taking longer than expected?