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.…