Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating scheduled jobs in a Multi-Tenant application

I am building a Multi-Tenant web application using Laravel/PHP that will be hosted on AWS as SaaS at the end. I have around 15-20 different background jobs that need scheduling for each tenant. The jobs need to be fired every 5 minutes as well. Thus the number of jobs which need to be fired for 100 tenants would be around 2000. I am left with 2 challenges in achieving this

  1. Is there a cloud solution that distributes and manages the load of the scheduled jobs automatically?
  2. If one is out there, how can we create those 15+ scheduled jobs on the fly? Is there an API available?

Looking for your assistance

like image 295
Rinsad Ahmed Avatar asked Oct 29 '25 05:10

Rinsad Ahmed


1 Answers

Finally, I have found a solution to my problem.

We cannot scale the background jobs in the way I want. It required me to look into the solution from a completely different angle.

The ideal solution to my problem is that I should generate SQS messages (with a payload describing the tenant id, the job needs to be executed and any additional parameters) corresponding to the number of tenants on a set interval and queue it.

For example, if I have 100 tenants and I want to run "Job 1" every our, the main application will generate 100 SQS messages and queue it in a particular SQS Queue every hour. It will do the same for all 15 different jobs I have per tenant.

On the other end, a scalable AWS Lambda function listening to the SQS queue will pick up the payload and execute the intended task based on the data being carried by the payload.

But unfortunately, my expertise lies in PHP/Laravel technology which is still not in the AWS Lambda stack. Hence I figured out a workaround as follows.

I built a Docker image with my PHP/Laravel application and placed it in Amazon ECS (EC2 container service). Still, I have the AWS Lambda function in place but this time it acts as a trigger to my docker containers. The Lambda picks an SQS Message, processes the payload and spawns a Docker container on ECS based on my Docker image. I got some of the ideas from the following article to arrive at this solution.

https://aws.amazon.com/blogs/compute/better-together-amazon-ecs-and-aws-lambda/

like image 193
Rinsad Ahmed Avatar answered Oct 30 '25 21:10

Rinsad Ahmed