Menu

Quick tip: one Postgres advisory lock stops your scheduled job running twice
📰

Quick tip: one Postgres advisory lock stops your scheduled job running twice

DEV Community: postgres·delay.run·about 14 hours ago
#pdV8bvvV
#devtopostgresql#devto#dev#lock#connection
Reading 0:00
15s threshold

You scaled your app from one server to two. Everything got faster, except that the nightly invoice job now runs twice, and some customers got two invoices.

It's one of the most common surprises in growing a backend: scheduled code that was safe on one machine isn't safe on several. The heavyweight fixes are a dedicated scheduler service, a leader election library, or a distributed lock in Redis or ZooKeeper. But if you already run PostgreSQL, there's a simpler tool sitting right there: advisory locks .

The tip

Wrap the job in a non-blocking advisory lock. Whoever gets the lock runs the job; everyone else skips it.

SELECT pg_try_advisory_lock ( 42 ) ;

Enter fullscreen mode

Exit fullscreen mode

pg_try_advisory_lock returns true if this session acquired the lock and false immediately if another session holds it. It never waits.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.