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

A job's duration varies from 1 second to several minutes. How do you choose the lock TTL without it expiring mid-job or blocking for ages after a crash?

Use a moderate TTL, such as 30 seconds, and extend it while the job runs.

  • A background timer in the lock holder renews the lock about every TTL/3.
  • It renews with a Lua script that checks the token, then calls PEXPIRE. So it only extends a lock it still owns.
  • If the renewal returns 0, the lock was lost. The job should stop, or at least not commit its results.

If the process crashes, the renewals stop. The lock then frees itself within one TTL. Several Redis lock libraries use this "watchdog" approach.

Renewal reduces the risk, but does not remove it. A long pause can still outlast the TTL before the timer runs. For work where correctness matters, add a fencing token or a conditional write at the resource.

  • Use a moderate TTL and renew it on a timer.
  • Renew with a Lua script that checks the token, then calls PEXPIRE.
  • A failed renewal means the lock is lost, so stop the work.
  • After a crash, renewals stop, so the lock frees within one TTL.
  • Long pauses can still break it, so use fencing for correctness.