Solution for two-node nestjs cron service

Viewed 518

I have a nestjs cron service that is deployed to two nodes in uat/production.

If i have record that is scheduled to be created at 3 pm, both nodes will fire off at the same time, resulting in a duplicate record in the db.

I am unable to use redis as it would cost money to provision.

I have considered locking the table as well.

Anyone have ideas on how i can solve this issue?

Thank you!

1 Answers

Ideally the way would be to create a lock in shared memory and run only when lock is acquired.

You can replicate the behaviour using the DB. Create a table that stored a lock and allow the cron job to fire only if they are able to acquire the lock else skip. This method would be alright if you're okay with any of the two to execute the job.

If you want to implement a round robin like execution, you can use the lock along with an ID of the previous service which executed, and grant the lock to another ID. But you'll have to ensure there is more than one ID.

If you are fixed on two nodes, a very simple approach would be to displace the first run of the execution by 3 hours, and let each instance run the cron job once in 6 hours.

You may look into the different ways concurrency is handled at an OS level and implement it.

Related