Skip to content

feat(keeper): take postgres advisory lock per job to prevent replica collisions - #1059

Merged
DevTobis merged 3 commits into
DevTobis:mainfrom
ntttttbl123-blip:feat/keeper-advisory-lock-58
Oct 23, 2026
Merged

DevTobis merged 3 commits into
DevTobis:mainfrom
ntttttbl123-blip:feat/keeper-advisory-lock-58

Conversation

@ntttttbl123-blip

Copy link
Copy Markdown
Contributor

Summary

Acquire a Postgres session-level advisory lock (pg_try_advisory_lock) per keeper job tick to prevent race conditions, duplicate submissions, and sequence number collisions across multiple replicas or during rolling restarts.

Changes

  • Export createRawClient and RawSql from @ballast/db for raw SQL connection acquisition.
  • Implement PostgresLockProvider and executeJobWithLock in services/keeper/src/lock.ts using dedicated reserved connections and session-level advisory locks.
  • Support configurable lock scope via KEEPER_LOCK_SCOPE=job|global (defaulting to job scope: hashtext("keeper:" || name)).
  • Wrap execute and runJob in services/keeper/src/index.ts to log and skip ticks when the advisory lock is busy.
  • Add unit tests verifying replica concurrency avoidance, lock release on crashed jobs, and scope handling.
  • Document replica behaviour and advisory locking in services/keeper/README.md.

Closes #58

@drips-wave

drips-wave Bot commented Oct 18, 2026

Copy link
Copy Markdown

@ntttttbl123-blip Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@DevTobis
DevTobis merged commit c9d46c0 into DevTobis:main Oct 23, 2026
4 checks passed
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.

Take a Postgres advisory lock per keeper job so two replicas cannot double-submit

2 participants