djt
djt

Reputation: 7535

AWS - Load Balanced Instances & Cron Jobs

I have a Laravel application where the Application servers are behind a Load Balancer. On these Application servers, I have cron jobs running, some of which should only be run once (or run on one instance).

I did some research and found that people seem to favor a lock-system, where you keep all the cron jobs active on each application box, and when one goes to process a job, you create some sort of lock so the others know not to process the same job.

I was wondering if anyone had more details on this procedure in regards to AWS, or if there's a better solution for this problem?

Upvotes: 3

Views: 3509

Answers (3)

Paras
Paras

Reputation: 9465

Check out this package for Laravel implementation of the lock system (DB implementation): https://packagist.org/packages/jdavidbakr/multi-server-event

Also, this pull request solves this problem using the lock system (cache implementation): https://github.com/laravel/framework/pull/10965

Upvotes: 0

Mark B
Mark B

Reputation: 200617

You can build distributed locking mechanisms on AWS using DynamoDB with strongly consistent reads. You can also do something similar using Redis (ElastiCache).

Alternatively, you could use Lambda scheduled events to send a request to your load balancer on a cron schedule. Since only one back-end server would receive the request that server could execute the cron job.

These solutions tend to break when your autoscaling group experiences a scale-in event and the server processing the task gets deleted. I prefer to have a small server, like a t2.nano, that isn't part of the cluster and schedule cron jobs on that.

Upvotes: 5

Luc Hendriks
Luc Hendriks

Reputation: 2533

If you need to run stuff only once globally (so not once on every server) and 'lock' the thing that needs to be run, I highly recommend using AWS SQS because it offers exactly that: run a cron to fetch a ticket. If you get one, parse it. Otherwise, do nothing. So all crons are active on all machines, but tickets are 'in flight' when some machine requests a ticket and that specific ticket cannot be requested by another machine.

Upvotes: -1

Related Questions