Scheduled jobs
The same cron runs on every instance, and the lock makes sure exactly one of them does the work.
Make sure only one worker runs a job at a time, with locks that expire on their own and are released safely by their owner.
NX takes it only if nobody holds it. PX 30000 makes it expire in 30 seconds if A dies.
An animated walkthrough with real HotKV commands and example replies. Click any step to jump to it.
Cron jobs on several machines, payment captures and cache rebuilds must not run twice at once. A lock without an expiry is held forever when its owner crashes, and releasing a lock with a plain DEL can delete someone else's lock after yours has expired.
SET with NX and PX acquires a lock only if nobody holds it, and gives it an expiry so a crashed owner cannot hold it forever. Each owner stores a random token as the value. DELEX with IFEQ deletes the lock only if it still holds your token, which is the safe release that used to need a Lua script.
NX takes it only if nobody holds it. PX 30000 makes it expire in 30 seconds if A dies.
SET lock:billing-run tok-a81f NX PX 30000The lock is held, so B gets nil and skips this run.
SET lock:billing-run tok-b22c NX PX 30000Only one worker runs the billing job.
PTTL lock:billing-runDELEX deletes the key only if the value is still A's token.
DELEX lock:billing-run IFEQ tok-a81fIf A had been too slow and B now held the lock, A's release would leave B's lock alone.
DELEX lock:billing-run IFEQ tok-staleThe same cron runs on every instance, and the lock makes sure exactly one of them does the work.
Capture and refund operations on one order are serialised so they never overlap.
Only one request rebuilds an expensive cache entry while the others wait or serve the stale value.
SET NX PX takes the lock only if free, with a timeout.
SETNXPXDELEX IFEQ deletes only if the value matches your token.
DELEXCheck the remaining time and extend it for long jobs.
PTTLPEXPIREFor large values, DELEX IFDEQ compares a digest instead of the full value.
DIGESTIFDEQIf your lock expired and another worker took it, a plain DEL would delete their lock. DELEX IFEQ only deletes it while it still holds your token.
Longer than the job normally takes. For long jobs, extend the expiry periodically while you still hold the lock.
No. DELEX does the compare and delete in one command.
HotKV speaks the Redis protocol, so you can point an existing client at it and run these same commands.