My application sends messages to a AWS SQS queue for jobs that need some sort of background processing. My processing daemon receives and processes the messages like this:
$result = $sqsClient->receiveMessage([
'QueueUrl' => \Myapp\Config::get('sqs.queue'),
'WaitTimeSeconds' => 20,
'MaxNumberOfMessages' => 1
]);
if (isset($result['Messages'])) {
foreach ($result->getPath('Messages/*/Body') as $messageBody) {
$handler = new \Myapp\Handler();
$handler->dispatch($messageBody);
}
}
This works fine, however I can see from the SQS console that the messages that my script retrieves are put into the "Messages in Flight" category, and then after a while they're put back into "Messages Available", and my script ends up picking them up again.
How can I remove the messages from the SQS queue, or even better mark them as completed?
When you receive a message from an SQS queue, the message will (by default) return to the queue 30 seconds later. This is to handle cases where processing of the message crashes and the message needs to be processed again. Once your message is successfully processed, use deleteMessage to delete the message.
messages will be deleted from SQS immediately after the successful execution of a batch by lambda.
You can configure the Amazon SQS message retention period to a value from 1 minute to 14 days. The default is 4 days. Once the message retention quota is reached, your messages are automatically deleted.
If you don't want to delete an Amazon SQS queue but need to delete all of the messages from it, purge the queue. The message deletion process takes up to 60 seconds. We recommend waiting for 60 seconds regardless of your queue's size. Important. When you purge a queue, you can't retrieve any of the deleted messages.
When you receive a message from an SQS queue, the message will (by default) return to the queue 30 seconds later. This is to handle cases where processing of the message crashes and the message needs to be processed again.
Once your message is successfully processed, use deleteMessage
to delete the message. You'll need the receiptHandle
value from the message when you received it from receiveMessage
in order to delete the message.
If typical processing of your message may take more than 30 seconds, then you can configure your queue to increase that "return to queue" time. This is called "Default Visibility Timeout" in the SQS queue configuration.
Also be aware that Amazon SQS works in such a way that:
You do need to delete messages after you process them - that would be considered 'marking them completed'; however, if something downstream needs to act on this 'completed'' action, its not uncommon for me to post a 'completion' message to another SQS queue, and then delete the message from the incoming queue. In this way another process, either now or down the road can take action that needs to be done when the first worker completes its work, i.e. chaining those processes together in a decoupled manner.
If there is nothing downstream that needs to be done, simply deleting it is enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With