Sync Mutex
Lease-based distributed lock via Redis SET NX PX. One mutex instance per lock namespace, one lock per resource string.
Decision Guide
- Must-have exclusivity:
withLockOrThrow(resource, fn)— throwsLockErrorif acquire fails. - Best-effort exclusivity:
withLock(resource, fn)— returnsnullif acquire fails. - Manual control:
acquire()→ work →release()infinallyblock. - Long work: call
extend(lock, ttlMs)before expiry. Ifextend()returnsfalse, the lock was lost — stop exclusive work immediately.
Gotchas
defaultTtlis 10s. If critical section takes longer and you don'textend(), the lock expires and another process can acquire it.retryCountdefault is 10,retryDelaydefault 200ms (+0-100ms jitter). Total acquire budget is ~2-3s.- Resources > 128 chars are auto-hashed: key becomes
{prefix}:{id}:hash:{sha256}. - Release and extend use Lua owner-check scripts — only the lock holder can release/extend.
- No automatic lease renewal — you must call
extend()explicitly. - No fairness: contenders are not queued in order.
lock.expirationisDate.now() + ttlat acquire/extend time. Use it to check remaining time before starting new work.- The scheduler module uses mutex internally for leader election.
API Reference
Read full API/types/config/defaults in references/api.md.