In this lesson, we will explore how to create and configure queues using Amazon Simple Queue Service (SQS). By the end of this lesson, you will understand how Amazon SQS works and how it can be used with Laravel workers.
The benefits of Amazon SQS are high scalability, thus it can handle a virtually unlimited number of messages per second. This makes it ideal for scenarios where the workload is expected to grow rapidly. Also, it is cost-effective as pricing is based on usage, which means that you only pay for the messages you send and receive.
We can look up pricing in this table:
Few things to note:
Along pricing Amazon also provides the Calculator so you can measure your monthly estimated costs.
To consume AWS services such as Amazon SQS first we need to install the AWS SDK package using composer:
composer require aws/aws-sdk-php:~3.0
arn:aws:iam::****:user/****
and will be required when setting send/receive permissions for the queue messages.Secret access key is displayed only once in this view, so if you fail to save that key you need to create new keys.
sqs
in the top search bar.eu-central-1
.mailing
to send user verification emails.A queue name is case-sensitive and can have up to 80 characters. You can use alphanumeric characters, hyphens (-), and underscores ( _ ).
Now let's briefly discuss queue Types.
Standard queue doesn't offer message de-duplication and it will behave like any other Laravel queue, and if you send the same job more than once, it will be processed multiple times as you would expect. It is important to note that there's no guarantee that jobs will be processed in the same order as they were dispatched. Amazon SQS will attempt to process them in the order they are received however it is not always possible because standard queues are designed to be massively scalable using a highly distributed architecture.
FIFO (first-in-first-out) queues preserve the exact order in which messages are sent and received. Unlike standard queues, FIFO queues don't introduce duplicate messages. FIFO queues avoid sending duplicates to a queue.
Amazon SQS is the only queue connection that does not contain the retry_after
(timeout) value. On Amazon SQS it is called Visibility timeout
Visibility timeout value sets the length of time that a message received from a queue will not be visible to other workers. This timeout starts when Amazon SQS returns a message. If the job fails to process and delete the message before the visibility timeout expires, the message becomes visible to other consumers (workers) and this is applied to all messages.
Typically you should set the visibility timeout to the maximum time that it takes your application to process and delete a message from the queue.
For example, if the visibility timeout value is set to 30 seconds and it takes more than 30 seconds to process a job, another queue worker will start processing it again, so when configuring the supervisor stopwaitsecs
value must be not higher than the visibility timeout.
Other values can be left at defaults.
arn:aws:iam::****:user/****
.URL
of the queue in the second column for later configuration. It has the following format https://sqs.<REGION>.amazonaws.com/<IAM-USER-ID>/<QUEUE-NAME>
It is time to update the environment file with the credentials and SQS values for a queue we just created.
SQS_PREFIX
should have a Queue URL without the last segment, which means we don't include the last part of the URL with the queue name /mailing
.
SQS_QUEUE
is where we put the queue name.
.env
QUEUE_CONNECTION=sqs //... AWS_ACCESS_KEY_ID=<YOUR-ACCESS-KEY-ID>AWS_SECRET_ACCESS_KEY=<YOUR-SECRET-ACCESS-KEY>AWS_DEFAULT_REGION=<YOUR-REGION>SQS_PREFIX=<YOUR-QUEUE-URL-WITHOUT-LAST-SEGMENT>SQS_QUEUE=<YOUR-QUEUE-NAME>
Finally, we can write supervisor configuration to run SQS queues.
For Ubuntu users - /etc/supervisor/conf.d/amazon-sqs.conf For Fedora users - /etc/supervisord.d/amazon-sqs.ini
[program:amazon-sqs]directory=/home/web/laravel-queuescommand=php artisan queue:work process_name=%(program_name)s_%(process_num)02dautostart=trueautorestart=truestopasgroup=truekillasgroup=trueuser=webnumprocs=10redirect_stderr=truestdout_logfile=/home/web/.supervisor/amazon-sqs.logstopwaitsecs=30
Save the file and then update the supervisor configuration with this command:
sudo supervisorctl update
Try registering a new account, and a verification email should be delivered.
It is possible to specify queue connection while invoking the queue:work
command like this instead of the .env file.
command=php artisan queue:work sqs
This also doesn't limit you to only one SQS queue, you can have multiple configurations of worker groups on the supervisor for each queue with different commands like:
command=php artisan queue:work sqs --queue=mailing
command=php artisan queue:work sqs --queue=another-queue-name