More Info:
Amazon Simple Queue Service (SQS) queues should not be holding a high number of unsuccessfully-processed messages due to unresponsive or incapacitated consumers.Risk Level
MediumAddress
ReliabilityCompliance Standards
NISTTriage and Remediation
Remediation
Using Console
Using Console
Step 1: Log into the AWS Management ConsoleStep 2: Navigate to the Amazon Simple Queue Service (SQS) dashboard by selecting ‘Services’ from the top menu and then selecting ‘SQS’ under the ‘Application Integration’ section.Step 3: In the Amazon SQS dashboard, you will see a list of all your SQS queues. Identify the queue that has a high number of unprocessed messages.Step 4: Click on the queue name to open its details.Step 5: Check the ‘Messages Available for Processing’ metric. If this number is high, it means that there are many unprocessed messages in the queue.Step 6: To remediate this, you need to either increase the number of consumers processing messages from the queue or increase the processing speed of the existing consumers.To increase the number of consumers:Step 7: Go to the AWS Lambda console and find the function that is processing the messages from the SQS queue.Step 8: Increase the ‘Concurrent executions’ limit for the Lambda function. This will allow more instances of the function to run in parallel, thus processing messages from the queue faster.To increase the processing speed of the existing consumers:Step 9: Review the code of the Lambda function that is processing the messages. Look for any inefficiencies or bottlenecks that could be slowing down the processing speed.Step 10: Optimize the code to process messages faster. This could involve things like batching operations, using faster algorithms, or reducing the amount of data being processed.Step 11: After making changes, monitor the ‘Messages Available for Processing’ metric in the SQS dashboard to ensure that the number of unprocessed messages is decreasing.Remember, the goal is to ensure that messages are being processed at least as fast as they are being added to the queue. If the number of unprocessed messages continues to grow, you may need to further increase the number of consumers or the processing speed.
Using CLI
Using CLI
In order to remediate this issue, you need to identify the reason behind the high number of unprocessed messages in your SQS queue. The problem could be due to slower processing of messages by the consumer or the producer sending messages at a faster rate than the consumer can handle.Here are the steps to remediate this issue:
-
Identify the Queue: Use the following AWS CLI command to list all your SQS queues and identify the one with the high number of unprocessed messages:
-
Check the Queue Attributes: Use the following AWS CLI command to check the attributes of the identified queue:
Look for the
ApproximateNumberOfMessages
andApproximateNumberOfMessagesNotVisible
attributes. These will give you an idea of the number of messages waiting to be processed and the number of messages currently being processed. -
Identify the Issue: If the number of
ApproximateNumberOfMessages
is high, it means your consumer is not processing the messages fast enough. If theApproximateNumberOfMessagesNotVisible
is high, it means the messages are not being deleted after being processed. - Increase the Number of Consumers: If the issue is with the consumer not processing the messages fast enough, you may need to increase the number of consumers. This can be done by either increasing the number of instances of your consumer application or by increasing the number of threads in your consumer application that are processing the messages.
-
Increase the Visibility Timeout: If the issue is with the messages not being deleted after being processed, you may need to increase the visibility timeout. This can be done using the following AWS CLI command:
- Monitor the Queue: After making these changes, monitor the queue to see if the number of unprocessed messages decreases. If it doesn’t, you may need to further increase the number of consumers or the visibility timeout.
<Your-Queue-URL>
and <New-Timeout-Value>
with your actual queue URL and desired timeout value.Using Python
Using Python
You can use the Boto3 library in Python to interact with AWS services including SQS. Here’s how you can remediate the issue:
-
Install Boto3: You can install Boto3 using pip. If you haven’t installed it yet, you can do so using the following command:
-
Set up AWS credentials: You need to configure your AWS credentials. You can do this by using the AWS CLI and running
aws configure
. This will prompt you for your AWS Access Key ID, Secret Access Key, default region name, and default output format. -
Import Boto3: In your Python script, you need to import the Boto3 library. You can do this with the following line of code:
-
Create SQS Client: You need to create an SQS client. You can do this with the following line of code:
-
Get the Queue URL: You need to get the URL of the SQS queue. You can do this with the following line of code:
-
Process the Messages: Now, you need to process the messages. You can do this with a while loop that continues to run as long as there are messages in the queue. Here is an example:
receive_message
is used to get the messages from the queue, and delete_message
is used to delete a message from the queue after it has been processed. The MaxNumberOfMessages
parameter is set to 10, which is the maximum number of messages that can be retrieved in a single call.Remember that you need to replace 'YOUR_QUEUE_NAME'
with the name of your actual SQS queue.This will help in processing and removing the messages from the queue thereby reducing the number of unprocessed messages.Note: The above script assumes that you have appropriate permissions to read and delete messages from the queue. If you do not have these permissions, you will need to update your IAM policy accordingly.