finds.dev← search

// the find

ClosureTree/with_advisory_lock

★ 702 · Ruby · NOASSERTION · updated Aug 2026

Advisory locking for ActiveRecord

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.

View on GitHub → Homepage ↗

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →