LockForge
A distributed lock service over HTTP. When several servers try to touch the same resource, exactly one gets the lock.
Overview
A small REST API for acquiring, releasing, extending, checking and listing named locks, backed by Redis. I built it to properly understand atomic operations. It's the kind of thing you need when two servers might process the same order at the same time.
Problem
A lock across machines has to be atomic (two servers can't both win), owner-safe (you can't release someone else's lock), and it can't stay stuck forever if the holder crashes.
Approach
Acquiring is a single SET key owner NX PX ttl, and since Redis runs commands one at a time only one caller can win. Release and extend have to check the owner and then act, so I wrote them as Lua scripts that run inside Redis and can't be interrupted halfway. The TTL handles crashed holders.
Architecture / how it works
acquireLock()is oneSET … NX PXcall.nullmeans the lock is taken, and the API answers 409 with the current owner andretry_after_ms.releaseLock()runs a Lua script:GET == ownerthenDEL. That closes the gap where another process grabs the lock between my check and my delete.extendLock()does the same thing withPEXPIRE: only the owner can extend.listLocks()walks keys withSCAN … MATCH lock:* COUNT 100instead ofKEYS, which would block Redis.- Release returns 403 for "not your lock" and 404 for "not locked". A GET after the script tells the two apart.
- The default TTL is 30s, and it runs next to
redis:7-alpinein Docker Compose.
Tech stack
Outcomes
All five operations (acquire, release, check, extend, list) work. scripts/test-concurrent.sh fires 5 acquire requests at the same moment to check that only one gets a 200.