Skip to content

Add Redis distributed lock so sweep workers run on a single active instance - #257

Merged
meshackyaro merged 2 commits into
trustflow-protocol:mainfrom
defimomof2:backend/182-distributed-lock
Aug 30, 2026
Merged

Add Redis distributed lock so sweep workers run on a single active instance#257
meshackyaro merged 2 commits into
trustflow-protocol:mainfrom
defimomof2:backend/182-distributed-lock

Conversation

@defimomof2

Copy link
Copy Markdown
Contributor
  • DistributedLockService (src/common/redis/distributed-lock.service.ts) — Redis SET key token NX PX ttl lease + compare-and-delete release (Lua). Wired into GigExpiryWorkerService and RepinWorkerService: each tick acquires a fresh lock (TTL = 1.5x the sweep interval) before running runOnce(), skips the tick if another instance holds it, and releases explicitly on onModuleDestroy. If REDIS_CLIENT is null (REDIS_URL unset), locking fails open — every instance runs independently with a warning logged, preserving today's single-instance behavior instead of silently disabling the sweep; this is documented in the service's doc comment. Existing worker specs updated to inject a fake lock and still pass; new distributed-lock.service.spec.ts covers concurrent-acquire racing (exactly one winner), release/re-acquire, compare-and-delete (can't release a lock you don't hold), and the fail-open path. Closes Add distributed lock so background sweep workers run as a single active instance #182
  • Design a Horizontally-Scalable WebSocket Gateway with Redis Fan-out #165 — Horizontally-Scalable WebSocket Gateway with Redis Fan-out — to be done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add distributed lock so background sweep workers run as a single active instance

2 participants