If your application performs operations or workflows that take a long time to complete, you can offload those tasks to a dedicated worker environment. Decoupling your web application front-end from a process that performs blocking operations is a common way to ensure that your application stays responsive under load.
A long running task is anything that substantially increases the time it takes to complete a request, such as processing images or videos, sending email, or generating a ZIP archive. These operations may only take a second or two to complete, but a delay of a few seconds is a lot for a web request that would otherwise complete in less than 500ms.
One option is to spawn a worker process locally, return success, and process the task asynchronously. This works, as long as your instance can keep up with all of the tasks sent to it. Under high load, however, an instance can become overwhelmed with background tasks and become unresponsive to higher priority requets. If individual users can generate multiple tasks, the increase in load might not correspond to an increase in users, making it hard to scale out your web server tier effectively.
To avoid running long-running tasks locally, you can use the AWS SDK for your programming language to send them to an Amazon Simple Queue Service queue, and run the process that performs them on a separate set of instances. The worker instances only take items from the queue when they have capacity to run them, preventing them from becoming overwhelmed.
Elastic Beanstalk simplifies this process by managing the Amazon SQS queue and running a daemon process on each instance that reads from the queue for
you. When the daemon pulls an item from the queue, it sends an HTTP POST request locally to
http://localhost/ with the contents of the queue message in the body. All that
your application needs to do is perform the long-running task in response to the POST. You can
configure the daemon
to post to a different path, use a MIME type other than application/JSON, connect to an existing
queue, or customize connections, timeouts, and retries.
With periodic tasks, you can also configure the worker daemon to queue messages based on a cron schedule. Each periodic tasks can POST to a different path. Enable periodic tasks by including a YAML file in your source code that defines the schedule and path for each task.
The Worker Environment SQS Daemon
Worker environments run a daemon process provided by Elastic Beanstalk. This daemon is updated regularly to add features and fix bugs. To get the latest version of the daemon, update to the latest platform version.
August 11, 2015
Monitor environment health with more detail and accuracy.
February 17, 2015
Run cron jobs that you configure in a
May 27, 2014
Send failed jobs to a dead letter queue for troubleshooting.
Changed the default visibility timeout from 30 seconds to 300 seconds.
When the application in the worker environment returns a
200 OK response to
acknowledge that it has received and successfully processed the request, the daemon sends a
DeleteMessage call to the SQS queue so that the message will be deleted from
the queue. If the application returns any response other than
200 OK, then Elastic Beanstalk
waits to put the message back in the queue after the configured
ErrorVisibilityTimeout period. If there is no response, then Elastic Beanstalk waits to put
the message back in the queue after the
InactivityTimeout period so that the
message is available for another attempt at processing.
The properties of Amazon SQS queues (message order, at-least-once delivery, and message sampling) can affect how you design a web application for a worker environment. For more information, see Properties of Distributed Queues in the Amazon Simple Queue Service Developer Guide.
SQS automatically deletes messages that have been in a queue for longer than the
The daemon sets the following HTTP headers:
SQS message ID, used to detect message storms
Name of the SQS queue
UTC time, in ISO 8601 format, when the message was first received.
SQS message receive count
Mime type configuration; by default,
Dead Letter Queues
Elastic Beanstalk worker environments support Amazon Simple Queue Service (SQS) dead letter queues. A dead letter queue is a queue where other (source) queues can send messages that for some reason could not be successfully processed. A primary benefit of using a dead letter queue is the ability to sideline and isolate the unsuccessfully processed messages. You can then analyze any messages sent to the dead letter queue to try to determine why they were not successfully processed.
A dead letter queue is enabled by default for a worker environment if you specify an autogenerated Amazon SQS queue at the time you create your worker environment tier. If you choose an existing SQS queue for your worker environment, you must use SQS to configure a dead letter queue independently. For information about how to use SQS to configure a dead letter queue, see Using Amazon SQS Dead Letter Queues.
You cannot disable dead letter queues. Messages that cannot be delivered will always
eventually be sent to a dead letter queue. You can, however, effectively disable this feature
by setting the
MaxRetries option to the maximum valid value of 100.
The Elastic Beanstalk
MaxRetries option is equivalent to the SQS
MaxReceiveCount option. If your worker environment does not use an
autogenerated SQS queue, use the
MaxReceiveCount option in SQS to effectively
disable your dead letter queue. For more information, see Using Amazon SQS Dead Letter
For more information about the lifecycle of an SQS message, go to Message Lifecycle.
You can define periodic tasks in a file named
cron.yaml in your
source bundle to add jobs to your worker environment's queue automatically at a regular
For example, the following
cron.yaml file creates two periodic tasks,
one that runs every 12 hours and a second that runs at 11pm UTC every day:
version: 1 cron: - name: "backup-job" url: "/backup" schedule: "0 */12 * * *" - name: "audit" url: "/audit" schedule: "0 23 * * *"
name must be unique for each task. The URL is the path to which
the POST request is sent to trigger the job. The schedule is a CRON expression that
determines when the task runs.
When a task runs, the daemon posts a message to the environment's SQS queue with a header indicating the job that needs to be performed. Any instance in the environment can pick up the message and process the job.
Elastic Beanstalk uses leader election to determine which instance in your worker environment queues the periodic task. Each instance attempts to become leader by writing to a DynamoDB table. The first instance that succeeds is the leader, and must continue to write to the table to maintain leader status. If the leader goes out of service, another instance quickly takes its place.
For periodic tasks, the worker daemon sets the following additional headers:
For periodic tasks, the name of the task to perform.
Custom message attributes assigned to the message being processed. The
Time at which the periodic task was scheduled
AWS account number of the sender of the message
Use Amazon CloudWatch for Auto Scaling in Worker Environment Tiers
Together, Auto Scaling and CloudWatch monitor the CPU utilization of the running instances in the worker environment. How you configure the autoscaling limit for CPU capacity determines how many instances the autoscaling group runs to appropriately manage the throughput of messages in the SQS queue. Each EC2 instance publishes its CPU utilization metrics to CloudWatch. Auto Scaling retrieves from CloudWatch the average CPU usage across all instances in the worker environment. You configure the upper and lower threshold as well as how many instances to add or terminate according to CPU capacity. When Auto Scaling detects that you have reached the specified upper threshold on CPU capacity, Elastic Beanstalk creates new instances in the worker environment. The instances are deleted when the CPU load drops back below the threshold.
Messages that have not been processed at the time an instance is terminated are returned to the queue where they can be processed by another daemon on an instance that is still running.
You can also set other CloudWatch alarms, as needed, by using the AWS Management Console, CLI, or the options file. For more information, go to Using Elastic Beanstalk with Amazon CloudWatch and Use Auto Scaling Policies and Amazon CloudWatch Alarms for Dynamic Scaling.
Configuring Worker Environments
You can manage a worker environment's configuration by editing Worker Configuration on the Configuration page in the environment management console.
To configure the worker daemon
The Worker Details page has the following options:
Worker queue – Specify the Amazon SQS queue from which the daemon reads. You can choose an existing queue, if you have one. If you choose Autogenerated queue, Elastic Beanstalk creates a new Amazon SQS queue and a corresponding Worker queue URL.
Worker queue URL – If you choose an existing Worker queue, then this setting displays the URL associated with that Amazon SQS queue.
HTTP path – Specify the relative path to the application that will receive the data from the Amazon SQS queue. The data is inserted into the message body of an HTTP POST message. The default value is
MIME type – Indicate the MIME type that the HTTP POST message uses. The default value is
application/json. However, any value is valid because you can create and then specify your own MIME type.
Max retries – Specify the maximum number of times Elastic Beanstalk attempts to send the message to the Amazon SQS queue before moving the message to the dead letter queue. The default value is
10. You can specify a value between
HTTP connections – Specify the maximum number of concurrent connections that the daemon can make to any application(s) within an Amazon EC2 instance. The default is
50. You can specify a value between
Connection timeout – Indicate the amount of time, in seconds, to wait for successful connections to an application. The default value is
5. You can specify a value between
Inactivity timeout – Indicate the amount of time, in seconds, to wait for a response on an existing connection to an application. The default value is
180. You can specify a value between
Visibility timeout – Indicate the amount of time, in seconds, an incoming message from the Amazon SQS queue is locked for processing. After the configured amount of time has passed, the message is again made visible in the queue for another daemon to read. Choose a value that is longer than you expect your application requires to process messages, up to
Error visibility timeout – Indicate the amount of time, in seconds, that elapses before Elastic Beanstalk returns a message to the Amazon SQS queue after an attempt to process it fails with an explicit error. You can specify a value between
Retention period – Indicate the amount of time, in seconds, a message is valid and will be actively processed. The default value is
345600. You can specify a value between
If you use an existing Amazon SQS queue, the settings that you configure when you create a
worker environment can conflict with settings you configured directly in Amazon SQS. For example,
if you configure a worker environment with a
RetentionPeriod value that is higher
MessageRetentionPeriod value you set in Amazon SQS, then Amazon SQS will delete
the message when it exceeds the
Conversely, if the
RetentionPeriod value you configure in the worker
environment settings is lower than the
MessageRetentionPeriod value you set in
Amazon SQS, then the daemon will delete the message before Amazon SQS can. For
VisibilityTimeout, the value that you configure for the daemon in the worker
environment settings overrides the Amazon SQS
VisibilityTimeout setting. Ensure that
messages are deleted appropriately by comparing your Elastic Beanstalk settings to your Amazon SQS