Queue depth is the number of messages waiting in a message queue at a given time. In Amazon SQS, it indicates how much asynchronous work remains for consumers to process. section 17.3.2 section 17.3
Queue depth can drive capacity changes. A CloudWatch alarm monitors the SQS ApproximateNumberOfMessagesVisible metric; when the threshold is exceeded, it triggers an Auto Scaling policy. section 17.3.2
Use queue depth to connect workload pressure with worker capacity:
- Scale up when the queue reaches five messages by adding an EC2 instance. section 17.3.2
- Scale down when the queue becomes empty, using an alarm and policy with opposite values. section 17.3.2
- Prefer step scaling here because message count does not directly correlate with the number of instances processing messages. section 17.3.2
URL screenshot workers
A bookmark sends a URL and identifier to an SQS queue. Node.js applications on EC2 poll the queue, create screenshots, and store them in S3. Queue depth controls the desired number of worker instances. section 17.3.2
Listing 17.7 Monitoring the length of the SQS queue
# [...]
HighQueueAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
EvaluationPeriods: 1 #1
Statistic: Sum #2
Threshold: 5 #3
AlarmDescription: 'Alarm if queue length is higher than 5.'
Period: 300 #4
AlarmActions: #5
- !Ref ScalingUpPolicy
Namespace: 'AWS/SQS' #6
Dimensions: #7
- Name: QueueName
Value: !Sub '${SQSQueue.QueueName}'
ComparisonOperator: GreaterThanThreshold #8
MetricName: ApproximateNumberOfMessagesVisible #9
# [...]
Inspect the CloudWatch alarm configuration for monitoring SQS queue depth, including its threshold, period, namespace, and metric name. section 17.3.2
