finds.dev

ClosureTree/with_advisory_lock

Advisory locking for ActiveRecord

View on GitHubHomepage ↗

★ 702RubyNOASSERTIONupdated Aug 2026

// the take

A gem that wraps MySQL GET_LOCK and PostgreSQL advisory lock functions so ActiveRecord apps can coordinate work across processes and hosts without a separate lock service. Useful for anyone running more than one app server or worker and needing a cross-host mutex for things like scheduled jobs or idempotent imports.

Handles the PgBouncer transaction-pooling gotcha explicitly — session-level locks will strand on connection reuse, so it documents and supports transaction: true as the fix instead of leaving you to discover it in production. The blocking: true mode hands off to Postgres's own deadlock detector (PG::TRDeadlockDetected) rather than rolling custom polling logic, and still bounds it with a transaction-scoped SET LOCAL lock_timeout that cleanly rolls back to a savepoint instead of aborting the whole transaction. Test suite includes actual parallelism and race-condition tests against real MySQL/Postgres via docker-compose, not mocked locking behavior.

MySQL and Postgres aren't actually interchangeable despite the shared API — no shared locks, no native deadlock detection, and blocking: true silently no-ops on MySQL, so switching databases under this gem isn't a drop-in swap. Recently dropped SQLite and MySQL 5.7, and now requires Rails 7.2+/Ruby 3.3+, with no stated migration path for anyone still on older stacks. Clustered MySQL is unsupported (confirmed dead-end issue, not just undocumented), which matters if you're running Vitess or similar. No guidance anywhere on connection pool sizing, even though a held advisory lock — especially in blocking mode — ties up a connection for the wait duration, which can starve a small pool under contention.

We read the README, the directory tree and a key source file in October 2026. Nobody pays to be listed. How we judge →

// at a glance

Stars
702
Forks
72
Language
Ruby
License
NOASSERTION
Last commit
5 Aug 2026

// nearby

If with_advisory_lock isn't quite it.

All repos →