-
Notifications
You must be signed in to change notification settings - Fork 28
Queue Multi-Task Handling #2460
Description
Laravel "Job Batching" allows you to dispatch multiple tasks at once. But I want to do the opposite: Have one handler receive multiple tasks at once.
For example, the ProcessPodcast class from the docs could be called ProcessPodcasts and the constructor would receive an array of Podcast classes instead of just one.
Configuration
There are two main configurations: "group size" and "wait time".
Group Size would be the maximum number of Podcast classes the ProcessPodcasts class can receive at once.
Wait Time is how long a job will wait for a full group of jobs. After some period of time (e.g. one minute) if there are still fewer than a full group of jobs, the queue will just send all waiting jobs to the handler.
Why would you want this?
When processing a job has significant setup overhead it may be more efficient to process multiple jobs at once.
Adding more queue workers does not solve this problem. Multiple works could allow you to process the jobs more quickly, but not more efficiently.
Example
Imagine that the ProcessPodcast job launches an EC2 instance to process each podcast. That setup takes a lot of time. It would be more efficient for the ProcessPodcasts class to receive a group of Podcasts so it can use the same container to process all of them.
Implementation in AWS Lambda
This feature already exists in AWS when using SQS and Lambda. You can configure each SQS message to trigger a Lambda function. Or you can configure a "batch size" and when your Lambda function is invoked it will receive an array of SQS messages.